[Solved] Simple question about lua variable declaration.

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
dusoft
Party member
Posts: 765
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Simple question about lua variable declaration.

Post by dusoft »

Another interesting thing is that you can have multiple locals with the same name in blocks like this:

Code: Select all

function xy(arg)
    local x,y=0,0
   if arg>5 then
       local x,y=5,5
       print(x,y)
   end
   print(x,y)
end
Just be careful about what values you expect from variables, sometimes one can get mixed up easily this way.

I have noticed that some people force local on everything include love functions in fashion like this:

Code: Select all

lg=love.graphics
lp=love.physics
-- etc.
I think those are unnecessary micro optimizations that also make your code less readable, but if you need that last percentile of performance, it might help.
RNavega
Party member
Posts: 457
Joined: Sun Aug 16, 2020 1:28 pm

Re: Simple question about lua variable declaration.

Post by RNavega »

qwdqwqwffqw wrote: Fri Feb 28, 2025 11:43 am
Always just the current block. If you set a local variable in global scope, it acts as implicitly global for that file (called chunk)
I can't believe I've been coding all this time in the way both ugly and inefficient, under my misconception. :crazy:
Just to be clear, and I don't have the link to it right now, but there was a mailing list post where the LuaJIT creator says that an upvalue has the same speed as an inner scope local.
So if you declare "local this_thing" at the top scope of a script, when you use this_thing it'll have the same speed as locals in inner scopes of that same script. It'll be visible everywhere within that script, kinda like a global, but a bit faster -- the speed cost of a global is that it's an implicit key search in table _G.
So if you want something to be visible everywhere within a single script, making it a top-level local in that script will give you some free speed improvement.
qwdqwqwffqw
Prole
Posts: 40
Joined: Sat Jan 25, 2020 4:11 pm

Re: Simple question about lua variable declaration.

Post by qwdqwqwffqw »

but if you need that last percentile of performance, it might help.
And maybe one can type much less in that way (also with less typos), if using text editor that does not support auto-completion.
LuaJIT creator says that an upvalue has the same speed as an inner scope local.
So if you want something to be visible everywhere within a single script, making it a top-level local in that script will give you some free speed improvement.
Oh, yes - this information is what I needed to know as well. So, within a module code file I can set some local variables for convenient coding as 'globals' for the whole scope of this file - which are still faster than real globals(in _G) in methods and loops, and won't pollute global table and effect other module files.
Post Reply

Who is online

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