New here but probably a stupid question

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
sinsiter
Prole
Posts: 3
Joined: Sat Jul 29, 2017 7:19 am

New here but probably a stupid question

Post 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.
Skeletonxf
Citizen
Posts: 87
Joined: Tue Dec 30, 2014 6:07 pm

Re: New here but probably a stupid question

Post 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
Last edited by Skeletonxf on Sun Jul 30, 2017 3:26 pm, edited 1 time in total.
User avatar
Мэтю
Prole
Posts: 32
Joined: Mon Jan 06, 2014 1:24 pm
Location: Espírito Santo, Brazil
Contact:

Re: New here but probably a stupid question

Post 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.
World needs love.
sinsiter
Prole
Posts: 3
Joined: Sat Jul 29, 2017 7:19 am

Re: New here but probably a stupid question

Post by sinsiter »

Ok thank guys!
sinsiter
Prole
Posts: 3
Joined: Sat Jul 29, 2017 7:19 am

Re: New here but probably a stupid question

Post 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
User avatar
IsawU
Prole
Posts: 16
Joined: Sat May 02, 2015 6:26 pm

Re: New here but probably a stupid question

Post 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).
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: New here but probably a stupid question

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: New here but probably a stupid question

Post 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,
}
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Bing [Bot] and 7 guests