Add a scene variable value from the storage value and save that value to storage (FINALLY SOLVED!)

I’m currently trying to make it so that the player can add a value from a scene variable, add that number by the value in storage (or vice versa), then save the result into the storage. Here’s how it looks like:

The BiscCount is the number of biscuits collected by the cat in the game.

This code right here is supposed to take the BiscCollected group, store the value in BiscFromSave, add the value BiscFromSave with BiscCount, and store the new BiscCount variable in the BiscCollected group of the game storage whenever the player restarts a round, exits to the menu screen, or when the round is over.

When the player visits the “Achievements” page, the game is supposed to take the saved value from the BiscCollected group and store it in the TotalBiscs variable.

However, the TotalBiscs variable is returning a 0 instead of the saved variable. Can anyone explain what I’m doing wrong? I can use a global variable to get the total amount of biscuits collected by the player, but when I close and open the preview window, the amount of biscuits collected are gone. So I want to save the total amount of biscuits collected by the player into the storage.

I did something pretty similar to this not too long ago, the events looked like this:


Whenever the player hits the red border they die and the scene is reset, I have a counter variable global totalDeaths that is incremented by one every death (for that level, it is reset when a new level is selected). When the player hits the green finish line the previously saved totalDeaths that holds all of the player deaths collectively is loaded into a scene variable totalDeaths (this is different than the global variable totalDeaths that counts just the deaths on that specific level, convoluted I know but I did all this hastily). The totalDeaths scene variable (that holds all the players deaths across the game) is then incremented by however many times they died on that specific level (the global variable totalDeaths), the new value of totalDeaths is then saved again and the score is sent to the leaderboard. Finally the global variable totalDeaths is reset to 0, so the deaths can be counted again on the next level.

Is this similar to what you were trying to do?

Sort of. Here’s what I did recently:

I altered the code by adding a new variable “TotalBisc.” The variable takes BiscCount (the amount of biscuits collected in the round) and BiscfromSave (the total amount of biscuits already collected by the player in the game from the save file) and adds them up. The result is supposed to overwrite the total amount of biscuits collected in the storage.

I tested out the code by playing the game. I opened the debugger as well.

debug1

The game is able to get the amount of biscuits collected in the game.

debug2

The problem is the TotalBisc variable. It’s supposed to say 36 since 36 (BiscCount) + 0 (BiscFromSave) = 36, but it won’t change the variable for some weird reason.

I would check the variable spelling and make sure the the global/scene is consistent and matches.

The only thing that did seem concerning is that unless there are more events, it’s only saving to storage if storage already exists. It’s a bit of a paradox.

1 Like

Yo, thanks for helping me out! Your comment made me realize that I didn’t need the condition to check if a storage group exists. Here are the screenshots of the updated code and the results:

Sorry for the cutoff, but it’s supposed to say that I collected 5 biscuits.

achieve

Now the total amount of biscuits collected will always be saved in a storage file whenever a player quits or restarts a round, or when the round is finished. Once again, thanks for the help!

Edit: I think I’ll remove the “if the storage exists” condition for the other events where I have to load or save a data to a storage as well.

1 Like

Good to hear. Sometimes it takes forever to find a simple typo.

It’s good to check if storage exists before loading just not before saving. It’s effecent. No sense in running more events than necessary plus sometimes empty or default values can have unintentional side effects. It’s good to have both a storage exists and an [inverted] storage exists to handle both scenarios.

1 Like

I hope you don’t mind, but I decided to open this topic again, because I found another glitch. While I was playing one of the modes where you have to dodge the objects falling from the sky, I got this weird glitch where it doesn’t count the number of objects the player has gotten hit in a round when the round is over.

debug1

The TotalBadBisc variable is supposed to be 3 (BadBiscCount + BadBiscFromSave), but it’s giving me a 0 instead.

ach1

When I play a round where I collect two biscuits and exit the round, the game works properly.

debug2

ach2

However, when I play another round of the same mode and get hit by the object three times again,

this weird glitch happens.

debug3

The BadBiscFromSave generates a high random number instead of the number 2 (which is the total amount of bad objects I collected)! I don’t understand why this glitch is happening and why it only triggers when the player finishes a round.

Here are additional screenshots of the code:

Again, this “generate a really high random number” glitch occurs only after the player finishes a round. It does not occur when the player restarts a round or exits to the menu screen.

In the last image, I cant tell if it’s cropped but it looks like there’s no condition for the loading, increasing and then resaving of the count. If it’s not a subevent of something then it needs to have a condition like a boolean check.

If the events in the last pic are subevents of miss≥3 then maybe miss needs to be set to 0 ir it will continue to execute.

Otherwise, I’m not sure what the issue is.

I updated the code by creating another version of the SaveBiscs external event. Here’s how it looks like:

And here’s where I put the code:

Now when I play one round of the game with 3 biscuits collected,

it is able to save the amount of biscuits the player has collected:

ach1

However, when I play another round with 2 biscuits collected,

the amount of biscuits collected is overwritten.

ach2

For some reason, the game is unable to check the existence of the BiscCollected and the BadBiscsCollected groups in the storage. When I removed the event with the “(Group name) does NOT exist in storage” condition, it is unable to save the biscuits collected into the storage at all.

I FINALLY DID IT! I solved the problem with a simple solution: make it so that when the round is over, the amount of objects collected will trigger only once. Here’s the new version of the code:

Here’s a screenshot of the results screen after I have played a round: (ignore the “Count: 304” on the top right)

And here’s the screenshot from the debugger:

debug

Thanks to the new code, the game is able to calculate the TotalBisc and the TotalBadBisc variables properly. 305 (BiscCount) + 100 (BiscFromSave) = 405, and 18 (BadBiscCount) + 6 (BadBiscFromSave) = 24 (TotalBadBisc).

Here are some of my thoughts and observations regarding the glitch:

  1. I can’t believe I needed to add just one condition to fix this whole mess. I should’ve noticed it earlier; the TotalBisc and the TotalBadBisc variables were properly calculated when I quit or restarted a round and didn’t need the “trigger once” condition to do so. But it wouldn’t do the same when the round is over. I tried to make it so that the code will only trigger when the miss count is exactly 3 or when the timer in one of the modes I have hits 0, but those did not solve the "generate a really high random number” glitch.

  2. Coincidentally, this isn’t the first time an event was triggered rapidly multiple times in a row until I used a “trigger once” condition to stop it. In the earlier version of the game, when a biscuit would hit the ground, it would go into an animation where the biscuit is broken and flickers a couple of times before it disappears. When I added other actions like making the game play a crackle sound effect and increasing the “miss” count by 1, it would trigger the actions multiple times while the broken biscuit animation was being played until it disappeared. So I needed to add the “trigger once” condition to prevent that from happening.

Now that I am done fixing the glitch, I can finally finish the game and publish it some time this afternoon or tonight.