#How do I get a reference to a specific World from a MonoBehaviour

1 messages · Page 1 of 1 (latest)

neon escarp
#

I'm using a MonoBehavior to handle some things and I want to have this MonoBehaviour create entities in my subscene. How can I get the correct World to spawn the entities in from my MonoBehaviour?

Also if this is an antipattern what's a better solution to have the GameObject scene interact with the subscene/World?

pale ingot
#

Is this during baking (before pressing play) or during runtime?

neon escarp
#

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.

pale ingot
#
foreach (World w in World.All)
{
    // Find the world you want here.
}```
neon escarp
#

THat's the best way? Just by name or something?

#

THere's no way to get it from the SubScene component for example?

pale ingot
#

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.

neon escarp
pale ingot
#

And depending on the subscene, they may be copied across multiple worlds. Like multiple client worlds.

neon escarp
#

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

pale ingot
#

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.

neon escarp
#

Oh right so maybe I need to get the world from netcode?

pale ingot
#

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. }

neon escarp
#

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

pale ingot
#

The world itself? Probably... maybe. Worlds change very rarely but it's possible the world reference can get invalidated (think thin client destruction).

neon escarp
#

yeah alright. I'll not worry about it for now

pale ingot
#

Is there a reason you're using monobehaviors and not SystemBase?

neon escarp
#

I can't imagine looping over all the worlds is very performance heavy

neon escarp
#

afaik

pale ingot
#

Not at all, at most you can have about 128 worlds (quantity may have been bumped in recent netcode patches). Iteration is negligable.

neon escarp
#

I could probably have the entity creation happen from a SystemBase though

pale ingot
#

The structural changes (creation of entities) will be the main problem

neon escarp
#

SystemBase can reference GameObject world yeah?

#

as long as I stick to the main thread?

pale ingot
#

Yeah. You have access to GameObject.Find<> in systembase

#

yep