BoltEngine / Bolt-Tracker

New issue tracker for Photon Bolt
10 stars 2 forks source link

Bolt Entites are not attached if they're scene entities. #183

Open Martin-PixelToys opened 3 years ago

Martin-PixelToys commented 3 years ago

Describe the bug Bolt Entites are not attached if they're scene entities.

To Reproduce Steps to reproduce the behavior:

  1. Open the attach repro project.
  2. Ensure that the 'Sample Scene' and 'Foo' scene is open in the Unity Editor.
  3. Enter Play Mode
  4. Notice that there are only 2 "Attached" logs in the console window.

Expected behavior I expected to see 6 logs, from 3 different instances (2 from scene objects, 1 from a runtime instanced prefab).

Actual behavior I see only 2 logs, and only from the runtime instanciated prfab.

Repro project Here's a repro project: https://drive.google.com/file/d/1unvCd-Uj9EX7a_fNKcuVfH8EyAy86fer/view?usp=sharing

Desktop (If applicable, please complete the following information):

Additional context

Martin-PixelToys commented 3 years ago

Currently, we work around this issue by always instanciating via BoltNetwork.Instantiate. However, this is a major nuisance, since we can't keep Scene References inside Prefabs. Therefore, we have to make large refactorings when networking-up systems that interact with Scene Objects.

Martin-PixelToys commented 3 years ago

We have found a way to 'fix' things:

  1. Open both scenes up in Editor
  2. Bolt > Generate Scene Object IDs
  3. Bolt > Compile Assembly

After doing that, 6 logs are printed as expected.

However, this was quite unexpected, since the repro project appears to have valid scene IDs and prefab IDs, even before we go through these steps. Also, we're worried that these issues may creep into our builds, potentially causing disasterious multiplayer issues. Therefore, we're considering adding additional build steps to ensure that 'Generator Scene Object IDs' has been called for every scene in the project, followed by a 'Compile Assembly'.

Please advice if this is something that is necessary, since it would add considerable time to our build pipeline.

ramonmelo commented 3 years ago

Hi @Martin-PixelToys ,

Yes, Generate Scene Object IDs is a necessary step if you are duplicating your Scene Objects and not including each one of them from the Project tab into your Scene.

When you duplicate a Scene Object already in the scene, it also duplicates the Scene Object ID, and running the Generate Scene Object IDs either fix non-existent IDs and fixes the duplicate ones.

Using scene objects is not recommended, as they can lead to such cases, so we always recommend using object spawners, like it's shown in the Getting Started sample, for example.

Link: https://github.com/BoltEngine/Bolt-Sample/blob/master/GettingStarted/Scripts/InteractiveSpawner.cs

Martin-PixelToys commented 3 years ago

@ramonmelo For our use-cases we normally drag prefabs in from the Project tab. In our actual production project, we find that this usually causes Attach() to not be called, unless we use workarounds such as spawners, or manually calling Attach() inside our scripts.

We'd be okay with adding some Editor scripting that forces Generate Scene Object IDs to be generated after saving a Scene, since this is a pretty fast operation, and being able to wire-up network objects directly in the scene is quite powerful, since it's possible to reference other objects in the scene directly, but this doesn't seem to fix our particular issue.

Martin-PixelToys commented 2 years ago

Hi @ramonmelo

I've managed to replicate the issues that we're seeing in our production project.

To Reproduce

  1. Download this repro project: https://drive.google.com/file/d/1D1DtQS-Sm6k_l1CCtrz65dbOzGNfOn6U/view?usp=sharing
  2. Open the project, and open the 'SampleScene'
  3. Enter play Mode

Expected behavior I expected to see 6 logs, from 3 different instances (2 from scene objects, 1 from a runtime instanced prefab).

Actual behavior I only see 4 logs. Base GO 2 from Scene 'Foo' that was loaded after calling BoltLauncher.StartSinglePlayer() never gets attached.

From what I can tell, it looks like Bolt won't call Attach() on SceneObjects that belong to scenes loaded after Bolt has been launched.

Martin-PixelToys commented 2 years ago

Using BoltNetwork.LoadScene instead of SceneManager.LoadScene appears to make the Attach() not called issue go away, although it causes other issues for us in our production project, since we rely heavily on Additively Loading Scenes, which BoltNetwork.LoadScene doesn't seem to support.

ramonmelo commented 2 years ago

Hi @Martin-PixelToys ,

You must use Bolt API in order to keep things in sync, in this case, using BoltNetwork.LoadScene will make your clients be always in sync with the server when it loads a new Scene. And yes, one of the extra tasks of loading scenes using Bolt API is the auto attach for scene objects. You can disable the Scene Auto Sync via the Bolt Settings (https://doc-api.photonengine.com/en/bolt/current/class_photon_1_1_bolt_1_1_bolt_config.html#af93c78289aa884e890244e9e822de111), if you want.

Unfortunately, Photon Bolt does not support additive scene loading, and if you need it, you will also need to manage other details manually, which includes either spawning the Entities when the scene is loaded or attaching them manually.

We also provide a Sample for Multi Scene loading here: https://github.com/BoltEngine/Bolt-Sample/tree/master/MultipleScenes Maybe you can use it as a base to implement your multi-scene project and include callbacks to spawn the entities, for example.