## Local variables and global variables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
KayleMaster
Party member
Posts: 223
Joined: Mon Aug 29, 2016 8:51 am

### Local variables and global variables

I often notice when somebody asks a question for love2d and supplies code, the one who answers that with the new code always changes the global variables to local.
For example:

Code: Select all

function love.load()

--Version, title and window information
version = 0.001;
width, height, flags = love.window.getMode();
view_xview = 0;
view_yview = 0;
mx = 0;
my = 0;
end

to

Code: Select all

function love.load()
--Version, title and window information
local version = 0.001;
local width, height, flags = love.window.getMode();
local view_xview = 0;
local view_yview = 0;
local mx = 0;
local my = 0;
end

I know that using locals are faster than globals but are they really that fast that it is worth doing this?
Also if I have a variable in another file that I want to use in main.lua or the other way around, by declaring local in this way, would it be possible to access it? It says it limits the scope of the variable only by the block it's placed in, but since it's not placed in a block would that work?
I'm new to this stuff.
Last edited by KayleMaster on Mon Apr 09, 2018 2:50 pm, edited 1 time in total.

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

### Re: Local variables and global variables

It's not just about speed. Or, i should say, it's more about avoiding littering the "global environment" than any minuscule speed gains you might get.

Let me give you a horrible analogy first: Let's say i want to buy groceries in the store. It's both faster and more concise if i take a list of the stuff i need with me, instead of calling back home each time i enter a new aisle (or worse, for each item the shop might sell).

Digressing from that, here's some other answers:
In Lua, a file is a block too, so that's another scope, the file scope.
When you use local x in your main.lua's file scope, for example, it means that those are only visible from within that file. (Sometimes you want to put your locals there, instead of inside a function block, since you might want the variable to be local AND to be used in more than one function in said file; see my example below for what i mean by this.

Going back to my bad analogy, here's how you'd access one file's stuff from another:

Code: Select all

-- a.lua
local t = {}

-- fill up t with stuff

return t

-- main.lua (as example)

local foo -- this could be inside love.load as well ,but this way, whatever foo will be later will be accessible by any function that's in this file, instead of just love.load.

foo = require 'a' -- returns the table we return from a.lua you can return a variable, or anything really.
end

The table "t" in a.lua exists only inside there, but we require it in main.lua since we want to use it there as well.

In the end, it's about being explicit about access across files, instead of using globals implicitly. And being implicit could lead you to modifying variables you used elsewhere, because you can't easily see what you defined (and used) where. The way i wrote it, you can exactly see what you use where, so you minimize these kinds of errors.
Me and my stuff True 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.

KayleMaster
Party member
Posts: 223
Joined: Mon Aug 29, 2016 8:51 am

### Re: Local variables and global variables

So If I put return varname in my second file, it's scope will also be extended to the file that called that file? (in this case main.lua)
Why are we initializing variables just there and not in love.load() as it is said in love2d beginner tutorial?
If I do local varname in love.load, does it mean I can only access it there?

Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

### Re: Local variables and global variables

KayleMaster wrote:So If I put return varname in my second file, it's scope will also be extended to the file that called that file? (in this case main.lua)
Why are we initializing variables just there and not in love.load() as it is said in love2d beginner tutorial?
If I do local varname in love.load, does it mean I can only access it there?
Not exactly a professional here, but to answer you first question, it's most likely to keep main.lua clean.
For your second question, yes. If you have a local var in a function, you can only access it from there.

ivan
Party member
Posts: 1619
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: Local variables and global variables

Another practical advantage of using locals:

Code: Select all

for i = 1, #objects do
objects[i]['position'].x = objects[i]['position'].x + 1
objects[i]['position'].y = objects[i]['position'].y + 1
end
is that locals can often make your code shorter:

Code: Select all

for i = 1, #objects do
local p = objects[i]['position']
p.x = p.x + 1
p.y = p.y + 1
end
having to scroll the code horizontally is just annoying,
yet a lot of people are tempted to stuff as much as possible in a single line.

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

### Re: Local variables and global variables

Do note that... without going into it too much, you can basically only have one return in a file scope, so you can't return more than one thing. It will error if you try to do that.
Also, return can only return one parameter, not more.
(By default, i mean; there are ways to get around both "limitations", with using an if-then-else (a conditional), and to return a function, that when called, unpacks whatever you put in it, respectively)

A beginner tutorial might start you out with simpler ways to do things, just so one can understand concepts more easily. Doesn't mean the tutorial uses best coding practices. (Not that what i've shown you is applicable in every circumstance)
Me and my stuff True 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.

KayleMaster
Party member
Posts: 223
Joined: Mon Aug 29, 2016 8:51 am

### Re: Local variables and global variables

Maybe it's better to keep my game in one file mostly then :/

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

### Re: Local variables and global variables

That's a good idea for beginners, yes. After you learned how lua and löve works, you will be able to do anything*.
Me and my stuff True 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.

KayleMaster
Party member
Posts: 223
Joined: Mon Aug 29, 2016 8:51 am

### Re: Local variables and global variables

Well, I'm not really a beginner, I came from GM:S so I know my way around.
The problem is I'm working on a large-scale game (huh, I've wondered if this is the hundredth time you heard this) so I'd really prefer the multiple files convenience. I'd guess I would use globals after all. (for some stuff)

Positive07
Party member
Posts: 1006
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

### Re: Local variables and global variables

He means beginning in Lua. The best way to code in Lua is with the concept of modules, each Lua file is it's own environment and doesn't access variables from any other file, it doesn't use globals so everything is local, and returns a table with the methods and data it exposes. To access data and methods from another file you simply require it. The only problem you should look out for is circular recursion with requires that is file A requires file B which requires file A...

If you can you should read about it, there are many articles about it on the web
If you are interested I can gather a few links and post them here for reference, I just don't have the time right now
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)

### Who is online

Users browsing this forum: No registered users and 46 guests