#How do I get a reference to a specific World from a MonoBehaviour
1 messages · Page 1 of 1 (latest)
Is this during baking (before pressing play) or during runtime?
during runtime
Short story is I want to use PlayerInputManager's automatic local multiplayer handling to create stub MonoBehaviours and then create entities associated with them to handle gameplay in ECS.
foreach (World w in World.All)
{
// Find the world you want here.
}```
THat's the best way? Just by name or something?
THere's no way to get it from the SubScene component for example?
Subscenes really only exist during bake. At runtime, all subscenes dump their entities into a single world. They might have some reference back to the original subscene they were created in but that connection isn't enforced.
And depending on the subscene, they may be copied across multiple worlds. Like multiple client worlds.
What am I looking at here? I'm using Netcode so maybe tthat's complicated things
I want things inside the "Shared Data" world
but if I use World.DefaultGameObjectInjectionWorld they end up in the outer list
Is that a world? Or maybe I'm confusing world with a parent/child relationship or something
Yeah, the default injection world isn't the actual gameplay world when using netcode. Netcode creates client and server worlds (depending on your setup) where the entities actually live.
Oh right so maybe I need to get the world from netcode?
If you have thin clients enabled, you can have multiple shared data subscenes.
What you want to do is use that foreach above, then each world:csharp foreach (World w in World.All) { // Find the world you want here. if ((w.Flags & WorldFlags.GameClient) != 0) // World is a client world, contains shared data subscenes. Do stuff. w.EntityManager.CreateEntity() // Stuff. }
I see - ok let me give this a shot.
Would it be generally safe to cache this within the context of a network session?
maybe I'm prematurely optimizing, let me see if it works first
The world itself? Probably... maybe. Worlds change very rarely but it's possible the world reference can get invalidated (think thin client destruction).
yeah alright. I'll not worry about it for now
Is there a reason you're using monobehaviors and not SystemBase?
I can't imagine looping over all the worlds is very performance heavy
PlayerInputManager only works in MonoBehaviour world
afaik
Not at all, at most you can have about 128 worlds (quantity may have been bumped in recent netcode patches). Iteration is negligable.
I could probably have the entity creation happen from a SystemBase though
The structural changes (creation of entities) will be the main problem