Why my Global Variables are reset on another scene ?!

Hello everybody. I have just discovered a SERIOUS problem in my game today.
I have a bunch of items in my game that grant different effects and stats via increasing global variables, and sometimes object variables. For example, one item gives me damage, so it increases a global variable called damage. Other more complex items that grant a special effect, I increase an object variable for the player. When the object variable = 1, it means I have the item, and the effect can do it’s thing and call it’s corresponding events.
However, my game is going to take place over different scenes. This is where the problem is. When I change scenes, ALL of my global variables and object variables are reset to their defaults. I have no idea what is up! Can anybody please help me out on this? How do I make global variables actually ACT like global variables?

Note: I feel like the culprit may be in the player’s logic. For every stat the player has, it goes:

Condition: Trigger Once.
Action: Do = 1 to Global Variable Damage.
I only want these starting stats to be applied once, not for every time the character exists in a scene. If any of you think this is the reason or not, please let me know how I can fix it. many thanks in advance. :slight_smile:

If you have this event for every single scene, it is going to be triggered each time you start a scene and will set the global variable to 1.
If you want to do it only once then do it only once at the first scene when the game starts or in case you want to do it only when certain conditions are met, then add those conditions with Trigger Once.

1 Like

I will need a bit more information before I can help, but @ddabrahim is right - if you have an action in the beginning of every scene to set the Global Variable to = 1, then it will constantly be reset back to 1 every time you start a new scene.

A way to work around it would be with conditions to check if there has already been a value assigned to Damage, for example. You could make a new global variable just to check that, or, if the value of Damage can only be 1 and 0, you could use a condition to check if the value is 0. I’m assuming Damage will have more values than just 0 and 1, though, so we will use a “confirmation variable” for this.

If global variable "ValueAssigned" = 0
Trigger once
  do = 1 to global variable "Damage"
  do = 1 to global variable "ValueAssigned

This way, the "Damage = 1 " will only happen if ValueAssigned is empty.
Occasionally, if Damage can NEVER be 0, you could just do something like:

If global variable "Damage" = 0
Trigger once
  do = 1 to global variable "Damage"

This is a good quicker solution without needing to make a new variable to check for it. This way, if Damage already has a value, it will not be assigned the “= 1” again - and you can feel free to use the event in every scene with no concern.
I hope this helps!

Yes, thank you. I put all stats in a “Beginning of Scene” event and it works! But how can I do this for object variables?
For example, remember when you helped me with the Walnut? Picking up the Walnut item increases an object variable for the player. When the object variable is 1, the character has a chance to shoot walnuts. When I go to another scene however, the player is no longer able to shoot walnuts. How can I have object variables transfer to other scenes, or will I just have to make them all global variables instead?

I mean, yeah, there are a few ways to do it!

Global Variables are the most practical ones if there are only a few variables, though. If it’s only 3-6 scene variables you need to pass from one scene to another, it shouldn’t be that much of a hassle to use events to store the values of the scene variables into global ones and then pass them back into scene variables in new scenes. Something like:

// Before Changing Scene //
If variable "Walnut" of Bullet = 1
  do = Bullet.Variable(Walnut) to global variable "Walnut"

// In The New Scene //
At the beginning of the scene
If global variable "Walnut" = 1
  do = GlobalVariable(Walnut) to variable "Walnut" of Bullet 

Honestly, though if it’s a stat that needs to ALWAYS exist through the game - for example, HP - making it a Global Variable is probably a good idea. Though it wouldn’t make sense to do it if there’s more than one Player object, so you can disregard what I just said if that’s the case.

If you want another way, however, you can just use the Storage - create a special storage file just for this kind of data, and store the variables there every single time before you change a scene; and just load the variables from that storage file when you’re in a new scene.
In one hand, it’s a bit more practical than having Global Variables for every object variable, but in the other hand, it’s only worth doing if you really have way too many variables to keep track of, as opposed to only one or two, in my opinion. It’s also easier to load if you use the right expressions, too.

Also, the Walnut is an item, right? You can also just use an event in the other scene to check if the Player still has the Walnut in their Inventory, and if they do, tell it to do = 1 to the Walnut variable again. ^v^; Something like:

At the beginning of the scene
If inventory "A" contains a "Walnut"
If variable "Walnut" of Bullet = 0
  do = 1 to variable "Walnut" of Bullet

This should make sure that as long as Walnut is in the player’s inventory, every time you start a new scene the “Walnut” variable will be 1. If it’s supposed to only work when equipped, you can use the “If Walnut is equipped in inventory” condition instead.

I hope this helps!

Okay, so I tried out the thing that tests if the Walnut is in the inventory, and it works! I just have to do this for all the other object variable items now! Thank you for the help with that, but there is one more thing that isn’t going right.
The player has an inventory, which is an external scene. This inventory displays the items the character has found, and you could even mouseover the items to get a little description on them (Note: They are put into the inventory in random places instead of in order for now. Not sure how to fix that, but whatever.)!
When I go to the next scene, the slots are empty, even though I’m still carrying the item.
I’m pretty sure I can just use your inventory example again (If item is in inventory, change animation to “ItemName.”) However, is there a more optimized way to save animations across scenes?

By “external scene”, do you mean a different scene or do you mean an External Layout?

Also, in order to display these items in the inventory, do you use conditions to check whether an item is in the inventory or do you use scene variables?
If you used scene variables - for example “Player has found Item1, so +1 to scene variable Item1”, then it makes sense why it won’t load in another scene. Using the built-in Inventory system to keep track of items is easier exactly because of this, it’s global. If you ARE using scene variables to check when the slots need to be filled, perhaps try replacing these conditions with Inventory ones?
And if you used the Inventory conditions, which would be the more practical way of doing it - I don’t know why they don’t load when you change the scene.

Also, you mentioned animations, right? Are you using animations to keep track of the items? As in, “Player got this, trigger once, change animation to this”?
If so, then I can understand the issue more properly. There isn’t really an easy way to save animations - I mean, technically, you COULD use Structures to do it, something like:

// Save Current Animation of Slot  //

For each Slot object
If scene variable "SaveInventory" = 1
  do = Player.Animation() to global variable SlotSave[ToString(Slot.Variable(ID))]

** Important** 
Assign an unique "ID" object variable to each Slot instance for this to work, in order to make storing them into a global variable easier.

// Load Inventory Animation When You Start It Up //

For each Slot object
At the beginning of the scene
  do = GlobalVariable(SlotSave[ToString(Slot.Variable(ID))]) to the number of current animation of Slot

This will store the value of the current animation of each Slot object into a global Structure, and will load it back into the Inventory scene whenever you need it.

If none of these are the issue, I think seeing a screenshot of the events for that would make it easier for me to help and stuff. ^v^;;

I hope this helps!

Yeah, sorry about not being specific about the last post. I actually meant external layout, but I call them external scenes.
And here are screenshots. When the item is grabbed, choose a random slot in the inventory, and change it to it’s corresponding animation. If this is a different kettle of fish, then how do I save the animations across the whole game, or will changes have to be made?

Picture of the code:

Picture of the inventory, with all the current items inside: