Question

I'm a little doubt, I think it is easy to solve, but I do not know ...

I'm needing to save the value of a scene to another, in a game.

For example: the player is playing, and the money variable increases, this value would stay for all scenes, which is what I want. However its value is supposedly being wiped when I enter the main menu, as if it has never existed...

Can someone tell me how to set value for all my scenes?

Was it helpful?

Solution

Are you declaring your variable as a local? For example, is the beginning of your function similar to this?:

local gamescore = score;

If so, you'll need to remove the local, like so:

gamescore = score;

This is because the local definition only declares variables in the local block of code that it resides (i.e, it cannot be called anywhere else). For example, if I had this function:

function = Test(score)
    local sc = 0+score;
    return sc;
end

The variable sc wouldn't be available anywhere else, apart from inside this function, whilst:

function = Test(score)
    sc = 0+score;
    return sc;
end

this sc would, as it is not local to that specific function.

Don't use local if you want your variable to be used elsewhere, but be careful that you don't overwrite it in some other function, it's easy to do, as I've done it myself a few times.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top