Page 1 of 1
player={} table initiation, sockmonkee dev
Posted: Wed Sep 11, 2013 2:42 am
by effortlessartist
So I'm watching sockmonkee's video's on youtube and he keeps using this:
player = {}
player.x = 5
player.y = 200
player.xvel = 5
Why does he initiate a table and then create variables/methods based on the table? I don't know what this is called in programming or what to search google for to get a why for this. I'm not entirely new to programming, but this is something I haven't seen before (at least in this form) and I don't understand exactly what's going on here. The only thing that makes sense is that he's making it kind of like:
player = {["x"]=5, ["y"]=200, ["xvel"] = 5} <-- key value pairs.
is this essentially what he's doing or is there a better explanation?
Thanks!
Re: player={} table initiation, sockmonkee dev
Posted: Wed Sep 11, 2013 3:33 am
by ejmr
effortlessartist wrote:is this essentially what he's doing or is there a better explanation?
No, you pretty much nailed it. It is because of this:
Code: Select all
-- For the sake of example let's say we never bother to
-- write 'player = {}' anywhere in scope. So Lua knows nothing about it.
player.x = 10 -- And so this is a fatal error.
It is common to create an empty table before adding properties to it for that reason, and because people oftem find it more readable than to do it all at once entirely in the table constructor (i.e. your second example).
Re: player={} table initiation, sockmonkee dev
Posted: Wed Sep 11, 2013 6:44 am
by raidho36
Performance-wise though, the second example is better, because Lua knows in beforehand what the table needs to be and allocates space for it properly, only once. The first example though will trigger table re-hashing several times, which is a slow operation, and will most likely end it up with extra unused yet occupied memory. You should only resort to dynamic table extension if you can't know in advance just how much you gonna do that, e.g. if you do it in a loop that depends on some volatile variable.
You can still use multiline notation in static table creation:
Code: Select all
player = { x = 10,
y = 20,
z = 30
}
Re: player={} table initiation, sockmonkee dev
Posted: Wed Sep 11, 2013 7:24 am
by Plu
As an additional potential argument, when you have properties relying on existing arguments in the table, you cannot add them in one go.
This doesn't work:
Code: Select all
enemy = {
x = math.random( 1, 100 ),
y = math.random( 1, 100 ),
goalX = x + 50
goalY = y + 50
}
And this looks a little weird (to me, at least):
Code: Select all
enemy = {
x = math.random( 1, 100 ),
y = math.random( 1, 100 ),
}
enemy.goalX = enemy.x + 50
enemy.goalY = enemy.y + 50
So most people either resort to this:
Code: Select all
enemy = {}
enemy.x = math.random( 1, 100 )
enemy.y = math.random( 1, 100 )
enemy.goalX = enemy.x + 50
enemy.goalY = enemy.y + 50
Or this:
Code: Select all
local x = math.random( 1, 100 )
local y = math.random( 1, 100 )
enemy = {
x = x,
y = y,
goalX = x + 50,
goalY = y + 50
}
But that's mostly personal preference. (Except the first one, which doesn't work)
Re: player={} table initiation, sockmonkee dev
Posted: Wed Sep 11, 2013 2:08 pm
by effortlessartist
I think the reason the first one doesn't work is because (or at least you do this in javascript):
Code: Select all
enemy = {
x = math.random( 1, 100 ),
y = math.random( 1, 100 ),
goalX = x + 50
goalY = y + 50
}
Should be:
(Not sure if this.x is the right thing, but maybe theres something with a similar use in lua)
Code: Select all
enemy = {
x = math.random( 1, 100 ),
y = math.random( 1, 100 ),
goalX = this.x + 50,
goalY = this.y + 50
}
Could be wrong though. I only read one article about learning lua in 30 minutes. I pretty much know nothing about lua, but I work with perl, python, bash, and mysql all day.
Re: player={} table initiation, sockmonkee dev
Posted: Wed Sep 11, 2013 2:10 pm
by bartbes
effortlessartist wrote:
Could be wrong though.
You are, 'this' does not exist in lua. But you do point out a flaw, you can't refer to other members in the (at that point anonymous) table while defining it.
Re: player={} table initiation, sockmonkee dev
Posted: Wed Sep 11, 2013 2:14 pm
by raidho36
Is having a couple of more variables is so much of a trouble?
Code: Select all
local x, y = math.random ( 1, 100 ), math.random ( 1, 100 )
player = { x = x, y = y, goalx = x + 50, goaly = y + 50 }
Re: player={} table initiation, sockmonkee dev
Posted: Wed Sep 11, 2013 3:07 pm
by Robin
bartbes wrote:you can't refer to other members in the (at that point anonymous) table while defining it.
raidho36 wrote:Is having a couple of more variables is so much of a trouble?
That's... the gist of Plu's post.
Re: player={} table initiation, sockmonkee dev
Posted: Wed Sep 11, 2013 3:43 pm
by raidho36
Oh, yeah. I skipped through most of the Plu's post. TL;DR.
Re: player={} table initiation, sockmonkee dev
Posted: Wed Sep 11, 2013 5:22 pm
by bartbes
Robin wrote:
That's... the gist of Plu's post.
Exactly! (Nice save, me!)