playMaker

Author Topic: New Scene / Store variables etc [SOLVED]  (Read 509 times)

curb47

  • Full Member
  • ***
  • Posts: 219
    • View Profile
New Scene / Store variables etc [SOLVED]
« on: October 27, 2020, 04:19:07 PM »
Hello again,

I'm at the point of my game where i need to start implementing a system that carries data from one scene to another.
The simplest example is, I have my little spaceship whizzing around, you collect some stuff, you take some damage, you lose energy etc, and when you dock into the mothership, it loads a new scene that represents the inside of the mothership but is just 4 sliding screens where you can upgrade weapons, get more health re-charge energy etc.

If you go into the mothership with 50% health, and you don't restore health whilst inside, when you leave the mothership your health should still be at 50%.

I looked at Easy Save, which has good reviews bit I don't think it's exactly what I need.

Is there a Playmaker system that handles this kind of thing?

Many thanks.

J.
« Last Edit: October 29, 2020, 05:15:52 AM by curb47 »

wetcircuit

  • Junior Playmaker
  • **
  • Posts: 99
    • View Profile
    • wetcircuit.com
Re: New Scene / Store variables etc
« Reply #1 on: October 27, 2020, 04:42:16 PM »
you can create Unity objects that don't get destroyed when the scene changes.

There is a PM action: "Dont Destroy on Load" to do it.

curb47

  • Full Member
  • ***
  • Posts: 219
    • View Profile
Re: New Scene / Store variables etc
« Reply #2 on: October 28, 2020, 01:23:39 AM »
Okay, thanks.

What about variables? Like health, energy levels, bombs carried. These exist as variables (floats & Ints) not game objects.

Thanks again. I'll look into your suggestion.

J.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 5784
    • View Profile
    • jinxtergames
Re: New Scene / Store variables etc
« Reply #3 on: October 28, 2020, 03:24:13 AM »
Hi.
These variables can be on a fsm on the gameobject that you don't destroy on load.

This 'Data' object you can set as a global for easier access.

and use get/set fsm actions.
You can find more fsm actions on the ecosystem, like FSM Bool Test / Fsm Int Add etc

curb47

  • Full Member
  • ***
  • Posts: 219
    • View Profile
Re: New Scene / Store variables etc
« Reply #4 on: October 28, 2020, 09:55:39 AM »
Ah, I see. A Game Manager object, that 'gets' the FSM variables from other objects (Health, Energy etc) and stores them, use Don't Destroy on Load when docking with Mothership, then when you Launch again, you set the various variables from the Game Manager. Sound right?

Or should the Game Manager have all the variables (Health, Energy etc) as the main logic in the runtime of the scene/game?

Thanks for your help.

curb47

  • Full Member
  • ***
  • Posts: 219
    • View Profile
Re: New Scene / Store variables etc
« Reply #5 on: October 28, 2020, 01:56:47 PM »
Djaydino... I'm curious, what's the Additive setting?

I've been reading about it a bit. Does it 'add' the new scene to the current scene? Are there benefits to that, or does it add more 'weight' to the loading etc?

Thanks.




wetcircuit

  • Junior Playmaker
  • **
  • Posts: 99
    • View Profile
    • wetcircuit.com
Re: New Scene / Store variables etc
« Reply #6 on: October 28, 2020, 03:01:05 PM »
This is a Unity thing.

The original concept was 'levels' which were loaded and unloaded, like you are thinking.

Now it's 'scenes' which can be 'added' and removed, but 'scenes' are more production savvy – the idea being that different teams might work on different aspects of the level (sound design, lighting, backgrounds) and these 'scenes' are modular so they can be added and removed dynamically while the game is running…

https://docs.unity3d.com/Manual/MultiSceneEditing.html

curb47

  • Full Member
  • ***
  • Posts: 219
    • View Profile
Re: New Scene / Store variables etc
« Reply #7 on: October 28, 2020, 03:30:55 PM »
Ahhhh... I see, thanks for enlightening me, wetcircuit.

curb47

  • Full Member
  • ***
  • Posts: 219
    • View Profile
Re: New Scene / Store variables etc
« Reply #8 on: October 28, 2020, 03:54:09 PM »
So, I've had some success... followed by, you guessed it, more failure.

I've got my Game Manager set up as described in a tutorial I found:



This all works fine, I've set it up to store 6 variables; 2 floats, 3 Ints and a Bool for 'docking'



I did a little test, and let the Mothership be attacked by aliens for a bit, reducing it's health to 12, and then I docked to see if the Health carried through, and it does.

You can see here the, Game Manager in the Mothership scene:


And the variables carried through...


When the game starts, the savedDocked bool is set to False, and when you dock it is set to True in the Game Manager:


All makes sense to me, I think.

The problem is, when I launch from the Mothership and go back to the main gameplay scene, the Game Manager has been duplicated. I don't understand why this is happening...



The original Game Manager still has the correct values (12 for Mothership health and Bool set to True) and the new duplicated one has everything set to the start values.

I used a Bool, so that when you re-launch from the Mothership, it can tell if you're starting the scene from New or you've just launched from Mothership.


I did a few docking/re-launch one after the other, and it's adding a new Game Manager every time I re-launch from the Mothership. I'm stumped.


What have I done that's causing the Game Manager to be duplicated? I thought if it had the Don't Destroy on Load action, then it will move from scene to scene.

Any help would, as ever, be greatly appreciated.

Many thanks.

J.

« Last Edit: October 28, 2020, 04:15:59 PM by curb47 »

LordHorusNL

  • Beta Group
  • Full Member
  • *
  • Posts: 215
    • View Profile
Re: New Scene / Store variables etc
« Reply #9 on: October 28, 2020, 04:01:57 PM »
Don't Destroy on Load is only responsible for making sure a Game Object isn't destroyed when loading another level. Nothing else.

To fix your duplication problem you need to turn your Game Manager into a singleton.

Just download the "Singleton Manager" action from the ecosystem and place it just after "Don't Destroy on Load" with GameManager or something as the reference. This way there will only ever be one instance of the Game Manger in your scene.

curb47

  • Full Member
  • ***
  • Posts: 219
    • View Profile
Re: New Scene / Store variables etc
« Reply #10 on: October 28, 2020, 04:17:06 PM »
oh my god! You're a star! Thank you!

curb47

  • Full Member
  • ***
  • Posts: 219
    • View Profile
Re: New Scene / Store variables etc
« Reply #11 on: October 28, 2020, 04:27:04 PM »
Yes, that worked. Sanity is restored!

Thanks again LordHorus.

curb47

  • Full Member
  • ***
  • Posts: 219
    • View Profile
Re: New Scene / Store variables etc
« Reply #12 on: October 28, 2020, 04:47:16 PM »
Oh blimey... now another weird problem!

The Game Manager doesn't duplicate itself any more, so all good on that front, however, I have the Game Manager set itself up as a global object variable, so it can have it's variables set from other FSMs when required. This works when the game starts, and also when docked into the Mothership:


However, when I re-launch from the Mothership back into the main gameplay scene, the Get Owner fails to set the Game Manager as a variable, so the saved variables on the Game Manager are not read by the corresponding FSMs.



I'm tired. Thanks again for any help.

J.

LordHorusNL

  • Beta Group
  • Full Member
  • *
  • Posts: 215
    • View Profile
Re: New Scene / Store variables etc
« Reply #13 on: October 28, 2020, 05:42:19 PM »
Try this

First thing I'd do is have Singleton manager, Don't Destroy on Load and a Wait action of 0.1 in the first state and Get Owner in a second state to make sure the correct variable is stored and to make sure any other instance of the Game Manger has time to destroy itself before trying to retrieve the global variable.

Also make sure you're not disabling the game object the manager is on or the FSM itself. It should only retrieve the global variable once after starting your game and then never enter that state again.

Edit:

After testing and having some problems myself, this is wat worked for me.
« Last Edit: October 28, 2020, 06:47:41 PM by LordHorusNL »

curb47

  • Full Member
  • ***
  • Posts: 219
    • View Profile
Re: New Scene / Store variables etc
« Reply #14 on: October 29, 2020, 01:49:57 AM »
HI LordHorus,

Thank you, I'll give your suggestion a shot and get back to you.

(EDIT) - All done! It works perfectly now, I'm really happy! Thank you!

J.
« Last Edit: October 29, 2020, 05:15:33 AM by curb47 »