Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: wallaceb68 on February 26, 2014, 09:43:29 AM

Title: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: wallaceb68 on February 26, 2014, 09:43:29 AM
How do i handle a global game object variable from changing between scene's
Title: Re: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: Lane on February 26, 2014, 09:49:50 AM
Can you give an example?
Title: Re: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: wallaceb68 on February 26, 2014, 11:09:43 AM
YES, i have a ten G.O.(dogs) that can be selected, when one is selected it set's the global G.O. variable " dog a chosen" to (dog 1) (if dog 1 was the one selected)
then when i go to another scene, the G.O. variable has been cleared, the dog i selected isnt set in the G.O. variable "dog a chosen" anymore,
Title: Re: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: Lane on February 26, 2014, 11:24:20 AM
When you switch scenes it doesn't carry any information over from the old scene.

Use DontDestroyOnLoad if you want to retain objects between scenes.
Title: Re: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: wallaceb68 on February 26, 2014, 12:03:08 PM
THANK YOU LANE, that worked, but now when i leave scene one and go to scene 2 then back to scene one, there are two of them! what am i missing? what do i do? thanks again.
Title: Re: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: Lane on February 26, 2014, 12:24:28 PM
Depends on how you're creating the dogs, I guess. If level 1 creates a dog that will not be destroyed on load then it will be in the next level, whatever that may be. So if you spawn another one it would keep creating them as you switch levels.

Maybe do a check to see if the global dog variable is null and skip creation if it isnt it?
Title: Re: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: wallaceb68 on February 26, 2014, 01:11:00 PM
The dogs are in scene one to begin with, they are in a kennel, then when selected it will run a itween path to a car and become a child of the car, then i drive the car to a spot on the terrain and when i get out of the car it sends me to another scene 2. then when i am done in that scene i click on the car and it will send me back to scene 1 where i drive home. and it is all working except when i go from scene 2 back to scene 1 there are 2 players, 2 cars, 2 dogs all the same.
Title: Re: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: Lane on February 26, 2014, 01:27:36 PM
If you have the dogs in scene 1 already, then when you load it again it will have two dogs because the one from the previous scene will still be retained.
Title: Re: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: wallaceb68 on February 26, 2014, 01:35:03 PM
yes, that is what is going on, (sorry sometimes i dont type what i think, lol) so any suggestions on how to stop it from loading it twice?
Title: Re: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: Lane on February 26, 2014, 01:49:00 PM
You can destory the dog on level 2 right before you switch to level 1 or create the dogs at runtime in level one, but first check if the global is null, to see if a dog is coming from level 2.
Title: Re: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: wallaceb68 on February 26, 2014, 03:24:29 PM
I just cant figure out how to destroy an object that came from a previous scene. i am trying to use destroy object , but cant set it up in the scene if the object isnt there yet.
Title: Re: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: Lane on February 26, 2014, 03:27:44 PM
Destroy it before leaving the scene.
Title: Re: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: wallaceb68 on February 26, 2014, 03:32:12 PM
yes but how? i can't set a 'destroy G.O.' action because the G.O. isnt in the scene yet. i must be missing something.
Title: Re: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: Lane on February 26, 2014, 03:48:33 PM
Destroy the one that is persisting through the scenes before you switch levels, or spawn the dogs at runtime in each level and test to see if they already exist. If they do, then you can skip creating them.
Title: Re: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: wallaceb68 on February 26, 2014, 11:38:26 PM
OK, IM STILL NOT GETTING THIS, how do you set-up something in a scene to be destroyed, that isnt in the scene yet? or how do you check the count of a specific G.O. in a scene and if it is more than one i can us destroy self! i have tried setting a global G.O. variable and the the G.O. to the car but when i switch scene's it lose's the preset. i thought global's where to keep there value globally!
Title: Re: GLOBAL G.O. VARIABLE CHANGES BETWEEN SCENE
Post by: jeanfabre on February 27, 2014, 03:25:59 AM
Hi,

 A global variable referencing a gameobject in a scene will loose that reference if this gameobject is destroyed ( deleted or because you changed scenes and that gameobject was not set to persists)

 A variable ( global or not) is only a "pointer" in the case of GameObject.


What you seem willing to achieve is called a "singleton". Something that can only be there once.

 Typically, you can solved this manually, by "finding" if a gameobject of the same name exists in the scene and destroy self if it does, or use a global variable "xxx" unique for each individual singleton, and check on START if that global variable is true, in which case it exists already and you destroy yourself, else, you set it to true and become that singleton.

Also, this is such a common pattern that I created an actino for this that handle this graceful in one action.

Typically, drop this action on a start state of a gamobject you only want once in your scene at any time. Use the reference as a unique identifier, and done.

If you have trouble understanding this, let me know. I'll provide a sample.


Bye,

 Jean