Photon Fusion 2.0.0

Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Properties | List of all members
NetworkBehaviour Class Reference

Base class for Fusion network components, which are associated with a NetworkObject. More...

Inherits SimulationBehaviour, ISpawned, and IDespawned.

Inherited by HitboxRoot, NetworkMecanimAnimator, and NetworkTRSP.

Classes

struct  ArrayReader
 Provides a reader for network arrays of type T. More...
 
struct  BehaviourReader
 Provides a reader for network behaviours of type T. More...
 
class  ChangeDetector
 Change detector for a NetworkBehaviour. More...
 
struct  DictionaryReader
 Provides a reader for network dictionaries with keys of type K and values of type V. More...
 
struct  LinkListReader
 Provides a reader for network linked lists of type T. More...
 
struct  PropertyReader
 Provides a reader for properties of type T in a network behaviour. More...
 

Public Member Functions

virtual void CopyBackingFieldsToState (bool firstTime)
 Copies the backing fields to the state. This method is meant to be overridden in derived classes.
 
void CopyStateFrom (NetworkBehaviour source)
 Copies entire state of passed in source NetworkBehaviour
 
virtual void CopyStateToBackingFields ()
 Copies the state to the backing fields. This method is meant to be overridden in derived classes.
 
virtual void Despawned (NetworkRunner runner, bool hasState)
 Called before the network object is despawned.
 
override void FixedUpdateNetwork ()
 Fixed update callback for networked behaviours.
 
ArrayReader< T > GetArrayReader< T > (string property)
 Gets an ArrayReader for a network array of type T.
 
BehaviourReader< T > GetBehaviourReader< T > (string property)
 Gets a BehaviourReader for a network behaviour of type T.
 
ChangeDetector GetChangeDetector (ChangeDetector.Source source, bool copyInitial=true)
 Creates a ChangeDetector for this network behaviour.
 
DictionaryReader< K, V > GetDictionaryReader< K, V > (string property)
 Gets a DictionaryReader for a network dictionary with keys of type K and values of type V.
 
T? GetInput< T > ()
 
bool GetInput< T > (out T input)
 Returns true if it a valid INetworkInput can be found for the current simulation tick (Typically this is used in FixedUpdateNetwork).
 
LinkListReader< T > GetLinkListReader< T > (string property)
 Gets a LinkListReader for a network linked list of type T.
 
int GetLocalAuthorityMask ()
 Gets a bitmask of AuthorityMasks flags, representing the current local authority over this NetworkObject.
 
PropertyReader< T > GetPropertyReader< T > (string property)
 Gets a PropertyReader for a property of type T in this network behaviour.
 
ref T ReinterpretState< T > (int offset=0)
 Allows read and write access to the internal state buffer.
 
void ReplicateTo (PlayerRef player, bool replicate)
 Controls if this network behaviours state is replicated to a player or not.
 
void ReplicateToAll (bool replicate)
 Sets the default replicated state for this behaviour, this by default is true so calling ReplicateToAll(true) does nothing if ReplicateToAll(false) hasn't been called before

 
void ResetState ()
 Resets the state of the object to the original state.
 
virtual void Spawned ()
 Post spawn callback.
 
bool TryGetSnapshotsBuffers (out NetworkBehaviourBuffer from, out NetworkBehaviourBuffer to, out float alpha)
 Tries to get the snapshot buffers for this network behaviour.
 
- Public Member Functions inherited from SimulationBehaviour
virtual void FixedUpdateNetwork ()
 Fusion FixedUpdate timing callback.
 
virtual void Render ()
 Post simulation frame rendering callback. Runs after all simulations have finished. Use in place of Unity's Update when Fusion is handling Physics.
 
- Public Member Functions inherited from Behaviour
AddBehaviour< T > ()
 Wrapper for Unity's GameObject.AddComponent()
 
GetBehaviour< T > ()
 Wrapper for Unity's GameObject.GetComponentInChildren()
 
bool TryGetBehaviour< T > (out T behaviour)
 Wrapper for Unity's GameObject.TryGetComponent()
 
void Spawned ()
 Method to be called when an object is spawned.
 
void Despawned (NetworkRunner runner, bool hasState)
 Called when a NetworkBehaviour is despawned.
 

Static Public Member Functions

static ArrayReader< T > GetArrayReader< T > (Type behaviourType, string property, IElementReaderWriter< T > readerWriter=null)
 Gets an ArrayReader for a network array of type T in a network behaviour of a specific type.
 
static BehaviourReader< T > GetBehaviourReader< T > (NetworkRunner runner, Type behaviourType, string property)
 Gets a BehaviourReader for a network behaviour of type T.
 
static BehaviourReader< TProperty > GetBehaviourReader< TBehaviour, TProperty > (NetworkRunner runner, string property)
 Gets a BehaviourReader for a network behaviour with a specific property of type TProperty.
 
static DictionaryReader< K, V > GetDictionaryReader< K, V > (Type behaviourType, string property, IElementReaderWriter< K > keyReaderWriter=null, IElementReaderWriter< V > valueReaderWriter=null)
 Gets a DictionaryReader for a network dictionary with keys of type K and values of type V in a network behaviour of a specific type.
 
static LinkListReader< T > GetLinkListReader< T > (Type behaviourType, string property, IElementReaderWriter< T > readerWriter=null)
 Gets a LinkListReader for a network linked list of type T in a network behaviour of a specific type.
 
static PropertyReader< T > GetPropertyReader< T > (Type behaviourType, string property)
 Gets a PropertyReader for a property of type T in a network behaviour of a specific type.
 
static PropertyReader< TProperty > GetPropertyReader< TBehaviour, TProperty > (string property)
 Gets a PropertyReader for a property of type TProperty in a network behaviour of type TBehaviour.
 
static NetworkBehaviourUtils.DictionaryInitializer< K, V > MakeInitializer< K, V > (Dictionary< K, V > dictionary)
 This is a special method that is meant to be used only for [Networked] properties inline initialization.
 
static NetworkBehaviourUtils.ArrayInitializer< T > MakeInitializer< T > (T[] array)
 This is a special method that is meant to be used only for [Networked] properties inline initialization.
 
static T * MakePtr< T > ()
 Creates a pointer to a value of type T. This method is meant to be used only for [Networked] properties inline initialization.
 
static T * MakePtr< T > (T defaultValue)
 Creates a pointer to a value of type T, initializing it with the provided default value. This method is meant to be used only for [Networked] properties inline initialization.
 
static ref T MakeRef< T > ()
 Creates a reference to a value of type T. This method is meant to be used only for [Networked] properties inline initialization.
 
static ref T MakeRef< T > (T defaultValue)
 Creates a reference to a value of type T, initializing it with the provided default value. This method is meant to be used only for [Networked] properties inline initialization.
 
static int NetworkDeserialize (NetworkRunner runner, byte *data, ref NetworkBehaviour result)
 Deserializes a NetworkBehaviour from a byte array.
 
static int NetworkSerialize (NetworkBehaviour obj, byte *data)
 Serializes a NetworkBehaviour into a byte array.
 
static int NetworkSerialize (NetworkRunner runner, NetworkBehaviour obj, byte *data)
 Serializes a NetworkBehaviour into a byte array.
 
static NetworkBehaviour NetworkUnwrap (NetworkRunner runner, NetworkBehaviourId wrapper)
 Converts a NetworkBehaviourId to a NetworkBehaviour.
 
static NetworkBehaviourId NetworkWrap (NetworkRunner runner, NetworkBehaviour obj)
 Converts a NetworkBehaviour to a NetworkBehaviourId.
 
static implicit operator NetworkBehaviourId (NetworkBehaviour behaviour)
 Converts NetworkBehaviour to NetworkBehaviourId.
 
- Static Public Member Functions inherited from Behaviour
static void DestroyBehaviour (Behaviour behaviour)
 Wrapper for Unity's GameObject.Destroy()
 

Public Attributes

int offset
 Gives access to the offset (in 32 bit words) and count (in 32 bit words) of this behaviour backing data.
 

Protected Member Functions

virtual bool ReplicateTo (PlayerRef player)
 Determines whether to replicate the network behaviour to the specified player. This method can be overridden in derived classes to implement custom replication logic.
 

Properties

Tick ChangedTick [get]
 The tick the data on this networked behaviour changed.
 
virtual ? int DynamicWordCount [get]
 Override this value for custom memory allocations. This is for advanced use cases only, and cannot be used if NetworkedAttribute is used in the derived class.

 
bool HasInputAuthority [get]
 Returns true if the Simulation.LocalPlayer of the associated NetworkRunner is the designated as Input Source for this network entity.
 
bool HasStateAuthority [get]
 Returns true if the associated NetworkRunner is the State Source for this network entity.
 
NetworkBehaviourId Id [get]
 The unique identifier for this network behaviour.
 
bool IsProxy [get]
 Returns true if the associated NetworkRunner is neither the Input nor State Authority for this network entity. It is recommended to use !HasStateAuthority or !HasInputAuthority when possible instead, as this check requires evaluating both authorities - and is therefore less performant than the individual checks.
 
NetworkBehaviourBuffer StateBuffer [get]
 Gets the state buffer associated with the network behaviour.
 
bool StateBufferIsValid [get]
 Gets a value indicating whether the state buffer is valid.
 
int int count WordInfo [get]
 
- Properties inherited from SimulationBehaviour
bool CanReceiveRenderCallback [get]
 Gets a value indicating whether this instance can receive render callbacks.
 
bool CanReceiveSimulationCallback [get]
 Gets a value indicating whether this instance can receive simulation callbacks.
 
NetworkObject Object [get]
 The NetworkObject this component is associated with.
 
NetworkRunner Runner [get]
 The NetworkRunner this component is associated with.
 

Detailed Description

Base class for Fusion network components, which are associated with a NetworkObject.

Derived from SimulationBehaviour, components derived from this class are associated with a NetworkRunner and Simulation. Components derived from this class are associated with a parent NetworkObject. and can use the NetworkedAttribute on properties to automate state synchronization, and can use the RpcAttribute on methods, to automate messaging.

Member Function Documentation

◆ CopyBackingFieldsToState()

virtual void CopyBackingFieldsToState ( bool  firstTime)
virtual

Copies the backing fields to the state. This method is meant to be overridden in derived classes.

Parameters
firstTimeIndicates whether this is the first time the method is called.

◆ CopyStateFrom()

void CopyStateFrom ( NetworkBehaviour  source)

Copies entire state of passed in source NetworkBehaviour

Parameters
sourceSource NetworkBehaviour to copy data from

◆ Despawned()

virtual void Despawned ( NetworkRunner  runner,
bool  hasState 
)
virtual

Called before the network object is despawned.

Parameters
runnerThe runner that owns the object
hasStateIf the state of the behaviour is still accessible

Implements IDespawned.

Reimplemented in HitboxRoot.

◆ FixedUpdateNetwork()

override void FixedUpdateNetwork ( )
virtual

Fixed update callback for networked behaviours.

Reimplemented from SimulationBehaviour.

◆ GetArrayReader< T >() [1/2]

ArrayReader< T > GetArrayReader< T > ( string  property)

Gets an ArrayReader for a network array of type T.

Template Parameters
TThe type of elements in the network array.
Parameters
propertyThe name of the property to be read.
Returns
An ArrayReader for the network array of type T.

◆ GetArrayReader< T >() [2/2]

static ArrayReader< T > GetArrayReader< T > ( Type  behaviourType,
string  property,
IElementReaderWriter< T >  readerWriter = null 
)
static

Gets an ArrayReader for a network array of type T in a network behaviour of a specific type.

Template Parameters
TThe type of elements in the network array.
Parameters
behaviourTypeThe type of the network behaviour.
propertyThe name of the property to be read.
readerWriter
Returns
An ArrayReader for the network array of type T in the network behaviour of the specified type.

◆ GetBehaviourReader< T >() [1/2]

static BehaviourReader< T > GetBehaviourReader< T > ( NetworkRunner  runner,
Type  behaviourType,
string  property 
)
static

Gets a BehaviourReader for a network behaviour of type T.

Template Parameters
TThe type of the network behaviour.
Parameters
runnerThe NetworkRunner associated with the network behaviour.
behaviourTypeThe type of the behaviour to be read.
propertyThe name of the property to be read.
Returns
A BehaviourReader for the network behaviour of type T.
Type Constraints
T :NetworkBehaviour 

◆ GetBehaviourReader< T >() [2/2]

BehaviourReader< T > GetBehaviourReader< T > ( string  property)

Gets a BehaviourReader for a network behaviour of type T.

Template Parameters
TThe type of the network behaviour.
Parameters
propertyThe name of the property to be read.
Returns
A BehaviourReader for the network behaviour of type T.
Type Constraints
T :NetworkBehaviour 

◆ GetBehaviourReader< TBehaviour, TProperty >()

static BehaviourReader< TProperty > GetBehaviourReader< TBehaviour, TProperty > ( NetworkRunner  runner,
string  property 
)
static

Gets a BehaviourReader for a network behaviour with a specific property of type TProperty.

Template Parameters
TBehaviourThe type of the network behaviour.
TPropertyThe type of the property in the network behaviour.
Parameters
runnerThe NetworkRunner associated with the network behaviour.
propertyThe name of the property to be read.
Returns
A BehaviourReader for the network behaviour with the specific property of type TProperty.
Type Constraints
TBehaviour :NetworkBehaviour 
TProperty :NetworkBehaviour 

◆ GetChangeDetector()

ChangeDetector GetChangeDetector ( ChangeDetector::Source  source,
bool  copyInitial = true 
)

Creates a ChangeDetector for this network behaviour.

Parameters
sourceThe source of the change detector.
copyInitialIndicates whether to copy the initial state of the network behaviour.
Returns
A ChangeDetector for this network behaviour.

◆ GetDictionaryReader< K, V >() [1/2]

DictionaryReader< K, V > GetDictionaryReader< K, V > ( string  property)

Gets a DictionaryReader for a network dictionary with keys of type K and values of type V.

Template Parameters
KThe type of keys in the network dictionary.
VThe type of values in the network dictionary.
Parameters
propertyThe name of the property to be read.
Returns
A DictionaryReader for the network dictionary with keys of type K and values of type V.

◆ GetDictionaryReader< K, V >() [2/2]

static DictionaryReader< K, V > GetDictionaryReader< K, V > ( Type  behaviourType,
string  property,
IElementReaderWriter< K >  keyReaderWriter = null,
IElementReaderWriter< V >  valueReaderWriter = null 
)
static

Gets a DictionaryReader for a network dictionary with keys of type K and values of type V in a network behaviour of a specific type.

Template Parameters
KThe type of keys in the network dictionary.
VThe type of values in the network dictionary.
Parameters
behaviourTypeThe type of the network behaviour.
propertyThe name of the property to be read.
keyReaderWriter
valueReaderWriter
Returns
A DictionaryReader for the network dictionary with keys of type K and values of type V in the network behaviour of the specified type.

◆ GetInput< T >() [1/2]

T? GetInput< T > ( )
Template Parameters
T
Type Constraints
T :unmanaged 
T :INetworkInput 

◆ GetInput< T >() [2/2]

bool GetInput< T > ( out T  input)

Returns true if it a valid INetworkInput can be found for the current simulation tick (Typically this is used in FixedUpdateNetwork).

The returned input struct originates from the NetworkObject.InputAuthority, and if valid contains the inputs supplied by that PlayerRef for the current simulation tick.

Type Constraints
T :unmanaged 
T :INetworkInput 

◆ GetLinkListReader< T >() [1/2]

LinkListReader< T > GetLinkListReader< T > ( string  property)

Gets a LinkListReader for a network linked list of type T.

Template Parameters
TThe type of elements in the network linked list.
Parameters
propertyThe name of the property to be read.
Returns
A LinkListReader for the network linked list of type T.

◆ GetLinkListReader< T >() [2/2]

static LinkListReader< T > GetLinkListReader< T > ( Type  behaviourType,
string  property,
IElementReaderWriter< T >  readerWriter = null 
)
static

Gets a LinkListReader for a network linked list of type T in a network behaviour of a specific type.

Template Parameters
TThe type of elements in the network linked list.
Parameters
behaviourTypeThe type of the network behaviour.
propertyThe name of the property to be read.
readerWriter
Returns
A LinkListReader for the network linked list of type T in the network behaviour of the specified type.

◆ GetPropertyReader< T >() [1/2]

PropertyReader< T > GetPropertyReader< T > ( string  property)

Gets a PropertyReader for a property of type T in this network behaviour.

Template Parameters
TThe type of the property in the network behaviour. Must be unmanaged.
Parameters
propertyThe name of the property to be read.
Returns
A PropertyReader for the property of type T in this network behaviour.
Type Constraints
T :unmanaged 

◆ GetPropertyReader< T >() [2/2]

static PropertyReader< T > GetPropertyReader< T > ( Type  behaviourType,
string  property 
)
static

Gets a PropertyReader for a property of type T in a network behaviour of a specific type.

Template Parameters
TThe type of the property in the network behaviour. Must be unmanaged.
Parameters
behaviourTypeThe type of the network behaviour.
propertyThe name of the property to be read.
Returns
A PropertyReader for the property of type T in the network behaviour of the specified type.
Type Constraints
T :unmanaged 

◆ GetPropertyReader< TBehaviour, TProperty >()

static PropertyReader< TProperty > GetPropertyReader< TBehaviour, TProperty > ( string  property)
static

Gets a PropertyReader for a property of type TProperty in a network behaviour of type TBehaviour.

Template Parameters
TBehaviourThe type of the network behaviour.
TPropertyThe type of the property in the network behaviour. Must be unmanaged.
Parameters
propertyThe name of the property to be read.
Returns
A PropertyReader for the property of type TProperty in the network behaviour of type TBehaviour.
Type Constraints
TBehaviour :NetworkBehaviour 
TProperty :unmanaged 

◆ MakePtr< T >() [1/2]

static T * MakePtr< T > ( )
static

Creates a pointer to a value of type T. This method is meant to be used only for [Networked] properties inline initialization.

Template Parameters
TThe type of the value. Must be unmanaged.
Returns
A pointer to a value of type T.
Exceptions
NotImplementedExceptionThrown because this method is meant to be used only for [Networked] properties inline initialization.
Type Constraints
T :unmanaged 

◆ MakePtr< T >() [2/2]

static T * MakePtr< T > ( defaultValue)
static

Creates a pointer to a value of type T, initializing it with the provided default value. This method is meant to be used only for [Networked] properties inline initialization.

Template Parameters
TThe type of the value. Must be unmanaged.
Parameters
defaultValueThe default value to initialize the value with.
Returns
A pointer to a value of type T.
Exceptions
NotImplementedExceptionThrown because this method is meant to be used only for [Networked] properties inline initialization.
Type Constraints
T :unmanaged 

◆ MakeRef< T >() [1/2]

static ref T MakeRef< T > ( )
static

Creates a reference to a value of type T. This method is meant to be used only for [Networked] properties inline initialization.

Template Parameters
TThe type of the value. Must be unmanaged.
Returns
A reference to a value of type T.
Exceptions
NotImplementedExceptionThrown because this method is meant to be used only for [Networked] properties inline initialization.
Type Constraints
T :unmanaged 

◆ MakeRef< T >() [2/2]

static ref T MakeRef< T > ( defaultValue)
static

Creates a reference to a value of type T, initializing it with the provided default value. This method is meant to be used only for [Networked] properties inline initialization.

Template Parameters
TThe type of the value. Must be unmanaged.
Parameters
defaultValueThe default value to initialize the value with.
Returns
A reference to a value of type T.
Exceptions
NotImplementedExceptionThrown because this method is meant to be used only for [Networked] properties inline initialization.
Type Constraints
T :unmanaged 

◆ NetworkDeserialize()

static int NetworkDeserialize ( NetworkRunner  runner,
byte *  data,
ref NetworkBehaviour  result 
)
static

Deserializes a NetworkBehaviour from a byte array.

Parameters
runnerThe NetworkRunner associated with the NetworkBehaviour.
dataThe byte pointer to read the serialized data from.
resultThe NetworkBehaviour to store the deserialized data in.
Returns
The size of the deserialized data in bytes.

◆ NetworkSerialize() [1/2]

static int NetworkSerialize ( NetworkBehaviour  obj,
byte *  data 
)
static

Serializes a NetworkBehaviour into a byte array.

Parameters
objThe NetworkBehaviour to be serialized.
dataThe byte pointer to write the serialized data to.
Returns
The size of the serialized data in bytes.

◆ NetworkSerialize() [2/2]

static int NetworkSerialize ( NetworkRunner  runner,
NetworkBehaviour  obj,
byte *  data 
)
static

Serializes a NetworkBehaviour into a byte array.

Parameters
runner
objThe NetworkBehaviour to be serialized.
dataThe byte pointer to write the serialized data to.
Returns
The size of the serialized data in bytes.

◆ NetworkUnwrap()

static NetworkBehaviour NetworkUnwrap ( NetworkRunner  runner,
NetworkBehaviourId  wrapper 
)
static

Converts a NetworkBehaviourId to a NetworkBehaviour.

Parameters
runnerThe NetworkRunner associated with the NetworkBehaviour.
wrapperThe NetworkBehaviourId to be converted.
Returns
The NetworkBehaviour represented by the NetworkBehaviourId. If the NetworkBehaviourId is not valid or a NetworkBehaviour with the NetworkBehaviourId does not exist, returns null.

◆ NetworkWrap()

static NetworkBehaviourId NetworkWrap ( NetworkRunner  runner,
NetworkBehaviour  obj 
)
static

Converts a NetworkBehaviour to a NetworkBehaviourId.

Parameters
runnerThe NetworkRunner associated with the NetworkBehaviour.
objThe NetworkBehaviour to be converted.
Returns
A NetworkBehaviourId representing the NetworkBehaviour. If the NetworkBehaviour is null or its NetworkObject's Id is default, returns a default NetworkBehaviourId.

◆ operator NetworkBehaviourId()

static implicit operator NetworkBehaviourId ( NetworkBehaviour  behaviour)
static

Converts NetworkBehaviour to NetworkBehaviourId.

Parameters
behaviourNetworkBehaviour to convert
Returns
NetworkBehaviourId representing the NetworkBehaviour

◆ ReinterpretState< T >()

ref T ReinterpretState< T > ( int  offset = 0)

Allows read and write access to the internal state buffer.

Parameters
offsetThe offset to generate a ref for, in integer words
Template Parameters
TThe type of the ref to generate
Returns
Reference to the location in memory defined by offset
Type Constraints
T :unmanaged 

◆ ReplicateTo() [1/2]

virtual bool ReplicateTo ( PlayerRef  player)
protectedvirtual

Determines whether to replicate the network behaviour to the specified player. This method can be overridden in derived classes to implement custom replication logic.

Parameters
playerThe player to potentially replicate the network behaviour to.
Returns
True if the network behaviour should be replicated to the player, false otherwise. The default implementation always returns true.

◆ ReplicateTo() [2/2]

void ReplicateTo ( PlayerRef  player,
bool  replicate 
)

Controls if this network behaviours state is replicated to a player or not.

Parameters
playerThe player to change replication status for
replicatetrue = replicate, false = don't replicate

◆ ReplicateToAll()

void ReplicateToAll ( bool  replicate)

Sets the default replicated state for this behaviour, this by default is true so calling ReplicateToAll(true) does nothing if ReplicateToAll(false) hasn't been called before

Parameters
replicateThe default state of this behaviour

◆ Spawned()

virtual void Spawned ( )
virtual

Post spawn callback.

Implements ISpawned.

Reimplemented in NetworkMecanimAnimator, and NetworkTransform.

◆ TryGetSnapshotsBuffers()

bool TryGetSnapshotsBuffers ( out NetworkBehaviourBuffer  from,
out NetworkBehaviourBuffer  to,
out float  alpha 
)

Tries to get the snapshot buffers for this network behaviour.

Parameters
fromThe buffer representing the state of the network behaviour at the start of the render frame.
toThe buffer representing the state of the network behaviour at the end of the render frame.
alphaThe interpolation factor between the start and end of the render frame.
Returns
True if the snapshot buffers are valid, false otherwise.