Tables and For

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Lamp,Kid of Wolfram
Prole
Posts: 2
Joined: Mon Jun 23, 2014 8:50 pm

Tables and For

Post by Lamp,Kid of Wolfram »

Hello ...
:emo:
I am pretty fresh in Lua and Love, would like to ask you guys how to make a piece of code simplier. I've been ctrl+C ctrl+V 'ing a lot. Would love to make this thing more loopy... There are no bugs though. Just a lump. ugly stuff.
so here it is.

Code: Select all


function love.load()
queen = {'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25'}
queen[1] = {mode,x,y}
queen[1][1]=3
queen[1][2]=720
queen[1][3]=80

queen[2] = {mode,x,y}
queen[2][1]=3
queen[2][2]=800
queen[2][3]=80

queen[3] = {mode,x,y}
queen[3][1]=3
queen[3][2]=880
queen[3][3]=80

queen[4] = {mode,x,y}
queen[4][1]=3
queen[4][2]=960
queen[4][3]=80

queen[5] = {mode,x,y}
queen[5][1]=3
queen[5][2]=1040
queen[5][3]=80

queen[6] = {mode,x,y}
queen[6][1]=3
queen[6][2]=720
queen[6][3]=160

queen[7] = {mode,x,y}
queen[7][1]=3
queen[7][2]=800
queen[7][3]=160

queen[8] = {mode,x,y}
queen[8][1]=3
queen[8][2]=880
queen[8][3]=160

queen[9] = {mode,x,y}
queen[9][1]=3
queen[9][2]=960
queen[9][3]=160

queen[10] = {mode,x,y}
queen[10][1]=3
queen[10][2]=1040
queen[10][3]=160

queen[11] = {mode,x,y}
queen[11][1]=3
queen[11][2]=720
queen[11][3]=240

queen[12] = {mode,x,y}
queen[12][1]=3
queen[12][2]=800
queen[12][3]=240

queen[13] = {mode,x,y}
queen[13][1]=3
queen[13][2]=880
queen[13][3]=240

queen[14] = {mode,x,y}
queen[14][1]=3
queen[14][2]=960
queen[14][3]=240

queen[15] = {mode,x,y}
queen[15][1]=3
queen[15][2]=1040
queen[15][3]=240

queen[16] = {mode,x,y}
queen[16][1]=3
queen[16][2]=720
queen[16][3]=320

queen[17] = {mode,x,y}
queen[17][1]=3
queen[17][2]=800
queen[17][3]=320

queen[18] = {mode,x,y}
queen[18][1]=3
queen[18][2]=880
queen[18][3]=320

queen[19] = {mode,x,y}
queen[19][1]=3
queen[19][2]=960
queen[19][3]=320

queen[20] = {mode,x,y}
queen[20][1]=3
queen[20][2]=1040
queen[20][3]=320

queen[21] = {mode,x,y}
queen[21][1]=3
queen[21][2]=720
queen[21][3]=400

queen[22] = {mode,x,y}
queen[22][1]=3
queen[22][2]=800
queen[22][3]=400

queen[23] = {mode,x,y}
queen[23][1]=3
queen[23][2]=880
queen[23][3]=400

queen[24] = {mode,x,y}
queen[24][1]=3
queen[24][2]=960
queen[24][3]=400

queen[25] = {mode,x,y}
queen[25][1]=3
queen[25][2]=1040
queen[25][3]=400
end
function love.update(dt)
		queen[1][3]=queen[1][3]+queenMoveSpeed *dt
		queen[2][3]=queen[2][3]+queenMoveSpeed *dt
		queen[3][3]=queen[3][3]+queenMoveSpeed *dt
		queen[4][3]=queen[4][3]+queenMoveSpeed *dt
		queen[5][3]=queen[5][3]+queenMoveSpeed *dt
		queen[6][3]=queen[6][3]+queenMoveSpeed *dt
		queen[7][3]=queen[7][3]+queenMoveSpeed *dt
		queen[8][3]=queen[8][3]+queenMoveSpeed *dt
		queen[9][3]=queen[9][3]+queenMoveSpeed *dt
		queen[10][3]=queen[10][3]+queenMoveSpeed *dt
		queen[11][3]=queen[11][3]+queenMoveSpeed *dt
		queen[12][3]=queen[12][3]+queenMoveSpeed *dt
		queen[13][3]=queen[13][3]+queenMoveSpeed *dt
		queen[14][3]=queen[14][3]+queenMoveSpeed *dt
		queen[15][3]=queen[15][3]+queenMoveSpeed *dt
		queen[16][3]=queen[16][3]+queenMoveSpeed *dt
		queen[17][3]=queen[17][3]+queenMoveSpeed *dt
		queen[18][3]=queen[18][3]+queenMoveSpeed *dt
		queen[19][3]=queen[19][3]+queenMoveSpeed *dt
		queen[20][3]=queen[20][3]+queenMoveSpeed *dt
		queen[21][3]=queen[21][3]+queenMoveSpeed *dt
		queen[22][3]=queen[22][3]+queenMoveSpeed *dt
		queen[23][3]=queen[23][3]+queenMoveSpeed *dt
		queen[24][3]=queen[24][3]+queenMoveSpeed *dt
		queen[25][3]=queen[25][3]+queenMoveSpeed *dt
		queen[1][2]=queen[1][2]-queenMoveSpeed *dt
		queen[2][2]=queen[2][2]-queenMoveSpeed *dt
		queen[3][2]=queen[3][2]-queenMoveSpeed *dt
		queen[4][2]=queen[4][2]-queenMoveSpeed *dt
		queen[5][2]=queen[5][2]-queenMoveSpeed *dt
		queen[6][2]=queen[6][2]-queenMoveSpeed *dt
		queen[7][2]=queen[7][2]-queenMoveSpeed *dt
		queen[8][2]=queen[8][2]-queenMoveSpeed *dt
		queen[9][2]=queen[9][2]-queenMoveSpeed *dt
		queen[10][2]=queen[10][2]-queenMoveSpeed *dt
		queen[11][2]=queen[11][2]-queenMoveSpeed *dt
		queen[12][2]=queen[12][2]-queenMoveSpeed *dt
		queen[13][2]=queen[13][2]-queenMoveSpeed *dt
		queen[14][2]=queen[14][2]-queenMoveSpeed *dt
		queen[15][2]=queen[15][2]-queenMoveSpeed *dt
		queen[16][2]=queen[16][2]-queenMoveSpeed *dt
		queen[17][2]=queen[17][2]-queenMoveSpeed *dt
		queen[18][2]=queen[18][2]-queenMoveSpeed *dt
		queen[19][2]=queen[19][2]-queenMoveSpeed *dt
		queen[20][2]=queen[20][2]-queenMoveSpeed *dt
		queen[21][2]=queen[21][2]-queenMoveSpeed *dt
		queen[22][2]=queen[22][2]-queenMoveSpeed *dt
		queen[23][2]=queen[23][2]-queenMoveSpeed *dt
		queen[24][2]=queen[24][2]-queenMoveSpeed *dt
		queen[25][2]=queen[25][2]-queenMoveSpeed *dt

end
function love.draw()
--1
		if queen[1][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[1][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[1][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[1][2], queen[1][3], 80, 80 )
--2
		if queen[2][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[2][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[2][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[2][2], queen[2][3], 80, 80 )
--3
		if queen[3][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[3][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[3][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[3][2], queen[3][3], 80, 80 )
--4
		if queen[4][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[4][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[4][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[4][2], queen[4][3], 80, 80 )
--5
		if queen[5][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[5][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[5][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[5][2], queen[5][3], 80, 80 )
--6
		if queen[6][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[6][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[6][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[6][2], queen[6][3], 80, 80 )
--7
		if queen[7][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[7][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[7][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[7][2], queen[7][3], 80, 80 )
--8
		if queen[8][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[8][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[8][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[8][2], queen[8][3], 80, 80 )
--9
		if queen[9][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[9][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[9][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[9][2], queen[9][3], 80, 80 )
--10
		if queen[10][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[10][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[10][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[10][2], queen[10][3], 80, 80 )
--11
		if queen[11][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[11][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[11][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[11][2], queen[11][3], 80, 80 )
--12
		if queen[12][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[12][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[12][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[12][2], queen[12][3], 80, 80 )
--13
		if queen[13][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[13][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[13][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[13][2], queen[13][3], 80, 80 )
--14
		if queen[14][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[14][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[14][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[14][2], queen[14][3], 80, 80 )
--15
		if queen[15][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[15][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[15][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[15][2], queen[15][3], 80, 80 )
--16
		if queen[16][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[16][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[16][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[16][2], queen[16][3], 80, 80 )
--17
		if queen[17][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[17][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[17][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[17][2], queen[17][3], 80, 80 )
--18
		if queen[18][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[18][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[18][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[18][2], queen[18][3], 80, 80 )
--19
		if queen[19][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[19][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[19][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[19][2], queen[19][3], 80, 80 )
--20
		if queen[20][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[20][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[20][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[20][2], queen[20][3], 80, 80 )
--21
		if queen[21][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[21][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[21][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[21][2], queen[21][3], 80, 80 )
--22
		if queen[22][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[22][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[22][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[22][2], queen[22][3], 80, 80 )
--23
		if queen[23][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[23][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[23][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[23][2], queen[23][3], 80, 80 )
--24
		if queen[24][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[24][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[24][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[24][2], queen[24][3], 80, 80 )
--25		
		if queen[25][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[25][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[25][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[25][2], queen[25][3], 80, 80 )



end
how to put this (the update and the draw parts) into a FOR ?
smt like

Code: Select all

for i=1,25 then
--i		
		if queen[i][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[i][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[i][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[i][2], queen[i][3], 80, 80 )


end
Hope I made it clear ... not good at English ...
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Tables and For

Post by Zilarrezko »

well, I would hate to explain with a paragraph and not have you understand it. So I hope that the best way to communicate is through the language of Lua (see what I did there?)

I think what you are looking for is nested loops, which is used for 2D arrays.

Code: Select all

for column = 1, #table do
    for row = 1, #table[column] do
        --do something with table[column][row]
    end
end
That's as simple as I can put it and as literal as I can put it.

If you'd like an explanation just ask. :awesome:
User avatar
riidom
Citizen
Posts: 74
Joined: Wed Jun 19, 2013 4:28 pm
Location: irgendwo an der Elbe
Contact:

Re: Tables and For

Post by riidom »

Hi, some things can surely be written shorter:

Code: Select all

queen = {'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25'}
--is same as..
queen = { } --need to declare queen as table first, before filling in values at the indices, or error; if you do implicit constructor like above ( queen={'1', '2', etc} ) then you dont need to do that
for i = 1, 25 do
  queen[i] = tostring(i)
end
for the next thing I am a bit confused.. you are overwriting queen[ i] here, because:

Code: Select all


queen[1][1]=3
queen[1][2]=720
queen[1][3]=80
--above 3 lines are same as:
queen[1] = {3, 720, 80}
so if there have been any values been stored in mode, x and y before, you will overwrite them with the hardcoded values

Code: Select all

queen[1][3]=queen[1][3]+queenMoveSpeed *dt
[...]
queen[25][3]=queen[25][3]+queenMoveSpeed *dt
-- here a for loop again:
for i = 1, 25 do
  queen[i][3] = queen[i][3]+queenMoveSpeed *dt
end
--you can probably put the queen[i][2] treatment in the same loop (but I cant really judge the code as whole)

Code: Select all

--for this..
		if queen[1][1]==1 then
			love.graphics.setColor( 255,0,0, 255 )
		end
		if queen[1][1]==2 then
			love.graphics.setColor(0,255,0,255)
		end
		if queen[1][1]==3 then
			love.graphics.setColor(0,0,255,255)
		end
		love.graphics.rectangle( 'fill', queen[1][2], queen[1][3], 80, 80 )
--2
[...]
--write a for loop again, same scheme as above, and the if's can happen like this:
  if queen[1][1]==1 then
    love.graphics.setColor( 255,0,0, 255 )
  elseif queen[1][1]==2 then
    love.graphics.setColor(0,255,0,255)
  elseif queen[1][1]==3 then
    love.graphics.setColor(0,0,255,255)
  else
    print "That shouldnt have happen!"
    love.graphics.setColor(255,0,255) -- ugliest color ever, also you can let the 4th param go, it defaults to 255
  end

--or you do it different, since you are mapping colours to numbers here.. the same thing can happens in an array, so lets try this:
col = { {255,0,0} , {0,255,0}, {0,0,255} } -- now col[1] = {255,0,0} ; also setColor can either take 3 numbers or 1 table with 3 numbers inside (or 4, respective, if you want alpha)
--now insert that in the if block above and we get (im typing a little lazy now, hope it stays understandable):
if q1[1] == 1 then setcolor( col[1] )
elseif q1[1] == 2 then setcolor( col[2] )
-- .. that looks pretty stupid now :) so we do better:
love.graphics.setColor( cols[ queen[1][1] ] ) -- and no if-then-else at all, because instead asking if the content of queen[x][1] is smth inbetween 1-3 each time, we just put the values for each of these cases in a little table and use the content of queen to grab the colour from our table directly (this works, because you do the same thing everytime queen[x][1] is 1 (or 2 or 3) )
There are probably other things that can be improved, I am for example wondering what you need the first thing for ( queen[10] = '10' ), but no need to fix everything at same time.
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Tables and For

Post by MadByte »

Also it might be much easier if you declare the variables of your queen tables with it's proper names not with [1] , [2], [3].
To do so you need a way to generate your queen objects and handle them.

Here is my suggestion ( should do almost the same as your code):

Code: Select all

----------------------
-- // Queen Test \\ --
----------------------

local queenArray = {}
local baseX, baseY = love.window.getWidth(), 16
local distance = 32
local modes = {{255, 0, 0}, {0, 255, 0}, {0, 0, 255} }

local function newQueen(x, y, mode)
  local queen = {}
  queen.x = x
  queen.y = y
  queen.width = 8
  queen.height = 8
  queen.speed = 50
  queen.mode = mode
  queenArray[#queenArray+1] = queen
  return queen
end


function love.load()
  for y = 0, 20 do
    for x = 0, 20 do
      newQueen(baseX + x * distance, baseY + y * distance, math.random(1, 3))
    end
  end
end


function love.update(dt)
  for k, queen in ipairs(queenArray) do
    queen.x = queen.x - queen.speed * dt
  end
end


function love.draw()
  for k, queen in ipairs(queenArray) do
    love.graphics.setColor(unpack(modes[queen.mode]))
    love.graphics.rectangle("fill", queen.x, queen.y, queen.width, queen.height)
  end
end
Here is the .love file !
QueenTest.love
As you can see the code is much shorter.
One thing to mension is - > forget the locals if you just started with lua & don't understand them. The same code also works without the "local".
Also you can see that I used "ipairs" to loop through the queenArray table. If you don't understand it, no worries. You can also use this instead to do the same thing ( but it is not as readable as using ipairs):

Code: Select all

function love.update(dt)
  for i = 1, #queensArray do
    queensArray[i].x = queensArray[i].x - queensArray[i].speed * dt
  end
end
Just to show you what the ipairs basically does:

Code: Select all

function love.update(dt)
  for i = 1, #queensArray do
    local queen = queensArray[i] -- this is all the magic, kinda!
    queen.x = queen.x - queen.speed * dt
  end
end
If there's something you don't get, feel free to ask us and we try to explain it :)
Happy coding !
Lamp,Kid of Wolfram
Prole
Posts: 2
Joined: Mon Jun 23, 2014 8:50 pm

Re: Tables and For

Post by Lamp,Kid of Wolfram »

I love you and you, and you. EVERYBODY IS AMAZING. thanks ... You people made my day... and a week... helped out a ton!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Semrush [Bot] and 3 guests