How does one save state between scenes in Xenko?
A bit more details might get you a decent response
Are you talking about saving game progress to disk or like transferring player between levels? Or perhaps both?
In memory ofcourse, indeed for example like transferring player data between scenes.
Could this be achieved by having a main scene that uses child scenes for the game states?
Using child scenes is an option and might make it easier to do some things. And you could do things like fancy transitions etc. But you can do it without child scenes. I took the TopDownRPG demo and added a Gate that loads a new level on collision trigger. The code below could be made simpler if the scenes were setup differently (see comments).
protected void LoadNextScene(Entity playerEntity, string nextSceneName, string nextNavMeshName)
{
//should validate arguments...
//Do it async :D
Func<Task> loadScene = async () =>
{
var currentScene = SceneSystem.SceneInstance.Scene;
// Load next scene and whatever else you need.
// If navmesh was nested under a different entity would make this simpler
var nextScene = await Content.LoadAsync<Scene>(nextSceneName);
var nextNavMesh = await Content.LoadAsync<NavigationMesh>(nextNavMeshName);
//remove player from current scene
currentScene.Entities.Remove(playerEntity);
//make appropriate changes to player
playerEntity.Get<PlayerController>().HaltMovement();
playerEntity.Transform.Position = Vector3.Zero;
playerEntity.Get<NavigationComponent>().NavigationMesh = nextNavMesh;
//add player to next scene
nextScene.Entities.Add(playerEntity);
// hook up camera to composition
// this could be simpler if camera wasn't nested under player
var camera = playerEntity.FindChild("Camera")?.Get<CameraComponent>();
(nextScene.Settings.GraphicsCompositor as SceneGraphicsCompositorLayers).Cameras[0].Camera = camera;
//set the next scene
SceneSystem.SceneInstance.Scene = nextScene;
};
Script.AddTask(loadScene);
}
Hope that helps. FYI these sort of questions are probably best asked on the Q&A Site