Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Corona SDK variable throughout different scenes

Tags:

coronasdk

I would like to know how to manage variables in corona throughout different scenes. In my game score variable is in every scene what i want to know is how to continuously add the score per scene, because every time the scene changes the score automatically becomes zero.

like image 858
Nana Avatar asked Aug 13 '26 19:08

Nana


2 Answers

You can use a separate file. Example:

mydata.lua

With a table:

local M = {}
return M

-- Then in your game.lua you can simply require it:

local mydata = require( "mydata" )

and use for example a score:

mydata.score = 0

In your highscore.lua or whatever you can call it easy pzy:

local mydata = require( "mydata" )

and now you can use mydata.score saved value

like image 129
Chagano Avatar answered Aug 17 '26 06:08

Chagano


I hope Scenes are no more used . You can try the composer which is very effective and efficient .

http://docs.coronalabs.com/api/library/composer/index.html

It has two functions setVariable() and getVariable(). Using these functions you can pass your score from one scene to other .

since using of global variable may lead to memory leak, its better not use the global variabes

like image 24
Kumar KS Avatar answered Aug 17 '26 04:08

Kumar KS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!