playMaker

Author Topic: Player as Global variable and changing levels [SOLVED]  (Read 3215 times)

DanielThomas

  • Beta Group
  • Full Member
  • *
  • Posts: 150
Player as Global variable and changing levels [SOLVED]
« on: July 28, 2016, 07:13:43 AM »
Hi!

I have a game manager object that puts the player object as an global variable - I use this for enemies and such to be able to use the player in it's state machine.

My player and manager objects both have 'singleton manager' and 'don't destroy on load' actions when they initiate.

it works fine, the global variable has the player object stored, they both move when switching level.

Problem is:
I start with 1st level where the manager and player object are placed in the scene. I change to 2nd level, so far all good, global variable hold the player object. Now I go back to 1st level where the player and manager object are in the scene. I don't get duplicates - BUT the global variable is empty.

So what is going on? The player objects doesn't change anything, it's in the states it was in previous level. So it's not like the object is destroyed and recreated. But somehow the global variable gets reset when the game manager and player object already are in the scene.
What works though is to use "find game object". This makes sure the global variable keeps the player object in it.


Any thought? Or any other idea how to where I can use/find the player in other object FSM's?

« Last Edit: July 28, 2016, 03:18:24 PM by DanielThomas »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15498
  • Official Playmaker Support
Re: Player as Global variable and changing levels
« Reply #1 on: July 28, 2016, 08:53:08 AM »
Hi,

It's likely a state you forgot that actually set that variable to null or too soon in its process.

You could tag your player with the "player" tag and then use findGameObject just with tag, tgat's very quick.

You could also check in your fsm tgat the variable is null, in which case tou go find the player, else you do nothing.

In all cases i would properly debug this , because it's likely an issue with your logic, else this is something we should file as a bug and fix.

Bye

Jean

DanielThomas

  • Beta Group
  • Full Member
  • *
  • Posts: 150
Re: Player as Global variable and changing levels
« Reply #2 on: July 28, 2016, 12:11:36 PM »
Thank you for the reply.
As I mentioned I managed to make it work with the "find by name", but I wasn't sure if this was optimal, and It makes me worried that maybe something else is wrong since I can't see why it would lose the player object in the global variable.

"The singleton" and "don't destroy object" are at the top of the FSM, it sets the global variable below them.

After some investigating I found out that removing the "singleton manager" from the player object makes the global variable keep the object. Obviously this creates a duplicate, so that is a no go.

SO, I made an new and empty project and tested this.
* Two objects, both start with singletonManager and dontDestroyObject.
* One object store the other in a global variable
* I reload the scene with loadLevel
* The global variable is now empty, no duplicates of the object is  in the scene.

- Test to disable the singletonManager in the object that is stored.
* After reloading the scene, now the global variable keeps it's value, but obviously there is now a duplicate of the object.


So it seems that it's the Singleton manager action - in the object that IS stored-  that cause this.


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15498
  • Official Playmaker Support
Re: Player as Global variable and changing levels
« Reply #3 on: July 28, 2016, 01:25:09 PM »
Hi,

Do you have an old version of the singleton manager? I recall haing provided an update, but i think it was when using several singletons.

Any how, you should then have a dedicated scene and prefab to really grasp what a singleton do and how it behaves, you could have a race condition between your fsm that does somethibg and the fsm that destroys the duplicate, so maybe you should wait obe frame if your logic is on the start state.

Bye,

Jean

DanielThomas

  • Beta Group
  • Full Member
  • *
  • Posts: 150
Re: Player as Global variable and changing levels
« Reply #4 on: July 28, 2016, 02:04:46 PM »
I got the one on ecosystem.

Moving the setObject to another state did it! So I probably don't understand Unity/playmaker enough. So basically what happened was the two FSMs was "racing" and one of the duplicates wasn't deleted fast enough?

Eitherway, thanks for the help! I learned something new :)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15498
  • Official Playmaker Support
Re: Player as Global variable and changing levels [SOLVED]
« Reply #5 on: August 10, 2016, 05:24:21 AM »
Hi,

 yes, likely. Racing condition happens with and without PlayMaker, it's a concept/issue that is important to keep in mind when designing logic. you'll get confortable with this as you gain experience, but be aware this is a difficult problem to debug on big project, so always be very careful during initialization protocoles, and similar features, there is always a potential order of actions issue lurking round the corner :)

 Bye,

 Jean