Passing variables to another Scene??

Hi I am new to Lua and I just studied the “Ghosts vs. Monsters” sample game that Corona made. I have a question about passing a variable to another scene.

In the “Ghosts vs. Monsters” sample code, I suspect they use the “director:changeScene( “level1” )” to open another file. However, I would like to know if there is any way to pass a variable to another scene ?

should I use a global variable ?? how does global variable work in Lua?

Thx

[import]uid: 38556 topic_id: 10016 reply_id: 310016[/import]

Welcome!

When using director global variables do come in very handy in my experience :slight_smile:

A global variable in Corona is as simple as using “_G.” before your variable.

For example;

[lua]lives = 3[/lua]

Would become;

[lua]_G.lives = 3[/lua]

Make sense? :slight_smile: [import]uid: 52491 topic_id: 10016 reply_id: 36569[/import]

My suggestion is using a module as a central global data. Then when you change your scene using director (or your own system) you read data from that module.

e.g:

game.lua

module( ... , package.seeall )  
  
data =   
{  
 selectedLevel = 1,  
 mode = "easy"  
}  

main.lua

require("game")  
director:changeScane("anyscene")  

anyscene.lua

local selected = game.data.selectedLevel  
-- use that data  

[import]uid: 41267 topic_id: 10016 reply_id: 36570[/import]

Hi inas, I don’t believe we’ve met :slight_smile:

That’s an equally good suggestion and elabguy, if you are at the stage you are comfortable using that rather than a global variable then it’s certainly a nice clean way of doing things :slight_smile:

Peach [import]uid: 52491 topic_id: 10016 reply_id: 36572[/import]

@Peach
Hello Peach, i rarely posts on the forum before i become Corona subscriber. But now i guess i will lurk in the forum much more often :slight_smile: [import]uid: 41267 topic_id: 10016 reply_id: 36573[/import]

I hope you do; clearly you’ve got some good advice to share :slight_smile:

Peach [import]uid: 52491 topic_id: 10016 reply_id: 36641[/import]

Thanks all !!! [import]uid: 38556 topic_id: 10016 reply_id: 36732[/import]

inas, I just wanted to personally thank you for your suggestion on how to make the variables accessible (utilizing Director specifically) above. You resolved something I was wrestling with a couple hours yesterday. I owe you big! You rock.

Thanks! [import]uid: 74844 topic_id: 10016 reply_id: 49306[/import]

@jerome82:
You’re welcome, i’m glad i could help you. Have fun with Corona SDK :slight_smile: [import]uid: 41267 topic_id: 10016 reply_id: 49309[/import]

HI there… i did exactly as you wrote, word by word, line by line

result:

Runtime error
/Applications/CoronaSDK/testgame/main.lua:2: attempt to index global ‘director’ (a nil value)
stack traceback:
[C]: ?
/Applications/CoronaSDK/testgame/main.lua:2: in main chunk
Runtime error: /Applications/CoronaSDK/testgame/main.lua:2: attempt to index global ‘director’ (a nil value)
stack traceback:
[C]: ?
/Applications/CoronaSDK/testgame/main.lua:2: in main chunk

to be true, i don’t get the whole SCENE Concept.
Once i tested with that director.lua it loaded another .lua but looked as he would mix my first lua with the second.

can’t i just load another scene… my old comes completely closed/destroyed and the one i call opens up fresh and new?

thx
chris
[import]uid: 4795 topic_id: 10016 reply_id: 49317[/import]

guruk, there’s a director tutorial you might find useful here; http://techority.com/2010/11/19/how-to-use-scenesscreens-in-corona/

Once you get the concept of “scenes” it tends to get a lot easier from there :wink:

Peach [import]uid: 52491 topic_id: 10016 reply_id: 49439[/import]

Director already has functionality to pass parameters to another scene… there is no need to use globals.

(BTW, i’m using Director v1.3)

this example is taken from an app that i put together.

scene-menu.lua

[lua]local params = {
url = “http://github.com/”,
provenance = “screen-menu”
}
director:changeScene( params, “screen-webview”, “moveFromRight” )[/lua]

screen-webview.lua

[lua]new = function ( params )

local url = params.url
local provenance = params.provenance

end[/lua]

[import]uid: 74908 topic_id: 10016 reply_id: 56114[/import]