Table manipulation (replacing a field)

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.
User avatar
dbltnk
Prole
Posts: 21
Joined: Thu Dec 10, 2009 8:23 pm

Table manipulation (replacing a field)

Post 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
	
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Table manipulation (replacing a field)

Post 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. ;)
Help us help you: attach a .love.
User avatar
dbltnk
Prole
Posts: 21
Joined: Thu Dec 10, 2009 8:23 pm

Re: Table manipulation (replacing a field)

Post 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?
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Table manipulation (replacing a field)

Post 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).
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

Re: Table manipulation (replacing a field)

Post 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.
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Table manipulation (replacing a field)

Post 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).
Help us help you: attach a .love.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Table manipulation (replacing a field)

Post by Taehl »

Really? Nice! The more I learn about Lua, the more I love it (please excuse the pun).
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
dbltnk
Prole
Posts: 21
Joined: Thu Dec 10, 2009 8:23 pm

Re: Table manipulation (replacing a field)

Post 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:
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Table manipulation (replacing a field)

Post by Robin »

What is the value of j when it crashes?
Help us help you: attach a .love.
User avatar
dbltnk
Prole
Posts: 21
Joined: Thu Dec 10, 2009 8:23 pm

Re: Table manipulation (replacing a field)

Post by dbltnk »

I can't tell you. It freezes before the next operation (love.graphics.draw(v[j])) can be processed.
Post Reply

Who is online

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