Local Variable Availability - Multiple Callback Functions

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
User avatar
i_love_u
Prole
Posts: 28
Joined: Sat Nov 29, 2014 12:13 am

Local Variable Availability - Multiple Callback Functions

Post 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?
Goober
Prole
Posts: 6
Joined: Wed Mar 11, 2015 9:28 pm

Re: Local Variable Availability - Multiple Callback Function

Post 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.
User avatar
i_love_u
Prole
Posts: 28
Joined: Sat Nov 29, 2014 12:13 am

Re: Local Variable Availability - Multiple Callback Function

Post 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.
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Local Variable Availability - Multiple Callback Function

Post 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".
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Local Variable Availability - Multiple Callback Function

Post 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.
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: Local Variable Availability - Multiple Callback Function

Post 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
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Local Variable Availability - Multiple Callback Function

Post 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.
Lapin
Prole
Posts: 19
Joined: Fri Mar 20, 2015 11:53 am

Re: Local Variable Availability - Multiple Callback Function

Post 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)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Local Variable Availability - Multiple Callback Function

Post by Robin »

Lapin wrote:Or even more local ...
That really doesn't make a difference.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 51 guests