1

So I'm making my incremental game, Periodic Alchemy, and I'm trying to make something that ensures that a save keeps all the new variables in another version. In order to save, I take a variable, "player", which includes all my variables inside it, and make it into a Base64 string that you can copy.

In almost every new version, however, I add new variables. If a player uses their old save on a new version with new variables, then those new variables are lost and the new functions break. So when you load the game, the game checks to see if that old save_data contains certain variables, and if a variable is not included, the game adds that variable to the save data. I don't know, however, how to do that. Here is my "player" code:

var player = {
    upQuarkClicks: 0,
    electronClicks: 0,
    downQuarkClicks: 0,
    auto_up_quark_clicks: 0,
    auto_electron_clicks: 0,
    auto_down_quark_clicks: 0,
    auto_proton_clicks: 0,
    auto_neutron_clicks: 0,
    up_quark_click_cost: 15,
    electron_click_cost: 15,
    down_quark_click_cost: 15,
    proton_click_cost: 15,
    neutron_click_cost: 15,
    upgrade_up_quark_1_cost: 50,
    upgrade_electron_1_cost: 50,
    upgrade_down_quark_1_cost: 50,
    auto_up_quark_clicks_amount: 1,
    auto_electron_clicks_amount: 1,
    auto_down_quark_clicks_amount: 1,
    auto_proton_clicks_amount: 1,
    auto_neutron_clicks_amount: 1,
    upgrade_up_quark_click_1_bought: "False",
    upgrade_electron_click_1_bought: "False",
    upgrade_down_quark_click_1_bought: "False",
    proton_up_quark_cost: 2,
    proton_down_quark_cost: 1,
    neutron_up_quark_cost: 1,
    neutron_down_quark_cost: 2,
    protonClicks: 0,
    neutronClicks: 0,
    hydrogen2DeuteriumClicks: 0,
    hydrogen1ProtiumClicks: 0,
    hydrogen3TritiumClicks: 0,
    helium3Clicks: 0,
    helium4Clicks: 0,
    lithium6Clicks: 0,
    lithium7Clicks: 0
};

So, let's say I added a new variable: lithium_7_bought: "false". This new variable is not in the old save data, and so is erased by the load. How do I add it in if it is not found?

1 Answer 1

3

Something like this should work:

if (!("lithium_7_bought" in player))
{
    player.lithium_7_bought = "false";
}
Sign up to request clarification or add additional context in comments.

3 Comments

I never knew about the "in" operator - +1!
That works great! I had tried something like that, by removing upQuarkClicks from player and pressing the Create an Up Quark button. It didn't work because it uses ++ but I tried it again with different code and it worked. I also didn't know about the "in" operator. Thanks!
@JonathanSpirit Glad I could help. Yeah, it should work for any property. Just make sure the property has been added before you try to use it (including operators like ++).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.