Last active
October 29, 2020 03:23
-
-
Save James-Frowen/4c784ad4122c0219c59ca4d4abf5a778 to your computer and use it in GitHub Desktop.
Reference to NetworkBehaviour that is not currently spawned on client (useful for synclists)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// player script (in its own file) | |
public class Player : NetworkBehaviour { } | |
// reference to Player that can be sent over network | |
public struct PlayerReference | |
{ | |
public uint netid; | |
private Player _player; | |
public PlayerReference(Player value) : this() | |
{ | |
_player = value; | |
if (value != null) | |
netid = value.netId; | |
} | |
public Player Player | |
{ | |
get | |
{ | |
if (_player == null) | |
{ | |
NetworkIdentity identity = NetworkIdentity.spawned[netid]; | |
if (identity != null) | |
{ | |
_player = identity.GetComponent<Player>(); | |
} | |
} | |
return _player; | |
} | |
} | |
public static implicit operator Player(PlayerReference reference) => reference.Player; | |
public static implicit operator PlayerReference(Player value) => new PlayerReference(value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment