player={} table initiation, sockmonkee dev

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.
Post Reply
effortlessartist
Prole
Posts: 2
Joined: Wed Sep 11, 2013 2:33 am

player={} table initiation, sockmonkee dev

Post 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!
User avatar
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

Post 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).
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: player={} table initiation, sockmonkee dev

Post 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
}
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: player={} table initiation, sockmonkee dev

Post 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)
effortlessartist
Prole
Posts: 2
Joined: Wed Sep 11, 2013 2:33 am

Re: player={} table initiation, sockmonkee dev

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: player={} table initiation, sockmonkee dev

Post 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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: player={} table initiation, sockmonkee dev

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

Re: player={} table initiation, sockmonkee dev

Post 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.
Help us help you: attach a .love.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: player={} table initiation, sockmonkee dev

Post by raidho36 »

Oh, yeah. I skipped through most of the Plu's post. TL;DR.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: player={} table initiation, sockmonkee dev

Post by bartbes »

Robin wrote: That's... the gist of Plu's post.
Exactly! (Nice save, me!)
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 0 guests