Search Unity

Question Adding runtime MonoBehaviours to NetworkObjects?

Discussion in 'Netcode for GameObjects' started by UnkelRambo, Mar 17, 2022.

  1. UnkelRambo

    UnkelRambo

    Joined:
    Oct 26, 2012
    Posts:
    80
    Hi!

    I'm curious if there's a "relatively sane" way to add MonoBehaviours to networked GameObjects at runtime?

    I tested this several months ago before using MLAPI for this project I'm working on, and my test was flawed which led me to believe this was relatively simple. Doesn't seem to be the case, however...

    My exact scenario is this:

    1. I have a huge database of objects and "traits" which can be added to instantiated objects, giving them unique behaviors.
    2. The behavior definitions are ScriptableObjects that may have an associated "state" MonoBehaviour given to the GameObject if the trait is added at runtime (think "Burning" trait which adds a simple timer for how long the object should burn.)
    3. The runtime added "state" MonoBehaviours are *currently* being handled via RPC, but that won't fly long term as I need to support join-in-progress.

    My current plan is to move the "state" data into a single MonoBehaviour that has several NetworkDictionarys for types I want to support. Not ideal, as I'd like to support quite a lot of objects in the world and I'm concerned about the added networking overhead of an integer GUID key per property, but it'll work.

    Any suggestions?

    Thanks!

    PS. As an added bonus, is there a preferred way to pass ScriptableObjects to RPC's or use with NetworkVariable? My plan here is to write a small wrapper struct to serialize the ScriptableObject's GUID and look that up. Curious if This Is The Way...
     
  2. cosminunity

    cosminunity

    Joined:
    Mar 4, 2021
    Posts:
    14
    Hey @HAMMERWANG,

    Just to confirm, you said you want to add MonoBehaviour's at runtime to a NetworkedObject, I think that is doable. But did you perhapes meant a NetworkBehaviour? If yes, then adding/registering NetworkBehaviours at runtime is not currently supported in this release. I am sorry for the inconvenience.

    Greetings,
    Cosmin
     
  3. UnkelRambo

    UnkelRambo

    Joined:
    Oct 26, 2012
    Posts:
    80
    Indeed and thank you for understanding. Precision of language ^_^

    I saw that adding NetworkBehaviours is not supported, I'm really trying to find a decent pattern to achieve something like this, and what I've gotten to so far is actually reasonably OK but I have to finish testing.

    The workaround I'm doing so far looks like this:

    1. Each object definition in the database has its list of "traits" associated with each object.
    2. When an object definition is spawned into the world, each "trait" spawns its own "trait state" object.
    3. The "trait state" object has a ulong that's the network ID of the parent object that wires up the transform parenting when replicated to clients.

    This *seems to work*, but I'm concerned about the overhead. My load test map spawns 1000 objects, and each has dozens of traits so far. Dynamically adding dozens more is quickly bloating the number of game objects in my test scene, but profiling is needed before I can know for sure whether it's an issue.

    I think what I'll end up doing is rolling "state manager" objects instead, so each "trait state" will be created and a list of objects with said traits will be managed by the server and replicated. This is kind of nice because I already have a couple of systems that require this behavior because I'm running some sim stuff using Jobs/ECS.

    We'll see how this goes, thanks for the response in the meantime!