playMaker

Author Topic: Help with singletons  (Read 2296 times)

Meshed

  • Playmaker Newbie
  • *
  • Posts: 3
    • View Profile
Help with singletons
« on: February 27, 2017, 08:26:46 PM »
I am trying to figure out how to use PlayMaker in my games in conjunction with a singleton game manager.

Basically, I have been a script junky for years and I am trying to figure out how to make my life easier with PlayMaker. This is the last piece I need of the puzzle.

I found a post that talks about using the singleton object to allow an FSM to live from one scene to another: http://hutonggames.com/playmakerforum/index.php?topic=13160.msg61371#msg61371.

What I don't fully understand yet is the logistics behind interacting with the GameManager FSM. Is this the same as interacting with other FSMs?

Does anyone have a small sample project, article, or video that shows this being done?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12351
  • Official Playmaker Support
    • View Profile
Re: Help with singletons
« Reply #1 on: February 28, 2017, 06:21:47 AM »
Hi,

 Interacting with a singleton is exactly the same, the only and great advantage is that you don't have to worry about it being duplicated, so you can have for each of your individual level scenes, managers of all sorts and you can run an individual level without having to start from the beginning of your game, via the menu and all. and when playing from the start, all managers within each level you'll load will be removed to only ever keep the first and only one instance generated.

 that's all, accessing a singleton is then exactly the same, it will received event being broadcasted for example.

 Bye,

 Jean

Meshed

  • Playmaker Newbie
  • *
  • Posts: 3
    • View Profile
Re: Help with singletons
« Reply #2 on: February 28, 2017, 05:22:18 PM »
Thanks for your feedback. I tried playing around with it a bit and have a follow up question.

Do I need to add the same Game Manager singleton to every scene?

If not, how do I reference the Game Manager from the previous scene?

To test this, I created a test project with two scenes. To each scene I added two UI Text, one displaying the level name and one blank, two empty game objects that I attached an FSM to with the singleton manager action, and a button to load the second scene.

In the first scene, I have the game manager setting the text to the blank one to Manager1, and in the second, Manager2.

When I run the project I get Manager1, and after clicking the button the second scene loads but I see Manager2.

The reference name for both Game Managers is "Game Manager".

I would expect to see Manager1 on both scenes since the second manager shouldn't have been created.

Can you help me understand what I did wrong?

Meshed

  • Playmaker Newbie
  • *
  • Posts: 3
    • View Profile
Re: Help with singletons
« Reply #3 on: February 28, 2017, 06:21:28 PM »
Did a little more testing and found that if I put the action on a separate event from the one I put the Singleton Manager on, the second scene's game manager is not there and the event to set the text value doesn't get called.

So my main question now is how do I access the FSM on the GameManager that has the Singleton Manager on it from the first scene in the second scene?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12351
  • Official Playmaker Support
    • View Profile
Re: Help with singletons
« Reply #4 on: February 28, 2017, 11:28:02 PM »
Hi,

you have lots of options:

- everytime a level is loaded, your singleton can fire a global event "I AM HERE" and then every interested fsm can simply catch this event and store the gameobject you sent that event

- you can use FingGameObject action, WARNING: use tags instead of the name to find objects, finding by name is very bad for perfs, it's a Unity thing unfortunatly...

- you can have a global fsm variable that your singleton set the at starts

Bye,

 Jean