Page 1 of 1

New here but probably a stupid question

Posted: Sat Jul 29, 2017 1:09 pm
by sinsiter
Hi guys New I'm New tonthisbso please bare with me lol. But it's more a lua question. So if I made a table like

Code: Select all

player = {} 
Player.x = 0
Player.y = 0
Player.speed = 100
Would all this get stores in the player table even though it has a .x or .y? When I was learning tables I could have sworn that to store something into the table it would have to use the same name like.. player = whatever . Sorry for the horrible question.

Re: New here but probably a stupid question

Posted: Sat Jul 29, 2017 2:30 pm
by Skeletonxf
that code is equivalent to

Code: Select all

player = {
  x = 0, 
  y = 0, 
  speed = 100
}
assuming you mean to be using

Code: Select all

player
as you haven't defined a table called

Code: Select all

Player
Edit: Managed to merge the two correct table construction syntaxes into one incorrect version as has now been pointed out

Re: New here but probably a stupid question

Posted: Sat Jul 29, 2017 2:34 pm
by Мэтю
Actually, that could would raise and error since lua is case sensitive, so player differs from Player. So, the dot refer to an object, which you want to store x, y and speed, but as I said before it'll raise an error as the object "Player" doesn't exists because you haven't declared it yet.

Re: New here but probably a stupid question

Posted: Sat Jul 29, 2017 4:15 pm
by sinsiter
Ok thank guys!

Re: New here but probably a stupid question

Posted: Sat Jul 29, 2017 7:12 pm
by sinsiter
Another question. What's the difference between the two if they do the same thing?
Skeletonxf wrote: Sat Jul 29, 2017 2:30 pm that code is equivalent to

Code: Select all

player = {
  "x" = 0, 
  "y" = 0, 
  "speed" = 100
}
assuming you mean to be using

Code: Select all

player
as you haven't defined a table called

Code: Select all

Player

Re: New here but probably a stupid question

Posted: Sun Jul 30, 2017 7:54 am
by IsawU
sinsiter wrote: Sat Jul 29, 2017 7:12 pm Another question. What's the difference between the two if they do the same thing?
The difference is almost none, sometimes you will find using .x = 0, .y = 0, .speed = 100 (easier to read), other times you will enjoy using {x = 0, y = 0, speed = 100} (initializing the table in one line of code).

P.S. At least in Lua 5.3 it should be {x = 0} not {"x" = 0} (which throws an error).

Re: New here but probably a stupid question

Posted: Sun Jul 30, 2017 8:55 am
by zorg
sinsiter wrote: Sat Jul 29, 2017 7:12 pm Another question. What's the difference between the two if they do the same thing?

Code: Select all

a = {
	b = 5,
	c = b -- or even a.b
}

-- The above code doesn't do what you expect it to (set a.c to 5 as well), as how the below code does it

a = {}
a.b = 5
a.c = a.b

Re: New here but probably a stupid question

Posted: Sun Jul 30, 2017 9:31 am
by bartbes
IsawU wrote: Sun Jul 30, 2017 7:54 am P.S. At least in Lua 5.3 it should be {x = 0} not {"x" = 0} (which throws an error).
Yeah it should be

Code: Select all

player = {
    x = 0,
    y = 0,
    speed = 100
}
or

Code: Select all

player = {
    ["x"] = 0,
    ["y"] = 0,
    ["speed"] = 100,
}