Local Variable Availability - Multiple Callback Functions
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Local Variable Availability - Multiple Callback Functions
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
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
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.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.
Re: Local Variable Availability - Multiple Callback Function
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".
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
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.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.
Check out my blog on gamedev
- DaedalusYoung
- Party member
- Posts: 413
- Joined: Sun Jul 14, 2013 8:04 pm
Re: Local Variable Availability - Multiple Callback Function
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
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.
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
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)
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Local Variable Availability - Multiple Callback Function
That really doesn't make a difference.Lapin wrote:Or even more local ...
Help us help you: attach a .love.
Who is online
Users browsing this forum: Amazon [Bot] and 8 guests