Page 1 of 2

Table manipulation (replacing a field)

Posted: Fri Jan 15, 2010 10:51 pm
by dbltnk
Hey everyone. I'm currently trying to write a small game where you can run around on a 2D map. The map scrolling works fine. I'm using a table for the terrain. For the location of my character I'd also like to use a table. This table is exactly the same size as the terrain.

My plan is to move the marker for my character on the table and then drawing it at the new location. The drawing thing works fine, but I don't know how to over-write the table fields where the character is stored. Anyone got help?

Character location table (works):

Code: Select all

move = { {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
            {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	    {3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
            {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
	    {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}
   }
Character draw (works):

Code: Select all

function love.draw()
   for i, v in ipairs(move) do
      for j, w in ipairs(move[i]) do
         if w==4 then
            love.graphics.draw(char, (j-1+map_off_x)*mult, (i-1+map_off_y)*mult)
         end
      end
   end
end
Character movement / table manipulation (doesn't work):

Code: Select all

if key == 'w' then 
		for i, v in ipairs(move) do
			for j, w in ipairs(move[i]) do
				if w==4 then
				w = 3
				w+1 = 4
				end
			end
		end
	end
	

Re: Table manipulation (replacing a field)

Posted: Fri Jan 15, 2010 11:01 pm
by Robin
Remember, j is the index here. So you can set it with:

Code: Select all

if key == 'w' then
      for i, v in ipairs(move) do
         for j, w in ipairs(v) do --notice I used v here instead of move[i] -- it's the same, but v makes more sense
            if w==4 then
            v[j] = 3
            v[j+1] = 4 --though this might have some problems. I can't remember the behavior of ipairs when changing values right now.
            end
         end
      end
   end
Try it. ;)

Re: Table manipulation (replacing a field)

Posted: Sat Jan 16, 2010 12:43 am
by dbltnk
One the positive side, youre code is probably right as the program starts without any errors. On the negative side it's somehow flawed as the game freezes when I press the key that calls the code. =D Maybe I should use table.delete and table.insert. But how do I find out where to delete the old and insert the new value?

Re: Table manipulation (replacing a field)

Posted: Sat Jan 16, 2010 1:43 am
by Taehl
Try using:

Code: Select all

if key == 'w' then 
	for i, v in ipairs(move) do
		for j, w in ipairs(move[i]) do
			if w == 4 then
				move[i][j] = 3
				move[i+1][j] = 4
			end
		end
	end
end
Just changing w is only changing a local copy of the value in the table. With this, you actually change the table's value. For down you'd want move[i-1][j], left would be move[j-1], and right would be move[j+1] (assuming you don't rotate your table or something).

Re: Table manipulation (replacing a field)

Posted: Sat Jan 16, 2010 10:02 am
by osuf oboys
Typically you draw the character on top of the tilemap though, e.g. by keeping track of the player's tile x and y. This allows smoother movement and a fairly static and optimized tilemap.

Re: Table manipulation (replacing a field)

Posted: Sat Jan 16, 2010 10:22 am
by Robin
Taehl wrote:Just changing w is only changing a local copy of the value in the table. With this, you actually change the table's value. For down you'd want move[i-1][j], left would be move[j-1], and right would be move[j+1] (assuming you don't rotate your table or something).

Nope. Tables are modifiable in Lua, therefore

Code: Select all

t = {{}}
a = t[1]
a[1] = 4
print(t[1][1]) --prints 4
So my code and your modifications don't have any differences, except mine is slightly faster. (One local lookup instead of two table lookups).

Re: Table manipulation (replacing a field)

Posted: Sat Jan 16, 2010 11:23 am
by Taehl
Really? Nice! The more I learn about Lua, the more I love it (please excuse the pun).

Re: Table manipulation (replacing a field)

Posted: Mon Jan 18, 2010 2:32 pm
by dbltnk
Sadly, both solutions seem not to work for me. The fifth line of the code (v[j] = 3) deletes the character just fine, but when using Robin's solution (v[j+1] = 4), the game just freezes and when I use Taehl's (move[i+1][j] = 4) I get an error ("attempt to index filed '?' (a nil value)).

I tried to use random constructions like v[w+1] = 4 and v[i+1] = 4 and those really did move the character - but in places where I never wanted it to appear. :cry:

Re: Table manipulation (replacing a field)

Posted: Mon Jan 18, 2010 3:48 pm
by Robin
What is the value of j when it crashes?

Re: Table manipulation (replacing a field)

Posted: Mon Jan 18, 2010 7:00 pm
by dbltnk
I can't tell you. It freezes before the next operation (love.graphics.draw(v[j])) can be processed.