Scenes
This C# Intermediate tutorial covers the concept of Scenes in Stride.
Explanation
Stride allows Scenes to have an infinite amount of child scenes which on their terms also can load an infinite amount of child scenes. However, every scene loaded is unique. A scene can not be loaded twice at the same time. Both the editor and when loading scenes through code, will prevent a scene from being loaded twice at the same time.
Loading a child scene
This script loads in a child scene by pressing a defined key. Pressing that same key again, will unload the loaded child scene. Every time we load the child scene again, we offset it a little in the Y direction to demonstrate the offsetting option for child scenes.
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using Stride.Core.Mathematics;
using Stride.Core.Serialization;
using Stride.Engine;
using Stride.Input;
namespace CSharpIntermediate
{
public class LoadChildScene : SyncScript
{
// We can load a scene by name, however if the scene would be renamed, this property would not update
//public string childSceneToLoad;
public UrlReference<Scene> childSceneToLoad;
private int loaded = 0;
private Scene loadedChildScene;
public override void Update()
{
DebugText.Print("Press C to load/unload child scene", new Int2(20, 60));
if (Input.IsKeyPressed(Keys.C))
{
if (loadedChildScene == null)
{
// loadedChildScene = Content.Load<Scene>(childSceneToLoad);
// Or
loadedChildScene = Content.Load(childSceneToLoad);
loadedChildScene.Offset = new Vector3(0, 0.5f * loaded, 0);
loaded++;
// Entity.Scene.Children.Add(loadedChildScene);
// Or
loadedChildScene.Parent = Entity.Scene;
}
else
{
// Entity.Scene.Children.Remove(loadedChildScene);
// Or
loadedChildScene.Parent = null;
Content.Unload(loadedChildScene);
loadedChildScene = null;
}
}
}
}
}
(Re)loading a scene
We can get the top most scene in our world which is called the RootScene. If we unload that scene, we can then load in a completely new scene in order to swap or switch to a new scene. That same script can also be used to reload the same scene in case you want to restart your scene,
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using Stride.Core.Mathematics;
using Stride.Core.Serialization;
using Stride.Engine;
using Stride.Input;
namespace CSharpIntermediate
{
public class LoadScene : SyncScript
{
public UrlReference<Scene> SceneToLoad;
public int DrawY = 20;
public string Info = "Info text";
public Keys KeyToPress;
public override void Update()
{
DebugText.Print($"{Info}: {KeyToPress}", new Int2(20, DrawY));
if (Input.IsKeyPressed(KeyToPress))
{
Content.Unload(SceneSystem.SceneInstance.RootScene);
SceneSystem.SceneInstance.RootScene = Content.Load(SceneToLoad);
}
}
}
}