Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Feedback NGO - change player NetworkObject

Discussion in 'Netcode for GameObjects' started by bentrewhella, Aug 7, 2023.

  1. bentrewhella

    bentrewhella

    Joined:
    Sep 4, 2014
    Posts:
    8
    There appears to be no way of changing the Player Network Object on the server once this has been spawned.
    SpawnAsPlayerObject uses Spawn Internal with a bool for player object, which ultimately sets the playerobject in connected clients, and for clients the LocalClient.PlayerObject, but there appears to be no simple way of changing this once set. Cannot workaround by setting these manually, because NetworkObject.IsPlayerObject is internal (without shenanigans anyway).
     
  2. Nyphur

    Nyphur

    Joined:
    Jan 29, 2016
    Posts:
    73
    The problem is that NGO mixes the concept of a player and a connected client in multiple locations, and various components use that as an assumption in their initialisation. Changing it would make certain IDs like NetworkObject LocalClientID wrong, so changing the actual NetworkObject associated with a client at runtime is inadvisable.

    If you need the functionality of changing a character at runtime, my suggestion would be to treat your player prefab as a container into which you can swap different objects. The simplest solution would be to have the character controllers and main colliders etc on the root player prefab and then have a series of sub-objects for the different characters that you toggle on/off using a manager script. The manager script would include a ServerRpc that the owner of the character can trigger, which then itself triggers a ClientRpc that informs all clients to swap objects. If you need to do object spawning or changing ownership or reparenting, that can all be done in the ServerRpc.

    If you have more specific details about what you're trying to achieve, It'd be easier to come up with a more specific solution.
     
  3. bentrewhella

    bentrewhella

    Joined:
    Sep 4, 2014
    Posts:
    8
    Yep I hear that,. and thanks for very fast answer! Unfortunately I have a single prefab that serves as an entity i.e. character, under that various methods instantiate avatars so each character can look different. But the overall idea is to allow players to take over each others characters, with data about what they're holding, inventory etc all remaining in place - otherwise I'd go to lengths of despawning and creating again. This is simple(ish) in Mirror, FishNet, PUN, and my system allows these backends to be interchanged easily. I can't see any way of doing this other than refactoring so I have a separate master player object, which never changes, and separate root individual entitites which can be taken over without affecting the LocalPlayer etc. Which to be fair is probably a better architecture for what I'm doing, but it's annoying it'll be triggered for one network stack. I'll prob move to such an architecture anyway, will just mark this as impossible in NGO for the minute. Thanks!
     
  4. Nyphur

    Nyphur

    Joined:
    Jan 29, 2016
    Posts:
    73
    It may be possible to swap the character ownership correctly at runtime with some effort, someone with more experience of this specific part of NGO could answer that.

    But in the absence of an easy way, I would suggest in this case possibly just faking everything. You could still have all the same characters and separate connected clients and player prefabs exactly as you have now, but then you create a specific method that swaps two characters. That method would handle teleporting the players to their respective locations, changing avatars, and exchanging inventory etc.

    It gets a bit more complicated if you want to do something where Player A takes over control of Player B but Player B is still watching his character being controlled. But you could do that by switching Player B's character and controls off and re-targeting its camera temporarily to Player A instead.
     
  5. bentrewhella

    bentrewhella

    Joined:
    Sep 4, 2014
    Posts:
    8
    Yep - many thanks for your help!