Using global variables with Director Class

Hi,
I’m all new to Corona(love it!), and slowly starting to learn.
I’m having a bit of problem with the Director Class(loving it though). I’ve searched the forums, but haven’t really found an solution for my problem;

-In my main.lua-file I’ve put a global variable called “monster_health” with an initial value of 100.

monster\_health = 100

-In the first “scene” of my program, there’s a button that, if you press it, adds 10 to “monster_health”.
Say you press that button two times, the value of “monster_health” 120. Everything works so far.

monster\_health = monster\_health + 10

-Then I try to move to my next “scene” and try to print out the “monster_health” variable in this scene, it goes back to my initial value of 100.

Have I missed something? Is there some other ways of keeping the information of a variable through “scenes”?
I’m sorry if it’s a stupid question, but I’m all new to this, and just can’t figure it out…
Thanks in advance! [import]uid: 11440 topic_id: 3934 reply_id: 303934[/import]

end you haven’t declared monster_health as local anywhere else? [import]uid: 9371 topic_id: 3934 reply_id: 11975[/import]

Yeah I’m pretty sure, I took out all other code of the program to see if that was the problem, but it still doesn’t work. This is all I have now, and the global variable still resets to 100:

main.lua - creates the global monster_health and loads mainscreen.lua

monster\_health = 100  
  
display.setStatusBar( display.HiddenStatusBar )  
  
-- Import director class  
local director = require("director")  
  
-- Create a main group  
local mainGroup = display.newGroup()  
  
-- Main function  
local function main()  
  
 -- Add the group from director class  
 mainGroup:insert(director.directorView)  
  
 -- Change scene without effects  
 director:changeScene("mainscreen")  
  
 return true  
end  
  
-- Begin  
main()  

mainscreen.lua - prints the variable and to buttons, one that adds 10 to monster_health and one who takes you to fight.lua

module(..., package.seeall)  
  
-- Main function - MUST return a display.newGroup()  
function new()  
 local localGroup = display.newGroup()  
  
 button\_xp = display.newImage( "button\_xp.png" )  
 localGroup:insert( button\_xp )  
 button\_xp.x = display.contentWidth - 74  
 button\_xp.y = 42  
  
 button\_fight = display.newImage( "button\_fight.png" )  
 localGroup:insert( button\_fight )  
 button\_fight.x = display.contentWidth - 74  
 button\_fight.y = 180  
  
-- skriver ut hp-mätaren  
 hpTextfield = display.newText( monster\_health, 10, 120, "Helvetica Bold", 12 )  
 hpTextfield:setTextColor( 0, 0, 255, 255 )  
 localGroup:insert( hpTextfield )  
  
 function button\_xp:tap( event )  
 monster\_health = monster\_health + 10  
 hpTextfield.text = monster\_health  
 end  
  
 function button\_fight:tap( event )  
 director:changeScene("fight","fade",255,255,255)  
 end  
  
 button\_xp:addEventListener( "tap", button\_xp )  
 button\_fight:addEventListener( "tap", button\_fight )  
 -- MUST return a display.newGroup()  
 return localGroup  
end  

fight.lua - prints monster_health. but always turn out “100”

module(..., package.seeall)  
  
-- Main function - MUST return a display.newGroup()  
function new()  
 local localGroup = display.newGroup()  
  
 monsterhpFight = monster\_health  
 monsterhpTextfield = display.newText( monsterhpFight, 40, 40, "Times", 36 )  
 monsterhpTextfield:setTextColor( 255, 0, 0, 255 )  
 localGroup:insert( monsterhpTextfield )  
 -- MUST return a display.newGroup()  
 return localGroup  
end  

Really can’t figure out what I’m doing wrong… [import]uid: 11440 topic_id: 3934 reply_id: 11995[/import]

@ringoringoringo: I am pretty sure, but not 100% certain yet, that when you put:

monster\_health = monster\_health + 10  

in your mainscreen.lua file, that you are creating a new ‘monster_health’ variable that is local to the mainscreen module.

All of your global variables are kept in a global _G table, and you can access them like this: _G[“yourvariable”]

So instead, in your mainscreen.lua file, try this:

\_G["monster\_health"] = \_G["monster\_health"] + 10  

and in your fight.lua module, try this:

monsterhpFight = \_G["monster\_health"]   

That SHOULD work the way you are expecting it to.
I am still learning though, and am not yet 100% clear on the way lua scopes variables and functions yet.

Joe [import]uid: 8444 topic_id: 3934 reply_id: 12000[/import]

Yihaa, it worked! Now I’ve learned something new today. Thank you very much for your help, Joe!
Now I can go back and continue to work on my game.

/Pär [import]uid: 11440 topic_id: 3934 reply_id: 12014[/import]

I believe you can also just use _G.monster_health rather than the quoted format
[import]uid: 6645 topic_id: 3934 reply_id: 12050[/import]

Ah, good to know! [import]uid: 8444 topic_id: 3934 reply_id: 12055[/import]

both haven’t worked with me for some reasons??

any ideas?
[import]uid: 11038 topic_id: 3934 reply_id: 13533[/import]

firemaple and jmp, you just saved my day! (Well, thats not ENTIRELY true, since it’s christmas and it would take more than corona issues to ruin it, but you definetely just made it awesome’er!) Thanks!!! [import]uid: 9175 topic_id: 3934 reply_id: 14821[/import]

Thanks for the tip on how to access global variables in other modules, this will be very useful. Overall I’m still going to avoid global variables as much as possible, ideally entirely, but this is good to know about. [import]uid: 12108 topic_id: 3934 reply_id: 14832[/import]

Thanks also for this.

On the same logic, always using director class, screen1, screen2, I try the following:

I want to pre-load different images in the main in different global variables, to have quick load of screen1 and screen2 when switching:


–in the main.lua
imageTest = display.newImageRect(“img1.png”, 320, 480)

–in screen1.lua
local b1= _G[“imageTest”] --or: b1= _G.imageTest
b1.x = 100; b1.y = 150
localGroup:insert(b1) --doesn’t works

The problem appears when switching back from screen2 to screen1. Inserting b1 in the localGroup of screen1 is the cause of crash. I would like b1 work, like if inserted in a localGroup. How to do that?
Thanks in advance for feedback on this. [import]uid: 8970 topic_id: 3934 reply_id: 14859[/import]

Guys, please write your questions at Director’s sub-forum. I don’t have too much time to answer and I really didn’t see most of the topics created for Director. I’m sorry for that and I will appreciate if you all could go to this link:

http://developer.anscamobile.com/forums/director-class [import]uid: 8556 topic_id: 3934 reply_id: 18692[/import]