Hi everyone
I am using all local variables on my games and apps, but now i am using the director popUps to use like the pause and score menus. And think on this situation.
i have a runtime on my game, and imagine that you start the game and the runtime is activated and a timer is running somewhere on my code, both running on the same time, the runtime and the timer.
and then i touch on my pause button, it goes to the popUp menu, and pause the timer but the runtime still activated, and then i touch on a button inside of the popUp menu to get back to the game, i cant start again my timer because i am on the popUp and the variable of the timer is inside of the game, i really dont want to use global variables, but if i will use, will use only one, to resolve this problem with the popUp, because if i use the global variable, i can change the value of the global variable and see on my popUp and Game, and my runtime inside of the game will check every time my global variable, to check if need to active the timer or no. Can understand the logic? 
can it be dangerous to check every time my global variable? maybe need to consider using other things?
what advise me?it is really important
Thank you
[import]uid: 26056 topic_id: 22269 reply_id: 322269[/import]
Global values are not recommended but they are often a necessary evil. My current app has a few. I will work out ways to reduce that but sometimes you just have to put up with it
[import]uid: 10389 topic_id: 22269 reply_id: 88745[/import]
yeh, i dont want to use global variable but i dont have other option
but i only need to use one global variable, not more. is dangerous to check the value of my global variable inside of a runtime? i need to check this variable all time that my game is running.
should i use:
main.lua
_G.myVar
game.lua
print (_G.myVar)
or
main.lua
_G.myVar
game.lua
local variable = _G.myVar
print (variable)
if anyone have good information and maybe other options, tell something here, it is important for all the users
[import]uid: 26056 topic_id: 22269 reply_id: 88754[/import]
There’s no danger with global variables. They just use memory which they don’t free until you quit the app.
Technically you could use all the globals you want. It’s just not recommended for memory overhead issues, particularly if you have large tables full of data. [import]uid: 10389 topic_id: 22269 reply_id: 88760[/import]
Hi Blickon,
WauloK is correct, sometimes it cannot be helped, but there
is a alternative, the link below is really good…
http://blog.anscamobile.com/2011/02/using-closures-in-lua-to-avoid-global-variables-for-callbacks/
The links inside this article are also really good…
A little demo:
--\*\* a little demo
function newCounter ()
local i = 0;
return function () -- anonymous function
i = i + 1;
return i;
end
end
local function printIt()
c1 = newCounter();
print("1st Call to Counter: ", c1()); --\> 1
print("2nd Call to Counter: ",c1()); --\> 2
end
printIt();
Hope that helps…
The above read, is good tho…
Larry
Willow Road games [import]uid: 107633 topic_id: 22269 reply_id: 88761[/import]
i think that this solution cant help me, because i need to check every time one variable on my runtime. and i will need to check every time the external module, i try it using sqlite and it dont work, i think that i dont have other option without using a global variable, but i think that this global variable will not cause anything on my game, because i only need 1 global variable and the values for this variable will be only 0 or 1 or 2 or 3, i think that i can use this global variable, and it will be better than use modules or sqlite or other things, because it is really one only global variable with a simple number, right? what you think? [import]uid: 26056 topic_id: 22269 reply_id: 88895[/import]
Hey Blickon,
There may be different ways of doing this, depending on
how you have coded your popup and director modules, but
in this situation, a GLOBAL is one way of accomplishing
that task, using a global just one(1)…is pretty darn
good and I don’t thing you will have any problems at all
I have coded programs where there were many…many
global variables and it didn’t affect the program at all
as long as you control and watch these carefully…
It sounds like you pretty much have a handle on watching
and looking at your 1 global variable …so NO worries
Go ahead and use it, if it works for ya, at some later
date you may find a better way to change, look at, or
use your global…variable so as not to worry about it…
Good luck, and press onward to finish your project…
“May all variables smile up at you!”…
Happy programming!
Best Regards,
Larry
willow Road games
[import]uid: 107633 topic_id: 22269 reply_id: 89137[/import]
Hi sharp1959, than a lot for your comment and help, i finished the code with the global variable and it works nice on simulator, dont affect my memory, i only use one global variable and it changes between 0 and 6, it is for 6 different things i change the variable and it works great the runtime detect all i do. But i dont know how it will work on a device, lather i will try and see the results, thanks a lot people
[import]uid: 26056 topic_id: 22269 reply_id: 89144[/import]
This is the best blog for anyone who needs to search out out about this topic. You understand a lot its virtually onerous to argue with you (not that I actually would needÂ?HaHa). You definitely put a new spin on a topic thats been written about for years. Great stuff, just nice!
Does hgh affect free testosterone and dht levels parrakeet HGH unfortunate Penis Enlargement hyphomycoses [import]uid: 136225 topic_id: 22269 reply_id: 95697[/import]