Playing and Stopping Scenes: Techal Game Engine Series

Welcome back to the Techal Game Engine Series! In our previous episode, we discussed UUIDs and how to integrate them into the engine. Today, we are going to dive into a fundamental aspect of game engines: playing and stopping scenes.

Playing and Stopping Scenes: Techal Game Engine Series
Playing and Stopping Scenes: Techal Game Engine Series

Introducing Runtime Scenes

Up until now, we have only been simulating an existing scene. However, in reality, we want to load and play a scene that we have been editing in a runtime state. So, what exactly does that mean?

When we hit play, we want the scene to load in a runtime fashion. This means that the scene’s physics, assets, and other components should be activated and ready for interaction. When we hit stop, we want to reset everything back to its original state and return to the editor scene.

Copying Scenes and Architecture

To achieve this behavior, we need to copy the editor scene into a new scene. This new scene will be our runtime scene, which we will set as the active scene during gameplay. When we hit stop, we will switch back to the editor scene as the active scene.

To copy the scene, we need to duplicate all the entities and their components from the editor scene to the runtime scene. We can achieve this by iterating through each entity in the editor scene, creating a new entity in the runtime scene, and copying the components from the corresponding entities in the editor scene.

Copying Components

To copy the components, we need to iterate through each component type in the editor scene and duplicate them in the runtime scene. This process involves creating a view of the component type, retrieving the component data from the source registry, and adding or replacing the components in the destination registry.

Further reading:  Fixing Mouse Behavior in the Path Tracer

Duplicating Entities

In addition to copying scenes, we also want to be able to duplicate individual entities within a scene. This allows us to easily create variations or repetitions of objects in our game. We can achieve this by creating a duplicateEntity function that duplicates an entity’s components and creates a new entity with the same properties.

Saving Scenes

To ensure that our hard work is not lost, we need to implement a way to save our scenes. We can add a “Save Scene” feature that saves the active scene to a file. This can be done by serializing the scene data and writing it to a file using a file path. Additionally, we can add a “Save Scene As” option that allows us to specify a new file path for the saved scene.

Conclusion

In this episode of the Techal Game Engine Series, we explored the concepts of playing and stopping scenes in a game engine. We learned how to copy scenes and duplicate entities to create a dynamic and interactive gameplay experience. Additionally, we implemented a feature to save our scenes, ensuring that our work is always preserved.

Stay tuned for the next episode where we will continue to enhance our game engine with more exciting features!

YouTube video
Playing and Stopping Scenes: Techal Game Engine Series