What exactly are Locals?

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.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

What exactly are Locals?

Post by Jasoco »

I can't seem to figure out what the benefits and limitations of Locals are. I tried Googling for a Lua site but it didn't really describe it helpfully.

What would the difference between these two be?

Code: Select all

variable = value

local variable = value
When should and shouldn't they be used?
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: What exactly are Locals?

Post by TechnoCat »

Sorry to reference you somewhere else, but...
http://www.lua.org/pil/4.2.html
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: What exactly are Locals?

Post by Jasoco »

Ah, that maks sense now. Thanks!
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: What exactly are Locals?

Post by TechnoCat »

I have a question now.
If I use

Code: Select all

foo=1
while foo do
   local foo = foo
   -- code goes here
end
Is there a way to modify the global variable in the same scope after I override a global with a local variable?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: What exactly are Locals?

Post by Robin »

use _G.foo
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: What exactly are Locals?

Post by bartbes »

or using a dynamic name (which is surprisingly useful):

Code: Select all

n = "foo"
_G[n] = "bar"
User avatar
Zorbatron
Citizen
Posts: 78
Joined: Wed May 27, 2009 6:58 pm

Re: What exactly are Locals?

Post by Zorbatron »

Man, how can you use lua and not know what local variable is XD?

Usually you only use local variables when you only need to store something temporarily. If you always use globals, not only will you be cluttering the global namespace (meaning there will be a lot of variable name conflicts), you are also using more memory than you should be.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: What exactly are Locals?

Post by Jasoco »

I never heard of Lua before I found Löve.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: What exactly are Locals?

Post by Robin »

Zorbatron wrote:Usually you only use local variables when you only need to store something temporarily. If you always use globals, not only will you be cluttering the global namespace (meaning there will be a lot of variable name conflicts), you are also using more memory than you should be.
Not to mention the speed advantages of using locals.
For example,

Code: Select all

function afunc()
    local dothings = dothings
    dothings()
    dothings()
    dothings()
    dothings()
    dothings()
end
will be faster than

Code: Select all

function afunc()
    dothings()
    dothings()
    dothings()
    dothings()
    dothings()
end
Help us help you: attach a .love.
User avatar
Zorbatron
Citizen
Posts: 78
Joined: Wed May 27, 2009 6:58 pm

Re: What exactly are Locals?

Post by Zorbatron »

@Robin - Its faster because accessing globals requires lua to use the hash-lookup algorithm on the global table correct?
Post Reply

Who is online

Users browsing this forum: No registered users and 87 guests