Page 1 of 1

Local Variable Availability - Multiple Callback Functions

Posted: Fri Mar 27, 2015 12:44 am
by i_love_u
I need to make a local variable from my "love.draw()" available in my "love.keypressed()". Is there a way to let it be accessed without making it a global / public class field?

Re: Local Variable Availability - Multiple Callback Function

Posted: Fri Mar 27, 2015 12:57 am
by Goober
Is there a reason you can't use a global variable? It's good to avoid using them when a local will do just fine, but you don't always need to avoid them.

Re: Local Variable Availability - Multiple Callback Function

Posted: Fri Mar 27, 2015 1:12 am
by i_love_u
Goober wrote:Is there a reason you can't use a global variable? It's good to avoid using them when a local will do just fine, but you don't always need to avoid them.
I am just new to programming and it was pretty much the first thing that I learned in class. Not to use global's, that is.

Re: Local Variable Availability - Multiple Callback Function

Posted: Fri Mar 27, 2015 1:27 am
by arampl
Depends. I've read some book on game development where the exact opposite rule is in charge: use global variables wherever you can. But with Lua it can be another story.

You know, there are many rules on programming. Never use "goto". Never use "do - while" construct. Etc. But it depends on context. Sometimes you can break rules.

Sometimes you should just leave the task with solution you have at the moment. Or you can stuck and drop project completely. Solution can always come later as a side effect of working on another task.

I can even name this rule as a golden rule of programmer: "Switch to another task if you stuck with the current".

Re: Local Variable Availability - Multiple Callback Function

Posted: Fri Mar 27, 2015 6:52 am
by micha
i_love_u wrote:I am just new to programming and it was pretty much the first thing that I learned in class. Not to use global's, that is.
It is a good rule of thumb to avoid globals, because globals tend to couple code that should not be coupled. More often than not, variables are only used locally and so it is a good idea to use local variables whenever possible. If, however, some data needs to be accessed globally, then there is no way around and that is reason enough to use a global variable.

Re: Local Variable Availability - Multiple Callback Function

Posted: Fri Mar 27, 2015 2:06 pm
by DaedalusYoung
Yeah, just declare the local outside any of the callbacks. Then it'll still be local to main.lua, not global, but can be accessed anywhere in that file.

Code: Select all

local var = 1

function love.draw()
    love.graphics.print(var)
end

function love.keypressed(k, r)
    var = var + 1
end

Re: Local Variable Availability - Multiple Callback Function

Posted: Fri Mar 27, 2015 2:34 pm
by Azhukar
In lua accessing a global variable is akin to indexing a table, that's why localizing any variable you will use at least twice in current function scope is faster.

In school they taught you to avoid globals because they wanted to teach you good programming habits, i.e. not writing hardcoded bullshit that you'll be unable to make heads or tails of the next week. Then they most likely tried teaching you java and negated any good habits you might have had.

Re: Local Variable Availability - Multiple Callback Function

Posted: Fri Mar 27, 2015 10:22 pm
by Lapin
DaedalusYoung wrote:Yeah, just declare the local outside any of the callbacks. Then it'll still be local to main.lua, not global, but can be accessed anywhere in that file.

Code: Select all

local var = 1

function love.draw()
    love.graphics.print(var)
end

function love.keypressed(k, r)
    var = var + 1
end

Or even more local ...

Code: Select all

do
local var = 1

function love.draw()
    love.graphics.print(var)
end

function love.keypressed(k, r)
    var = var + 1
end

end


Local variables are faster than global ones, this is why you can do some kind of easy optimisation like

local love = love

at the top of a file, it will copy the love table to a local one and all of your function in this file (in the love table) will run 20-30% faster (Well, this is what i'm doing with GLua and it works)

Re: Local Variable Availability - Multiple Callback Function

Posted: Mon Mar 30, 2015 11:46 am
by Robin
Lapin wrote:Or even more local ...
That really doesn't make a difference.