playMaker

Author Topic: Dont Destroy On Load and duplicated Instances + Saving/Loading  (Read 7764 times)

Mayhem

  • Full Member
  • ***
  • Posts: 171
Dont Destroy On Load and duplicated Instances + Saving/Loading
« on: December 12, 2013, 09:41:01 AM »
So, some months ago I asked the same question:

http://hutonggames.com/playmakerforum/index.php?topic=3677.msg17033#msg17033

We're working on a mobile game.
That time I found another "solution" at Unity Answers which doesn't have to do anything with PlayMaker. I was suggested to have a scene like the Scene before the Start-Scene which loads once and there I create the objects with "Dont Destroy On Load" and can use them as I like in the following scenes. The only problem is: This scene has to exist and won't be loaded only once. For example, when the Application is quit and complete closed by the user that specific scene will load again and duplicate objects. That is a no go for the game.

I searched here in the forums how to tackle this with PM and found different topics like:

http://hutonggames.com/playmakerforum/index.php?topic=4053.msg18891#msg18891

or

http://hutonggames.com/playmakerforum/index.php?topic=1653.msg7288#msg7288

But I can't figure out how exactly I should use these global variables.

Let's say:

- Scene 1 has the Player and a MANAGER-Object

-> The Manager from Scene1 must check if the Player exists, when the level is loaded again from Scene 2 for example -> if the Player exists, I have to destroy the duplicate (or scene-instance or whatever it is called)
- since the Manager checks also for the Player on START when I start the game in Scene1 he destroys my first player and the scene has no player at all.

How to deal with it exactly?

I need some help on this since the "life" of some certain objects have paramount importance, like the UI. We have a Booksystem Quests and Items, and these Elements should be carried over from level to level. So after revisiting older levels there can't be two/three/or more UIs.


And another thing:

As mentioned above we have these Quests and Items which are Labels and Sprites (nGUI/2DTK)
Now I don't know how to save/load these things.

For example: When the Player ends a certain Quest and gets an Item, the Questpage in the book and the Text (NGUI-Label) is marked as "Accomplished" and the Item-Sprite is shown in the Inventory.
When the Player ends the level the game should be autosaved. When the Player quits and closes the game he can load it in the Loadmenu and all the progress incl. the accomplished quests and the collected items should be "there" like he left the game.

What would be the best way to save/load these things?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Dont Destroy On Load and duplicated Instances + Saving/Loading
« Reply #1 on: December 14, 2013, 01:01:18 PM »
Hi,

 Re your first question ( always try to make a thread per questino, it's easier :) )

-- you defined it in your descriptiong, apply this as a fsm.

Check for the player on fsm start, and if there, destroy self, that's all there is to it from what I understand.

I think in your case, since your condition is the existance of the player, using a global doesn't apply isn't it? it's only when you want a singleton that you need to maintain a gloval flag per singleton to know if it's already present or not.


Re your second question:

It really depends how much data you need to save. PlayerPrefs can be used, but a proper game state serialization system could be handy here I feel, something like:

http://whydoidoit.com/unityserializer/

Bye,

 Jean

Mayhem

  • Full Member
  • ***
  • Posts: 171
Re: Dont Destroy On Load and duplicated Instances + Saving/Loading
« Reply #2 on: December 14, 2013, 04:07:56 PM »
1) That's the thing, how exactly can I check if the player exists with PlayMaker?

2) I tried UnitySerializer, and though it's great it does not work with the latest Version of Unity :/

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Dont Destroy On Load and duplicated Instances + Saving/Loading
« Reply #3 on: December 15, 2013, 05:35:09 AM »
Hi,

 to check if a player exists, you either try to find it using the action "Find Game Object" or your player is setting a global boolean variable to true when it starts and anyone interested can simply check that variable.

It's the whole principle explained for these singleton, it doesn't matter who is checking for this global boolean flag, what matters is that the player or the singleton sets it to true when it starts AND set it to false when it is destroyed ( ONLY IF IT WAS THE ONE WHO SET IT TO TRUE, if you try to instanciate a second player, it has to check first if the flag is true, in which case it would destroy itself silently, because a player exists already)

bye,

 Jean