Custom implementation of SpawnedObjectManager [SOLVED]

Announcements, support questions, and discussion for Save System for Opsive Character Controllers.
Post Reply
robloxillian
Posts: 27
Joined: Mon Sep 13, 2021 6:55 pm

Custom implementation of SpawnedObjectManager [SOLVED]

Post by robloxillian »

I created a subclass of SpawnedObjectManager and am using it to manage created objects. I'm running into two problems:

1. My subclass object is automatically loading spawned objects at the start of the game, but I want to copy them from a special "new game" scene instead. Alternately, if there's an easy way in the editor to control and adjust the objects it loads, that would work. I set breakpoints and it looks like the method being called is

Code: Select all

SaveSystem.RecursivelyApplySavers()
. I've unchecked all the checkboxes I can find related to automatically loading on start, so I'm not sure how to prevent this.

2. The objects in the "new game" scene aren't automatically added to the spawned object's list of spawned objects. This is a bit confusing to me, since if I create new objects of the same type while the game is running, they do get added to the list of spawned objects. I don't think I'm doing anything unusual when I add them, other than maybe calling

Code: Select all

GameObject.Instanitate()
.

Sorry if this isn't enough information to diagnose the issue. I'm hoping it will be as simple as changing a setting somewhere for the first issue and adding a line of code in the

Code: Select all

Start()
method for the spawned objects to solve the second issue.
Last edited by robloxillian on Sat Sep 25, 2021 2:41 pm, edited 1 time in total.
User avatar
Tony Li
Posts: 22200
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom implementation of SpawnedObjectManager

Post by Tony Li »

Hi,

SaveSystem.RecursivelyApplySavers() is only used when you're additively loading scenes. If you're additively loading your "new game" scene, can you put the SpawnedObjectManager in the "new game" scene?

(Reminder: Each scene should have its own SpawnedObjectManager if it needs to manage spawned objects, and each SpawnedObjectManager should have a unique key.)

Make sure a SpawnedObjectManager is present before instantiating SpawnedObject objects.

If that doesn't help, please let me know how you've set up the flow of what scenes get loaded and when.
robloxillian
Posts: 27
Joined: Mon Sep 13, 2021 6:55 pm

Re: Custom implementation of SpawnedObjectManager

Post by robloxillian »

I tried moving the SpawnedObjectManager to NewGameScene, but I'm still seeing the problem-- I think this time it's happening when I call SaveSystem.LoadScene(). I did find out a bit more about what's happening, though: wherever it's loading from when calling RecursivelyApplySavers(), it's getting updated whenever I change the position of spawned plants in NewGameScene. This means it's not just loading them from a saved file, but making clones of the plants in NewGameScene.

Still, I should always be using SaveSystem.LoadAdditiveScene() if I want to register savers when loading scenes additively, right? Or is there a way to get it to register existing objects rather than creating clones of them?

There's also a new problem I noticed-- only some plants get cloned/registered properly. I have different "types" of plants (thorn, fern, etc), with each type represented by a prefab. When I add a second fern, only the first fern gets cloned and registered by the SpawnedObjectManager. I changed the second's SpawnedObject key so it's not identical to the first, but that didn't fix it. I'm hoping this will be fixed by proxy when I figure out how to register the existing objects but I thought it was worth mentioning.

I have experimented with different scene load orders but right now I have:

Code: Select all

SaveSystem.LoadScene("NewGameScene"); // where the starting plants and spawned object manager are
I've also tried this to make sure the SpawnedObjectManager is initialized before the plants, but I get the same result:

Code: Select all

SaveSystem.LoadScene("Manager"); // where the SpawnedObjectManger is
...
SaveSystem.LoadSceneAdditive("TileScene"); // where starting plants are
It seems like it's close to working, but I suspect my use of SaveSystem.LoadSceneAdditive() or my subclass of SpawnedObjectManager aren't quite right for what I'm trying to do. I just want to load NewGameScene additively and register (not clone) the objects there, or if I do clone them, do so in a way that lets me add new ones via the editor, and reliably delete the originals after cloning.
User avatar
Tony Li
Posts: 22200
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom implementation of SpawnedObjectManager

Post by Tony Li »

Hi,

I don't think I have a clear understanding of your goal here. Would you mind if we take it from the top?

Without concerning ourselves with how to implement it, what do you want to accomplish? What is the "new game" scene? Is it used only for new games, or also when loading a saved game? In total, what scenes need to be loaded, and what should be in each scene?
robloxillian
Posts: 27
Joined: Mon Sep 13, 2021 6:55 pm

Re: Custom implementation of SpawnedObjectManager

Post by robloxillian »

Sorry, I was getting ahead of myself. I want to use the save system to achieve this behavior:

- From the title screen, the player can start a new game or load a saved game. In the normal sense, there is only one "slot" for a saved game.
- However, I would like there to be separate save states for each ingame "day" that lasts about 15 minutes. The game starts on day 1 and ends on some day I will set as a constant. The day-specific saves are like separate slots that the player can load from.
- The player can return to a specific day by interacting with a calendar menu. This removes the ability to return to any days after that one-- this is only possible by playing out the intervening days.
- At the end of each day, whether the player goes to sleep or stays up until midnight, there is a fade-out cutscene and the game advances to the next day. This means the transition does not need to be "smooth"-- for example, objects like the player that move around get teleported back to their start locations.
- So far, the only objects whose save states I want to manage are plants. Plants grow slightly each day, changing their sprites depending on their age, and spawn new plants. (This is what I was using a SpawnedObjectManager subclass for.)
- The NewGameScene is intended to set the locations of all of the plants that exist on day 1, but all other plants should either be loaded from saves or spawned by existing plants.

Altogether, the main scenes I want to manage are the title scene where the game starts, the new game scene where the initial plants (and other objects I add in the future) are stored, and the "tile scene" that can be reloaded either from the title screen or from the calendar.
User avatar
Tony Li
Posts: 22200
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom implementation of SpawnedObjectManager

Post by Tony Li »

When the player starts a new game from the title scene, will it unload the title scene, and then load the new game scene and tile scene additively so both are loaded at the same time?

And if the player choose a saved game from the title scene, I assume it will do the same -- unload title scene, load new game and tile scenes -- and then apply the saved game data?

If that's correct, I recommend not treating the initial plants in the new game scene as spawned objects. If they can disappear during play, add DestructibleSavers to them, or use a [Multi]ActiveSaver component to track their active/inactive states.

Use the spawned object system only for plants that are spawned by existing plants during play.

Alternatively, you may find it easier to forgo the spawned object system entirely, and track all of your plants in a script. You can make this script a subclass of Saver, or write a second script that's a Saver to save the data in the script. The spawned object system is best for objects that don't change their internal states during play, so they can always be recreated from a prefab without having to keep track of individual differences on different spawned objects of the same type.
robloxillian
Posts: 27
Joined: Mon Sep 13, 2021 6:55 pm

Re: Custom implementation of SpawnedObjectManager [SOLVED]

Post by robloxillian »

Thank you! I ended up getting everything working by using a custom saver rather than SpawnedObjectManager, though I did use a lot of the same code as SpawnedObjectManager. I think the main issue was on the end of the Plants being SpawnedObjects and not registering themselves in a way that worked with my specific use case, but handling how they get registered myself made it possible to reload them.
User avatar
Tony Li
Posts: 22200
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom implementation of SpawnedObjectManager [SOLVED]

Post by Tony Li »

Glad to help! This is a perfect use case for why the save system is expandable with your own saver components. I'm glad you have it working now.
Post Reply