player={} table initiation, sockmonkee dev
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 2
- Joined: Wed Sep 11, 2013 2:33 am
player={} table initiation, sockmonkee dev
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!
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!
- ejmr
- Party member
- Posts: 302
- Joined: Fri Jun 01, 2012 7:45 am
- Location: South Carolina, U.S.A.
- Contact:
Re: player={} table initiation, sockmonkee dev
No, you pretty much nailed it. It is because of this:effortlessartist wrote:is this essentially what he's doing or is there a better explanation?
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.
Re: player={} table initiation, sockmonkee dev
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:
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
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:
And this looks a little weird (to me, at least):
So most people either resort to this:
Or this:
But that's mostly personal preference. (Except the first one, which doesn't work)
This doesn't work:
Code: Select all
enemy = {
x = math.random( 1, 100 ),
y = math.random( 1, 100 ),
goalX = x + 50
goalY = y + 50
}
Code: Select all
enemy = {
x = math.random( 1, 100 ),
y = math.random( 1, 100 ),
}
enemy.goalX = enemy.x + 50
enemy.goalY = enemy.y + 50
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
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
}
-
- Prole
- Posts: 2
- Joined: Wed Sep 11, 2013 2:33 am
Re: player={} table initiation, sockmonkee dev
I think the reason the first one doesn't work is because (or at least you do this in javascript):
Should be:
(Not sure if this.x is the right thing, but maybe theres something with a similar use in lua)
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.
Code: Select all
enemy = {
x = math.random( 1, 100 ),
y = math.random( 1, 100 ),
goalX = x + 50
goalY = y + 50
}
(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
}
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: player={} table initiation, sockmonkee dev
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.effortlessartist wrote: Could be wrong though.
Re: player={} table initiation, sockmonkee dev
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 }
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: player={} table initiation, sockmonkee dev
bartbes wrote:you can't refer to other members in the (at that point anonymous) table while defining it.
That's... the gist of Plu's post.raidho36 wrote:Is having a couple of more variables is so much of a trouble?
Help us help you: attach a .love.
Re: player={} table initiation, sockmonkee dev
Oh, yeah. I skipped through most of the Plu's post. TL;DR.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: player={} table initiation, sockmonkee dev
Exactly! (Nice save, me!)Robin wrote: That's... the gist of Plu's post.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 0 guests