Storing variables between screens (Optimization)

I’m building a turn-based game using Director to move between different control screens in my game. There’s a main view with several sub-screens that the player needs to go back and forth from to change settings. I need the variables to be stored when the player leaves the sub-screen and then comes back, so what’s the best way to do this? Right now I’m using a global for money, since I need that in every screen, and I’m planning to use a global table with each of the values stored away. Here’s a piece of the code:

subScreen1\_tb = {}  
local sub1 = subScreen1\_tb  
  
local sub1.value = 5  
local sub1.value2 = 26  

Since this is a turn-based game, my main priority is to make sure I’m building this in an efficient way once I scale it up to include more sub-screens and variables.

Thanks! [import]uid: 45104 topic_id: 12736 reply_id: 312736[/import]

Wow! @Andreas! I’m sorry I let this sit for so long! I ended up wandering around reading and trying several different ideas, but I finally took your advice! Works like a charm! Thank you!

http://www.youtube.com/watch?v=76RrdwElnTU

I setup a simple sandbox to test everything out so I’m going to incorporate it into my full app now. Thanks again for your help! This makes a lot more sense than Property Bag and some of the other ideas I was trying.

Cheers,
Ben [import]uid: 45104 topic_id: 12736 reply_id: 49045[/import]

I structure my apps in such a way that the sub-screens must use “getters” and “setters” in order to manipulate global data.

What this does is it allows you to perform special actions when a specific value (such as money) is grabbed (“getter”) or altered (“setter”).

Here is the basic setup of this using a table.

Note that this code is located in your “main.lua” file.

[lua]local GameSettings =
{
[“Name”] = “Andreas”,
[“Money”] = 5000,
[“Health”] = 100
}
function getGameSetting ( sSetting )

– Get the value
local valSetting = GameSettings[sSetting]


– OPTIONAL: value-specific function example

– if ( sSetting == “Health” ) then
– print ( “Health value was accessed!” )
– end

return valSetting

end

function setGameSetting ( sSetting, valValue )

– OPTIONAL: Get the previous value
– local valPrevious = GameSettings[sSetting]

– Set to the new value
GameSettings[sSetting] = valValue


– OPTIONAL: value-specific function example

– if ( sSetting == “Health” ) then
– if( valValue < valPrevious ) then
– print ( “Player was hurt!” )
– end
– end

return

end[/lua]

With those functions in “main.lua” you can now “get” and “set” global values from all sub-screens with the following code:

[lua]-- Get money
local nMoney = getGameSetting ( “Money” )

– Set health to 0
setGameSetting ( “Health”, 0 )[/lua]

Regards,

Andreas Ricci
NuPlay Entertainment
Founder & Lead Developer

[import]uid: 7366 topic_id: 12736 reply_id: 46759[/import]

Not a problem.

Always a pleasure to help a fellow coder!

Regards,

Andreas Ricci
NuPlay Entertainment
Founder & Lead Developer

[import]uid: 7366 topic_id: 12736 reply_id: 53310[/import]