Manually loading additive scenes

Announcements, support questions, and discussion for Save System for Opsive Character Controllers.
Post Reply
christianwiele
Posts: 29
Joined: Sat Mar 27, 2021 7:34 am

Manually loading additive scenes

Post by christianwiele »

Hi,

I am working on a 2-player online game, and I need a save system that allows a player to reconnect upon dropping out. I load all my scenes additively through a custom level loader. I have a bootstrap scene, a game scene (containing the players, dialogue system), and as third layer the level containing everything level specific. The game scene is loaded after the lobby being always active, and the level scenes are switched dynamically.

Now my question. I need to save the dialogue system, the UIS/UCC inventory from the game scene, and some game object states from the level scene. Is this possible with the save system, even if I don't let the save system load the additive scenes?

Thanks, Christian
User avatar
Tony Li
Posts: 22149
Joined: Thu Jul 18, 2013 1:27 pm

Re: Manually loading additive scenes

Post by Tony Li »

Hi Christian,

Does one player's machine act as the host/server? Is this where you're saving and loading? Does it hold an authoritative copy of the world state, or is the world state shared peer-to-peer?

The save system can work with multiplayer games. It just saves and restores data (and associated processes). Your multiplayer implementation will need to handle syncing on top of that.
christianwiele
Posts: 29
Joined: Sat Mar 27, 2021 7:34 am

Re: Manually loading additive scenes

Post by christianwiele »

I am using Photon PUN, which is more peer-to-peer, and I am taking care of the sync. The master will trigger the save, but each player will have its local persistence to avoid sending all the data over the network. I will also take care about scene loading. I am just nor sure how the save system handles objects in additive loaded scenes.
User avatar
Tony Li
Posts: 22149
Joined: Thu Jul 18, 2013 1:27 pm

Re: Manually loading additive scenes

Post by Tony Li »

PixelCrushers.SaveSystem.currentSavedGameData is a serializable object. You can serialize it as a string and send it to whatever client has just connected.

If you load and unload scenes using PixelCrushers.SaveSystem.LoadAdditiveScene() and UnloadAdditiveScene(), then the save system will automatically save and restore save data in those scenes.

If you don't use those methods, you'll have to do it manually:

Before unloading an additive scene, tell its savers to record their states, and then inform them that the scene will be unloaded. For each saver in the scene, call:

Code: Select all

int saverSceneIndex = saver.saveAcrossSceneChanges ? SaveSystem.NoSceneIndex : currentSceneIndex;
SaveSystem.currentSavedGameData.SetData(saver.key, saverSceneIndex, saver.RecordData());
saver.BeforeSceneChange();
After loading an additive scene, tell its savers to retrieve their states from the current saved game info:

Code: Select all

var rootGOs = scene.GetRootGameObjects();
for (int i = 0; i < rootGOs.Length; i++)
{
    SaveSystem.RecursivelyApplySavers(rootGOs[i].transform);
}
Post Reply