# Problems with conditionals based on table held values and the sprite command.

Current code which achieves matrix representation of a variety of 3 x 3 tiles in the output window.
No problems in functionality. Possible problems with style

``````function setup()
parameter.action("Get matrix",devtab)
parameter.integer("k",1,512,432)
parameter.action("Clear Output", function() output.clear() end )
basetile=readImage("Documents:newtile")  --these are to be used in the draw function section
xcoord={0,41,82,0,41,82,0,41,82}         -- ditto
ycoord={0,0,0,41,41,41,82,82,82}         -- ditto

m={}
for ident = 1, 512 do
m[ident]={}
for y=1,9 do
m[ident][y]=0
end
binary(ident,ident-1,1)
end
end

function devtab()

print("new matrix")
print(m[k][7],m[k][8],m[k][9])
print(m[k][4],m[k][5],m[k][6])
print(m[k][1],m[k][2],m[k][3])
end

function binary(ident,rntot,i)

rntot,r=math.modf(rntot/2)
if r>0 then r=1 else r=0 end
m[ident][i]=r
i = i + 1
if rntot>0 then binary(ident,rntot,i) else return
end
end
``````

So at this point I have a tile image a 3x3 grid upon which I wish to add additional images (shaded squares). Everything has been converted to the correct size so as to overlay nicely. Now, I can generate a single composite image using codea and save it no problem (learnt that bit)…except I want to create a bank of 512 of these tiles…so I want the computer to do it all for me. So it would seem obvious (and herein lies my downfall) that I should be able to say for example: if the entry in the matrix representation of matrix k which corresponds to the middle square m[k][5] of the grid m is equal to 1 then lay shady on top of basetile using corner coordinate 41,41

Which is looking something like this…(we are in the draw function and the basetile is already on screen and the sprite mode setting is okay etc)

So I have a list of 9 of these statements and generally nothing happens, or at most the three bottom shades randomly appear.
If I however remove the table references and replace them with a manual input of let’s say squareA = 1 and/or squareB = 1 etc as defined variables at the beginning of the draw function and then run the same conditional statements using these then it works perfectly…but then I’m back to doing everything myself again…

The problem is obviously the use of the table references as if these are removed the conditionals work fine and the composite image is generated…but why will they not transfer over…when they work fine for the print command into the output? What has it taken me 6 hours of my life to not get…To mix movie metaphors, Help me Obi wan Kenobi I’m shrinking!.. And losing the will to live.

It’s hard to say without seeing all the code - can you put it in a gist or something? I can’t see a problem with what you’ve explained, so it’s probably in another part of your code.

``````
--to convert the numbers 0 to 255 into binary
--and store the binary code for each number in a table.

function setup()
parameter.action("Get matrix",devtab)
parameter.integer("k",1,512,432)
parameter.action("Clear Output", function() output.clear() end )
basetile=readImage("Documents:newtile")  --these are to be used in the draw function section
xcoord={0,41,82,0,41,82,0,41,82}         -- ditto
ycoord={0,0,0,41,41,41,82,82,82}         -- ditto

m={}
for ident = 1, 512 do
m[ident]={}
for y=1,9 do
m[ident][y]=0
end
binary(ident,ident-1,1)
end
end

function devtab()

print("new matrix")
print(m[k][7],m[k][8],m[k][9])
print(m[k][4],m[k][5],m[k][6])
print(m[k][1],m[k][2],m[k][3])
end

function binary(ident,rntot,i)

rntot,r=math.modf(rntot/2)
if r>0 then r=1 else r=0 end
m[ident][i]=r
i = i + 1
if rntot>0 then binary(ident,rntot,i) else return
end
end

function draw()
spriteMode(CORNER)
sprite(basetile,0,0)
end
end
end
end
end
end
end
end
end
end

``````

It sort of works but doesn’t, once the 4th conditional goes in then things start getting missed out…something to do perhaps with the refresh rate and the input change of the slider…

I can see an immediate problem, which is that all your if statements in draw are nested. This means that you only test m[k][2] if m[k][1]==1, and so on.

What you need is this

``````for i=1, 9 do
if m[k][i]==1 then sprite(xcoord[i],ycoord[i]) end
end
``````

But when I run this, all the tiles pile up in the bottom corner. This is because your x and y coordinates are all in that corner, and maybe need to be adjusted.

Yes, now works perfectly, I had tried this previously and it hadn’t worked but I’d tried it with this

``````function setup()
parameter.action("Get matrix",devtab)
parameter.integer("k",1,512,1)
m={}
mcoordx={0,41,82,0,41,82,0,41,82}
mcoordy={0,0,0,41,41,41,82,82,82}
bitab={}
for z=0,511 do
bin(z)
end
end

function devtab()

for mtr=1,9 do
m[mtr]={string.sub(bitab[k],mtr,mtr),mcoordx[mtr],mcoordy[mtr]}
end
print("new matrix info")
print(m[7][1],m[8][1],m[9][1])
print(m[4][1],m[5][1],m[6][1])
print(m[1][1],m[2][1],m[3][1])
print(m[7][2],m[8][1],m[9][2])
print(m[4][2],m[5][1],m[6][2])
print(m[1][2],m[2][1],m[3][2])
end

function bin(z)
str=""
for a=8,0,-1 do
if z>=2^a then
str=str.."1"
z=z-2^a
else
str=str.."0"
end
end
table.insert(bitab,str)
end

function draw()
spriteMode(CORNER)
sprite(newtile,0,0)
for i = 1,9 do
end
end
``````

Now that I’ve had some sleep it would seem that the above wouldn’t work because it was calling strings…is that correct…but I thought lua handled string/number conversion in such instances…obviously I was wrong…is there a simple fix to the above…
Many thanks again

You can convert strings to number and vice versa, but comparing 1 with “1” gives false.

However, because Lua converts strings to numbers when you do arithmetic, multiplying the string value by 1 makes it a number, like so

``````a=1
b="1"
print(a==b) --> false
print(a==b*1) --> true
``````

use tonumber(string) to check it, this way you don’t have to multiply it by another number.
tonumber(“100”) == 100 gives true

Many thanks, that’s great.