multidimensional tables/arrays

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Kevin Tillman
Prole
Posts: 14
Joined: Sun Nov 22, 2009 12:42 am

multidimensional tables/arrays

Post by Kevin Tillman »

Multidimensional tables/arrays..

Hi, I am trying to grasp the concept of multidimensional tables, so I came up with something to help me understand this better but I dont know if I am on the right track or not.
What I did was make a table called pistols that have 2 guns each with their own tables with values for power, bullet capacity etc.

Now, other questions that I have beside the main question of me being on the right track are...
1. Say I make an empty table for the inventory for my character to hold the guns, and lets say I want to add the revolver from the table called pistols and place it in my characters inventory, will the attributes power and capacity carry over as well since they are in a sub table of "revolver"?
2. And if so, how would I set that up through code?

function load()

font = love.graphics.newFont(love.default_font, 20)
love.graphics.setFont(font)

pistols = {
nineMM = {power = 10, capacity = 24},
revolver = {power = 50, capacity = 6}}
end

function update()
end

function draw()
love.graphics.draw(pistols.revolver.power, 100, 100)
end
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: multidimensional tables/arrays

Post by bmelts »

It might go something like this:

Code: Select all

pistols = {
nineMM = {power = 10, capacity = 24},
revolver = {power = 50, capacity = 6}} 

inventory = {
  revolver = pistols.revolver
}
At which point inventory.revolver would be identical to pistols.revolver.

Not just identical, in fact, but the exact same table - any changes to one will also affect the other. So if the player puts a silencer on the revolver in their inventory, the revolver in the pistols table gets silenced as well. (Can you put a silencer on a revolver? I don't know guns.)

If you don't want that sort of behavior, and want each gun to be treated separately, you need to actively clone the gun. The easiest way is to write a table-cloning function, a simple version of which looks like this:

Code: Select all

function clone(t)
   local newT = {}
   for k,v in pairs(t) do
     newT[k] = v
   end
   return newT
end
This doesn't handle contingencies like the table you're cloning being multidimensional - I leave that as an exercise for you.
Kevin Tillman
Prole
Posts: 14
Joined: Sun Nov 22, 2009 12:42 am

Re: multidimensional tables/arrays

Post by Kevin Tillman »

Thank you anjo that has really helped me out. Also, I am going to buy myself a book on Lua programming, would anyone recommend this book

http://www.amazon.com/Programming-Lua-S ... 421&sr=8-1
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: multidimensional tables/arrays

Post by bmelts »

I highly recommend that book - the first edition is available free online (it's a bit out of date now, though), and was absolutely the most useful resource I had when learning Lua - I still use it as a reference sometimes, along with the official manual.
Kevin Tillman
Prole
Posts: 14
Joined: Sun Nov 22, 2009 12:42 am

Re: multidimensional tables/arrays

Post by Kevin Tillman »

Anjo, thanks again. I have ordered the book. And I have a question for you all....

What is the best approach to learn and be good at pogramming in Lua? My theory is that I should utilize trial and error combined with divide and conquor. Basically I should break every concept, every key term and definition down to its simplist form and learn everything there is to know about any given particualar thing through practice with trial and error until I fully understand it and then move on to the next. And once I come to something new I can repeat those steps again and incorporate that into what I have already learned previously to get a more better understanding of Lua as a whole as I continue to learn.

Because there is so much to learn, its not that its hard to learn, its just takes more time to grasp than most things. I believe hard is an illusionary word. This is how ive come to learn art and animation. I just took the time to learn it and didnt give up. www.tillmansart.com

Programming is deep, even with a more syntax friendly language like Lua, is this a good approach to help me learn Lua better? Thanks all..
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: multidimensional tables/arrays

Post by Robin »

Sure.
Help us help you: attach a .love.
User avatar
beatthelastboss
Citizen
Posts: 63
Joined: Wed Dec 09, 2009 10:50 pm
Location: Jacksonville, Florida, USA

Re: multidimensional tables/arrays

Post by beatthelastboss »

This was surprisingly useful, even for a newbie coder like me! Though I suggest a name change of this topic, as the title almost scared me away from even looking to see this useful content! Perhaps the term "Crossover Tables" would be more noob-friendly ;)
IN SOVIET RUSSIA, LUA CODES IN YOU!!
User avatar
dbltnk
Prole
Posts: 21
Joined: Thu Dec 10, 2009 8:23 pm

Re: multidimensional tables/arrays

Post by dbltnk »

I'm currently trying to understand 2d-tables as well and I've got a problem when referring to a field in the same table.

In line 5 I'm trying to set a variable depending on what the variable in line 4 is. I've tried r = 78*s, r = 78*circle.s and r = 78*object.circle.s but I always get the same error messages:

"attempt to index global 'objects' / 'circle' / 's' (a nil value)"

Code: Select all

circle = {	sprite = images.circle1, 
			x = math.random(50,350), 
			y = math.random(50,350), 
			s = math.random(50,150)/100,
			r = 78*objects.circle.s},
	box = {	sprite = images.box3, 
			x = math.random(50,350), 
			y = math.random(50,350), 
			s = math.random(50,150)/100,
			w = 1, 
			h = 1}
	}
Any ideas?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: multidimensional tables/arrays

Post by kikito »

You can't access circle.s yet because "circle" is undefined until the assignment of line 1 ends. In general, Lua variables need to be "initialized" via an assignment before you can use them.

Fortunately, there's a simple solution to your problem: End the statement, and then use circle.

You see, tables in lua are not "unmodifiable" after their initial creation. It is possible to create circle, and then add circle.r, like this:

Code: Select all

circle = {   sprite = images.circle1,
  x = math.random(50,350),
  y = math.random(50,350),
  s = math.random(50,150)/100, -- I've removed the r line from here to the end
  box = { ... --all the box stuff here
  }
} -- the statement ends here. Now I can do circle.s
circle.r = 78*objects.circle.s -- circle is now a defined variable, so I can use it ... to extend circle itself!
You can generalize this more. The following code is equivalent to the code above:

Code: Select all

circle = {} --you start with an empty table. this defines the circle variable, but there's nothing "inside" it.
circle.x = math.random(50,350) -- and then fill it up with "attributes"
circle.y = math.random(50,350) -- one step at a time
circle.s = math.random(50,150)/100 --this defines circle.s
circle.r = 78*objects.circle.s -- circle.s is defined, so you can do this here now. If you tried to execute this before defining circle.s, you would get an error.
circle.box = { sprite = images.box3, --You can define box like this
  x = math.random(50,350),
  y = math.random(50,350),
  s = math.random(50,150)/100,
  w = 1,
  h = 1
}
Notice that the same applies to circle.box! So the following code is also equivalent:

Code: Select all

circle = {} --you start with an empty table. this defines circle
... -- define x, y, s and r, as before
circle.box = {} --circle.box starts being an empty table
circle.box.sprite = images.box3 -- and you also fill it up in several lines
circle.box.x = math.random(50,350) -- like this
circle.box.y = math.random(50,350)
circle.box.s = math.random(50,150)/100
circle.box.w = 1
circle.box.h = 1
}
Keep in mind that you can mix-and match both styles: define some stuff on the initial assignment { x=..., y= ... } and modify/add attributes several lines later, once the variables are defined.

You can also add attributes depending on a condition (inside an if), and you can pass circle (or circle.box) to a function that adds even more attributes to it.

By the way, you can remove attributes from circle by equaling them to nil (circle.x=nil)
When I write def I mean function.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: multidimensional tables/arrays

Post by Robin »

One thing to add to kikito's excellent explanation:
You need to use circle.s, objects.circle.s won't work unless you have a table in objects, and a table in objects.circle.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: slime and 3 guests