playMaker

Author Topic: Global variables between loads seems.... random  (Read 2553 times)

nickg84

  • Playmaker Newbie
  • *
  • Posts: 10
Global variables between loads seems.... random
« on: September 01, 2022, 02:46:08 PM »
I'm running into some pretty random issues regarding global variables.  I don't know if I'm just setting it up wrong (I could be, I'm learning).

I have a player prefab with lots of components and logic.  I'm using a number of global variables to save and load clothing between scenes.  I'll store the data into the variables at save points or as an event right before a player enters a new scene.  Then, the data is loaded right away in the new scene.

It works.  I can equip anything, save at a checkpoint and reload, all good.  Enter a new scene, all works.  Until it doesn't, randomly.  I've even tested it where it doesn't work anymore, and without touching anything, I replay the scene to keep an eye on the Playmaker Globals window and... it now works again.

It's very baffling and random.  But I'm mainly here because, perhaps, I'm just using global variables all wrong.  As I said, I'm still learning so my completely spaghetti workarounds to solutions might work well in my head, but Unity isn't having it.

Thoughts?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7618
    • jinxtergames
Re: Global variables between loads seems.... random
« Reply #1 on: September 02, 2022, 02:14:34 AM »
Hi
On each scene, are you reloading the player?

Maybe if you set the player with a don't destroy on load and add a singleton manager (Ecosystem)

this way the player does not need to reload.
Then set the player on a global variable (call it Player for example)
then on the player, have a "Data" fsm.
on that fsm you can Get/Set all kinds of variables that you need.
(Using Get/Set Fsms xxxxx actions)
there are also many addition Fsm actions on the Ecosystem
For example 'Fsm Bool Test' which can be very useful.

for globals i only use Gameobject variables.

if you get data from your globals, are you doing some on start?
if so try setting a 'Next Frame Event' and see if that helps.

GMPlay

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Global variables between loads seems.... random
« Reply #2 on: September 02, 2022, 08:14:52 AM »
Hey there nickg84,

adding to the suggestions by djaydino,

 I was also having these 'random' issues with global variables. First of all I decided to try and avoid global variables as much as I can. And now I have zero globals in my project so far. May be Ill have them as I progress depending on the case.

based on a previous conversation with djaydino I realized most of my problems were due to fsm's unintentional order of execution and even the individual actions within fsm execution order.

Everything went smooth once I was advised to use 'action sequence'. This is a great tool to initialize things that need to happen in a specific order lets say when the scene loads. Coz if they don't then it may cause global variables to become highly unreliable.

I suggest you too look into it. Avoiding global variables as much as possible has turned out to be useful for me. 'Action Sequences' are something not be be ignored as well.

Cheers.
 

nickg84

  • Playmaker Newbie
  • *
  • Posts: 10
Re: Global variables between loads seems.... random
« Reply #3 on: September 02, 2022, 04:45:25 PM »
Thank you both, this is all super helpful!  I'll give it a shot.

As a side question: do you know why my globals would reset back to 0 upon reloading the scene (say, after death)?  I turned off anything that would "set" the globals, so not sure why I'd have a global INT stored with, say, 3 at player death, but at player reload it resets back to 0.  It was my understanding they survive from scene to scene, unless it's because they're stored in a prefab?

GMPlay

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Global variables between loads seems.... random
« Reply #4 on: September 03, 2022, 01:47:08 AM »
Exactly.

It is things like these I couldn't understand as a beginner, which irritated me a lot.

 It is tough to track down which fsm (or script) is modifying a global at what point. Playmaker has a powerful feature to see local variables values while fsm is LIVE, this helps in debugging so much easier.

But for globals, it gets hard to understand what is happening. Which brings us back to the point that avoiding globals as much as possible is better unless we know how to track globals properly. Even in Playmaker video tutorials it has been mentioned that having only a couple of globals per project is enough but not more. Sure they can be conveniently accessed from everywhere but it is a double edged sword. Coz that means they can be modified from anywhere also. So many times my algorithms were convoluted due to this.

One more tip: Sometimes when we are observing a global variable in the 'global variable' window, it does not always 'display' the updated value. When I move the mouse cursor over the 'global variable' window only then the updated value shows. It seems the window doesn't refresh in certain cases, this can add to confusion while debugging. I don't know why this happens, my guess is when global is modified multiple times in a short time then the window display cant keep up.

ALSO. djaydino

"Maybe if you set the player with a don't destroy on load and add a singleton manager (Ecosystem)

this way the player does not need to reload.
Then set the player on a global variable (call it Player for example)
then on the player, have a "Data" fsm."

.....this is an extremely useful info. Could you please elaborate. Coz I think I want to implement a Player with global variable but I may need to wrap my head around it first. Thanks.
« Last Edit: September 03, 2022, 02:12:22 AM by GMPlay »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7618
    • jinxtergames
Re: Global variables between loads seems.... random
« Reply #5 on: September 03, 2022, 03:27:40 AM »
Hi.
In our game "Dark Light" i actually use a scene for the player instead of a prefab, and scenes are loading additive.
Each of the Level Scene have a 'Map Manager Fsm' when loaded in, it will position the player to the location it should be and enable the player / fade out loading screen.

i have 1 gamobject in the global, called Meta Data which hold links to other object / has a save system / player inventories / etc
Then i have a gameobject (also Global) with item data wit hash tables that holds data for each item used in the game.

then 1 global for player root and player damage handler

there are a few more global Game objects i have but those 3 are the major ones.
As i need to access them a lot from other objects.

I could set just one Global and use 'Get Fsm Game Object' (which i do for some object that rarely need to be used)

The Singleton manager will make sure that you will have only 1 instance of an object, this is not a required thing if you are sure you can load only 1 player.

its good for example if you have for example a "Player" object in scene 1

It will make sure that if you would load scene 1 again that the "player" object on that scene will be removed and the object that you set as singleton will stay.
if you did not use the singleton you will end up with 2 "Player" game objects.

Don't destroy will make sure that the game object will not be destroyed if loading to a different scene.

GMPlay

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Global variables between loads seems.... random
« Reply #6 on: September 03, 2022, 05:11:49 AM »
(Moderators please excuse me if I may be hijacking the core topic here.....)

Okay, having player as a separate scene makes a lot of sense, and loading it additive solves this problem. Great!

My last concern would be... does the singleton manager automatically destroys the player in previous scene and frees-up the memory?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7618
    • jinxtergames
Re: Global variables between loads seems.... random
« Reply #7 on: September 04, 2022, 10:04:36 AM »
Hi.
It will destroy the object when you load that scene again.
for example in scene 1 you have an object called 'Audio Manager' with a singleton.
Then you load a different scene (single, not additive)
Then you load back to scene 1, now it will destroy the 'Audio Manager' from scene 1 and keep the Audio Manager that was loaded the 1st time.

if you load additive and keep scene 1 alive, then there is no need for the singleton.

But since i use a separate scene i am not using singleton for this.

Also it was more tedious to use a prefab, as a lot of changes where done on player.
So a separate scene for the player worked best for me as i can make a copy of the scene.
And if i want to use it in the game i just rename and place in the build settings.
(we are now at version 63 :) )

GMPlay

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Global variables between loads seems.... random
« Reply #8 on: September 05, 2022, 07:38:29 AM »
Okay then,  since we are talking about globals, singletons, prefabs and loading scene additive. It appears in your case it suits your purpose, and totally makes sense  :o Gosh ver.63!!

To finally clear up things, please bear with me one more time  ;D

Here is my situation:

A 3D point and click adventure game where the camera moves to a hotspot when we click. Left click(on hotspot) moves forward, right click (anywhere) moves backward. Each hotspot maintains a list of the other hotspots that connect to it.

If we are at the first hotspot of a scene, then right-click takes us to the previous scene. Similarly if we are at the last hotspot,  left clicking it will take us to the next scene.

I have a game manager with multiple fsms that handle most of the logics behind the player(camera) movement, interaction, inventory, dialog window, scene transition and what not. Even the maincamera and player are part of the game manager in the hierarchy itself.

So now all I have to do is drop the game manager prefab into a scene. That's it. All the logic that I have fsm'ed  still works, no problem, as long as I have the hotspots setup correctly.

So as long as the scene contains GameManager prefab , things just work. Therefore each scene must have Gamemanger prefab. I may have 10-20 scenes per story-chapter. 6-7 chapters in total.

But I am not sure about this approach when a scene transition happens. In order to solve the puzzles, the player would have to go back-forth multiple times between scenes carrying data for items, puzzle states etc.

Please suggest if this is the best approach for this type of situation, or does it have the potential to take a performance hit (on mobile)? My concern is that I may end up hogging memory when player is moving between scene and there is not proper garbage collection.

Please kindly suggest:

Do I need to have singleton and exactly where?

Do I need to load the adjacent scenes additive in this case?

How does 'Do not Destroy on Load' can play its part here?

Finally where does the global variable(s) play its part in this kind of setup?

Really appreciate the discussions so far, hope this is not becoming a nuisance.

But let me tell , once this confusion of mine is rectified I can finally move on and start building the actual game :)

Thanks to you and playmaker I have come so far! But still a long way to go.



« Last Edit: September 05, 2022, 07:42:03 AM by GMPlay »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7618
    • jinxtergames
Re: Global variables between loads seems.... random
« Reply #9 on: September 06, 2022, 04:28:50 AM »
Hi.
In our case we load and unload world scenes.
Player scene will stay all the time (unless you go to main menu)

our camera is on each scene as we have different camera settings depending on the scene.

the player has some loading time needed, so in this way it only needs to be loaded once, instead of loading on each scene.

Quote
Do I need to have singleton and exactly where?
There is no need for this, if your setup has no chance to have duplicates due to changing scenes.

if the loading (time) of your scenes is fine, don't change what you have.

Only make 1 global with a 'Data' gameobject here you can store for example your inventory (array maker's (Ecosystem) array list or hashtable are easiest to access and are more flexible/powerful than the build in arrays)

also have your save system on it

GMPlay

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Global variables between loads seems.... random
« Reply #10 on: September 06, 2022, 10:08:40 AM »

if the loading (time) of your scenes is fine, don't change what you have.

Only make 1 global with a 'Data' gameobject here you can store for example your inventory (array maker's (Ecosystem) array list or hashtable are easiest to access and are more flexible/powerful than the build in arrays)

also have your save system on it

Thank you for the valuable info. Very soon I'll implement all systems into Game Manager, and observe load times closely. If it gets too busy, then I may have to refactor as per your previous suggestions.

Cheers!