How to save the value of a variable?

Hey Everyone,

My question is: How do you save the value of a variable, so when you reference it later on the old value shows up. For example here is my situation: In each level players can collect coins, but in the next level in my game, the coins are reset to 0. How do I save the value of the coins they got from each level, to display the total in my upgrade store? Thanks for your help! [import]uid: 175550 topic_id: 31502 reply_id: 331502[/import]

Hey there,

as far as I understand you’re looking for this:

make a data.lua file and place that in there:

  
local M = {  
  
var1 = 'test',  
var2 = '123'  
}  
  
return M  
  

later on you can access these values from everywhere by doing this:

local data = require ( "data" )   
  
local getvar = data.var1  
print( getvar )  
  
data.var2 = 'hello'  
print( data.var2 )  
  

I hope that helps… [import]uid: 140000 topic_id: 31502 reply_id: 125870[/import]

You can send ure “coins” variable as parametar to new scene / level.

Example for Direcor class:
http://developer.coronalabs.com/forum/2012/07/31/sending-variables-new-scenes-director
But romancfischer’s class approach is probably better. [import]uid: 177091 topic_id: 31502 reply_id: 125874[/import]

You could always save it as a global variable ie _G.coins=xx

Apologies, I don’t have a suitable example to hand as I’m on my iPad, but if you search for global variables you’ll find plenty of examples.

MrC [import]uid: 45444 topic_id: 31502 reply_id: 125913[/import]

Assuming you want to save the values so they carry over when the app is relaunched etc and not just through scenes you should check out Ego, it actually has an upgrade store example; http://corona.techority.com/2012/04/08/making-an-upgrade-store-in-your-coronasdk-app/

Peach :slight_smile: [import]uid: 52491 topic_id: 31502 reply_id: 125943[/import]

Hey there,

as far as I understand you’re looking for this:

make a data.lua file and place that in there:

  
local M = {  
  
var1 = 'test',  
var2 = '123'  
}  
  
return M  
  

later on you can access these values from everywhere by doing this:

local data = require ( "data" )   
  
local getvar = data.var1  
print( getvar )  
  
data.var2 = 'hello'  
print( data.var2 )  
  

I hope that helps… [import]uid: 140000 topic_id: 31502 reply_id: 125870[/import]

You can send ure “coins” variable as parametar to new scene / level.

Example for Direcor class:
http://developer.coronalabs.com/forum/2012/07/31/sending-variables-new-scenes-director
But romancfischer’s class approach is probably better. [import]uid: 177091 topic_id: 31502 reply_id: 125874[/import]

You could always save it as a global variable ie _G.coins=xx

Apologies, I don’t have a suitable example to hand as I’m on my iPad, but if you search for global variables you’ll find plenty of examples.

MrC [import]uid: 45444 topic_id: 31502 reply_id: 125913[/import]

Assuming you want to save the values so they carry over when the app is relaunched etc and not just through scenes you should check out Ego, it actually has an upgrade store example; http://corona.techority.com/2012/04/08/making-an-upgrade-store-in-your-coronasdk-app/

Peach :slight_smile: [import]uid: 52491 topic_id: 31502 reply_id: 125943[/import]