question about local and global [SOLVED]

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
test
Prole
Posts: 28
Joined: Sun Apr 14, 2019 2:36 pm

question about local and global [SOLVED]

Post by test » Mon Apr 29, 2019 4:26 am

Whats the difference between the codes above?

Code: Select all

function love.load()
x = 10
end

Code: Select all

local x
function love.load()
x = 10
end
In the second code, x is local. But in the first code, is it global or local? I think both codes is the same.
Last edited by test on Tue May 07, 2019 7:08 pm, edited 1 time in total.

User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: question about local and global

Post by raidho36 » Mon Apr 29, 2019 5:25 am

The difference is technical: global variables are stored in a globally accessible table (it's called _G and you can access it manually), and local variables are stored in Lua VM registers. For that reason, globals are accessible everywhere, but locals only accessible within the scope they were defined (file, function, block of code, etc). For the same reason, locals are faster, and are limited to 200 at a time in any given scope.

User avatar
ivan
Party member
Posts: 1560
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: question about local and global

Post by ivan » Mon Apr 29, 2019 5:34 am

Put simply: in the second example the 'x' variable cannot be accessed outside of the file because it's local.

test
Prole
Posts: 28
Joined: Sun Apr 14, 2019 2:36 pm

Re: question about local and global

Post by test » Mon Apr 29, 2019 6:25 am

So x in the first code is global right?

User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: question about local and global

Post by arampl » Mon Apr 29, 2019 6:32 am

Yes. Variables in Lua are global by default.

User avatar
zorg
Party member
Posts: 2909
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: question about local and global

Post by zorg » Mon Apr 29, 2019 6:54 am

Global to the lua state, anyway (with löve, it may matter, since you can have multiple synchronous threads that have their own lua states), but the globals are really just keys in a table that's, by default, as raidho already said.
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
pgimeno
Party member
Posts: 2168
Joined: Sun Oct 18, 2015 2:58 pm

Re: question about local and global

Post by pgimeno » Mon Apr 29, 2019 1:13 pm

Here's how you can tell the difference between a global and a local.

main.lua:

Code: Select all

function love.load()
  x = 10
  require 'extra'
end
extra.lua:

Code: Select all

print(x)
That will print 10, because x is a global, therefore it's accessible from every file in the current thread.

Now if you add this at the top of main.lua:

Code: Select all

local x
it will print 'nil' because the second file is accessing the global variable x, which does not exist, because x is local to main.lua, and there isn't a global with that name.

Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests