Issues With Multiple Worlds and Loading Scenes

Hi all,

I’m trying to create multiple worlds, and in the scene mark up which stuff goes in world A and/or world B. I’ve been taking an approach similar to what I saw in the Unity multiplayer github repo, which is:

Create an ICustomBootstrap class, which creates an additional world in the Initialize() function:

using System;
using System.Collections.Generic;
using Unity.Entities;

public class WorldManager : ICustomBootstrap
{
    public static World[] worlds;

    public List<Type> Initialize(List<Type> systems)
    {
        if (worlds != null)
        {
            return systems;
        }

        worlds = new World[2];
        worlds[0] = World.Active;

        World world = new World("Second World");
        worlds[1] = world;
        // second worlds systems will be created here

        return systems;
    }
}

Then I create a “WorldA” subscene, a “WorldB” subscene, and a “Common” subscene, unticking auto load scene. I attach the following script to each subscene:

using UnityEngine;
using Unity.Entities;
using Unity.Scenes;

public class SubSceneWorld : MonoBehaviour
{
    public int[] worlds;

    private void OnEnable()
    {
        SubScene subScene = GetComponent<SubScene>();

        subScene.AutoLoadScene = true;
        World previouslyActiveWorld = World.Active;

        foreach (int world in worlds)
        {
            World.Active = WorldManager.worlds[world];
            subScene.UpdateSceneEntities();
        }

        subScene.AutoLoadScene = false;
        World.Active = previouslyActiveWorld;
    }
}

Is this the correct way to load a scene into a specific world?

I can see the stuff in the WorldA subscene (which is for world[0], the default world), but WorldB and Common I can’t see. I understand why I can’t see the stuff in WorldB because I haven’t added any systems to it yet, but why can’t I see the Common subscene objects?

Cheers
J

1 Like

Updated post with most recent progress, hopefully someone has an idea what I’m getting wrong.

I’ve been trying to figure this out for days now, no Unity people who can help me even?

What do you mean with ‘see’?

I can see them in the game viewport

You can or you can’t? If you can’t, it’s because they’re not in the main world.

As I said in the original post, I have 3 subscenes and 2 worlds. I’m trying to load scene 1 into world 1, scene 2 to world 2, and scene 3 to worlds 1&2. I can see the scene 1 contents but not scene 3. I can’t see scene 2 but I don’t expect to yet (it doesn’t have any systems). This is an approach used in the unity multiplayer GitHub repo but I can’t get it to work.

What you’re doing does not work.

When you call UpdateSceneEntities() it destroys all existing entities that were created previously before creating them again.

Your approach works on loading to a specific world but it does not allow loading to multiple worlds at same time. The SubScene script by default only allows loading into a single world. You would need multiple SubScene scripts.

Thank you!

So I guess the stuff in the unity multiplayer doesn’t work then, or maybe requires a future release of the entities package

For reference, this https://github.com/Unity-Technologies/multiplayer/blob/master/sampleproject/Assets/NetCode/Authoring/ClientServerSubScene.cs

The multiplayer package is very out of date, it uses Unity 0.0.12-preview.33

There is meant to be an update coming soon~

Is there a recommended way to load a scene or subscene to specific (and potentially multiple) worlds?

I think this is the recommended way to loading it to a specific world.

As for multiple worlds you could potentially have more than 1 subscene component pointing to the same scene or maybe better clone it per world at runtime.

I’d probably just load it into into a single world (probably a staging world) then clone the entities it into different worlds.

But I’ve never tried or really looked into it so no idea if it’d work.

1 Like

To load a subsene in a specific world you can look at this but I don’t think it works with the current entities version so you’ll have to wait a bit: Load a subscene to a specific world ?_ga=2.18677689.1034887855.1572877018-2065661543.1556050628#post-5026634

If you want to then split object between multiple worlds you’ll need to use this: Class EntityManager | Package Manager UI website

Note that I managed to have multiple worlds with renderer working but static RenderMesh won’t show after the move. The frozen render mesh update starts by checking GetComponentOrderVersion wich is odd because FrozenRenderSceneTag is a shared component and should not be covered by GetComponentOrderVersion

1 Like

Yeah cloning the entities sounds like a good shout, thanks

Loading into a staging world had some issues for me - basically I couldn’t figure out when the scene has finished loading because the StreamingState component used by the SubSceneStreamingSystem is inaccessible. I guess I’ll try multiple subscene scripts unless anyone knows a way around that?

Ah, and multiple instances of the same subscene script doesn’t work because there’s a static list of subscenes in SubScene.cs so it detects that I’m trying to do this and logs a warning

Trying the same thing with mixed subscene for client and server, and bumped into same problem^^ Hopefully next release will fix it.

1 Like