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 »

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 »

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: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: question about local and global

Post by ivan »

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 »

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 »

Yes. Variables in Lua are global by default.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: question about local and global

Post by zorg »

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: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: question about local and global

Post by pgimeno »

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 1 guest