Feature: Add SceneSpawner #502
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reopened this pull request to its own branch.
Real life use case:
Imagine a fully server authoritative game. There are static nodes on the world which does not need constant updates with any synchronizer node. We may want some of these static nodes to have initial values such as global_transform, custom timers, any custom property. We may want to control spawn/despawn logic per peer to prevent vision hacks. We dont want to maintain custom serializable data structs or assignments just to initialize some values on scenes.
Problems:
Implementation:
SceneSpawner:
This node is designed to use on host application.
Usage
Replicate on spawn -> If true will broadcast scene spawn on all peers.
Replicate on despawn -> If true will broadcast scene despawn on all peers.
Notes: To allow spawning/despawning on specific peers SceneSpawner exposes 2 functions.
1- spawn_on_peer
2- despawn_on_peer
SceneSpawner will automatically despawn scene from instantiated remote peers by keeping internal list of them as well since this is the desired behavior for most games.
NetworkSceneSpawner:
This singleton listens despawn / spawn rpcs.
Notes: Loading scenes on runtime frequently is not good for performance reasons therefore singleton exposes set_preloaded_scenes(scenes : Array[PackedScene]) function. Scenes can be loaded when the game started or preloaded and passed to the singleton to avoid drops in the frame rate.