Photon Bolt Engine API  1.2.7
Public Member Functions | Static Public Member Functions | Properties | Private Member Functions | Private Attributes | List of all members
BoltEntity Class Reference

A game entity within the bolt simulation More...

Inheritance diagram for BoltEntity:

Public Member Functions

void AddEventCallback< T > (Action< T > callback)
 Add a event callback to this entity. More...
 
void AddEventListener (UE.MonoBehaviour behaviour)
 Add an event listener to this entity. More...
 
void AssignControl (BoltConnection connection)
 Assigns control of this entity to a connection More...
 
void AssignControl (BoltConnection connection, IProtocolToken token)
 Assigns control of this entity to a connection More...
 
void DestroyDelayed (float time)
 Destroy this entity after a given delay More...
 
void Freeze (bool pause)
 Freeze or unfreeze an entity More...
 
TState GetState< TState > ()
 Get the state if this entity More...
 
void Idle (BoltConnection connection, bool idle)
 Set this entity as idle on the supplied connection, this means that the connection will not receive update state for this entity as long as it's idle. More...
 
void InvokeMany (Command cmd, int duration, CommandCallback callback)
 
void InvokeOnce (Command cmd, int delay, CommandCallback callback)
 
bool IsController (BoltConnection connection)
 Checks if this entity is being controlled by the connection More...
 
BoltEntitySettingsModifier ModifySettings ()
 Creates an object which lets you modify the internal settings of an entity before it is attached to Bolt. More...
 
bool QueueInput (INetworkCommandData data)
 Queue an input data on this entity for execution. This is called on a client which is controlling a proxied entity. The data will be sent to the server for authoritative execution More...
 
void ReleaseControl ()
 Releases local control of this entity More...
 
void ReleaseControl (IProtocolToken token)
 Releases local control of this entity More...
 
void RemoveEventCallback< T > (Action< T > callback)
 Remove a event callback to this entity. More...
 
void RemoveEventListener (UE.MonoBehaviour behaviour)
 Remove an event listern from this entity More...
 
void RevokeControl ()
 Revokes control of this entity from a connection More...
 
void RevokeControl (IProtocolToken token)
 Revokes control of this entity from a connection More...
 
void SetParent (BoltEntity parent)
 Sets the parent of this entity More...
 
void SetScope (BoltConnection connection, bool inScope)
 Sets the scope for the connection passed in for this entity. Only usable if Scope Mode has been set to Manual. More...
 
void SetScopeAll (bool inScope)
 Sets the scope of all currently active connections for this entity. Only usable if Scope Mode has been set to Manual. More...
 
bool StateIs (Type t)
 Checks which type of state this entity has More...
 
bool StateIs< TState > ()
 Checks which type of state this entity has More...
 
void TakeControl ()
 Takes local control of this entity More...
 
void TakeControl (IProtocolToken token)
 Takes local control of this entity More...
 
override string ToString ()
 String representation of the entity More...
 
bool TryFindState< TState > (out TState state)
 A null safe way to look for a specific type of state on an entity More...
 

Static Public Member Functions

static implicit operator UE.GameObject (BoltEntity entity)
 

Properties

IProtocolToken attachToken [get]
 
bool canFreeze [get, set]
 Whether the entity can be paused / frozen More...
 
IProtocolToken controlGainedToken [get]
 
BoltConnection controller [get]
 If this entity is controlled by a remote connection it contains that connection, otherwise null More...
 
IProtocolToken controlLostToken [get]
 
IProtocolToken detachToken [get]
 
internal Entity Entity [get]
 
bool hasControl [get]
 Do we have control of this entity? More...
 
bool hasControlWithPrediction [get]
 Do we have control of this entity and are we using client side prediction More...
 
bool isAttached [get]
 If this entity is attached to Bolt or not More...
 
bool isControlled [get]
 
bool isControllerOrOwner [get]
 
bool isFrozen [get]
 If this entity is currently paused More...
 
bool isOwner [get]
 Did the local computer create this entity or not? More...
 
bool isSceneObject [get]
 This is a scene object placed in the scene in the Unity editor More...
 
object IBoltListNode< BoltEntity >. list [get, set]
 
NetworkId networkId [get]
 The unique id of this entity More...
 
BoltEntity IBoltListNode< BoltEntity >. next [get, set]
 
bool persistsOnSceneLoad [get]
 Should this entity persist between scene loads More...
 
PrefabId prefabId [get]
 The prefabId used to instantiate this entity More...
 
BoltEntity IBoltListNode< BoltEntity >. prev [get, set]
 
internal UniqueId sceneGuid [get, set]
 
internal UniqueId serializerGuid [get, set]
 
BoltConnection source [get]
 If this entity was created on another computer, contains the connection we received this entity from, otherwise null More...
 

Private Member Functions

void Awake ()
 
IEnumerator DestroyDelayedInternal (float time)
 
void OnDestroy ()
 
void OnDisable ()
 
void OnDrawGizmos ()
 
internal void VerifyNotAttached ()
 

Private Attributes

internal bool _allowFirstReplicationWhenFrozen = false
 
internal bool _allowInstantiateOnClient = true
 
internal bool _alwaysProxy = false
 
internal int _autoFreezeProxyFrames = 0
 
internal bool _autoRemoveChildEntities = false
 
internal bool _clientPredicted = true
 
internal bool _detachOnDisable = true
 
internal Entity _entity
 
internal QueryComponentOptions _entityBehaviourQueryOption = QueryComponentOptions.UseGlobal
 
internal QueryComponentOptions _entityPriorityCalculatorQueryOption = QueryComponentOptions.UseGlobal
 
internal QueryComponentOptions _entityReplicationFilterQueryOption = QueryComponentOptions.UseGlobal
 
internal bool _persistThroughSceneLoads = false
 
internal int _prefabId = -1
 
internal string _sceneGuid
 
internal bool _sceneObjectAutoAttach = true
 
internal bool _sceneObjectDestroyOnDetach = true
 
internal string _serializerGuid
 
internal int _updateRate = 1
 

Detailed Description

A game entity within the bolt simulation

Example: Instantiating and taking control of a new BoltEntity that will replicate to all connected clients.

public void InstantiateEntity() {
BoltEntity entity = BoltNetwork.Instantiate(BoltPrefabs.Player, RandomSpawn(), Quaternion.identity);
entity.TakeControl();
}

Member Function Documentation

◆ AddEventCallback< T >()

void BoltEntity.AddEventCallback< T > ( Action< T >  callback)
inline

Add a event callback to this entity.

Type Constraints
T :Bolt.Event 

◆ AddEventListener()

void BoltEntity.AddEventListener ( UE.MonoBehaviour  behaviour)
inline

Add an event listener to this entity.

Parameters
behaviourThe behaviour to invoke event callbacks on

◆ AssignControl() [1/2]

void BoltEntity.AssignControl ( BoltConnection  connection)
inline

Assigns control of this entity to a connection

Parameters
connectionThe connection to assign control to

Example: Instantiating and assigning control of an entity to a newly connected player.

public override void Connected(BoltConnection connection) {
var playerEntity = BoltNetwork.Instantiate(BoltPrefabs.Player, RandomSpawn(), Quaternion.Identity);
playerEntity.AssignControl(connection);
}

◆ AssignControl() [2/2]

void BoltEntity.AssignControl ( BoltConnection  connection,
IProtocolToken  token 
)
inline

Assigns control of this entity to a connection

Parameters
connectionThe connection to assign control to
tokenA data token of max size 512 bytes

Example: Instantiating and assigning control of an entity to a newly connected player.

public override void Connected(BoltConnection connection, IProtocolToken token) {
var playerEntity = BoltNetwork.Instantiate(BoltPrefabs.Player, RandomSpawn(), Quaternion.Identity);
var fingerprint = ((UserInfo)token).fingerprint;
PlayerLoadout loadout;
if(playerDatabase.Contains(fingerprint, out loadout)) {
playerEntity.AssignControl(connection, loadout);
}
else {
playerEntity.AssignControl(connection, new BeginnerLoadout());
}
}

◆ Awake()

void BoltEntity.Awake ( )
inlineprivate

◆ DestroyDelayed()

void BoltEntity.DestroyDelayed ( float  time)
inline

Destroy this entity after a given delay

Parameters
timeThe time interval to delay

Example: A death routine that makes state changes and initiates a destruction with configurable delay.

void OnDeath(BoltEntity entity) {
var state = entity.GetState<ILivingEntity>();
state.Modify().alive = false;
state.Modify().DeathTrigger();
entity.DestroyDelayed(ServerConfig.DESTRUCT_DELAY);
}

◆ DestroyDelayedInternal()

IEnumerator BoltEntity.DestroyDelayedInternal ( float  time)
inlineprivate

◆ Freeze()

void BoltEntity.Freeze ( bool  pause)
inline

Freeze or unfreeze an entity

Parameters
pauseTrue if entity should freeze and false to unfreeze

Example: Pausing all entities.

void Pause() {
foreach(BoltEntity entity in BoltNetwork.entities) {
if(entity.canFreeze) {
entity.Freeze(true);
}
}
}

◆ GetState< TState >()

TState BoltEntity.GetState< TState > ( )
inline

Get the state if this entity

Template Parameters
TStateThe type of state to get
Returns
The state

Example: Modifying the state of an entity to change its name.

public void RenameEntity(BoltEntity entity, string name) {
entity.GetState<IPlayerState>().Modify().name = name;
}

◆ Idle()

void BoltEntity.Idle ( BoltConnection  connection,
bool  idle 
)
inline

Set this entity as idle on the supplied connection, this means that the connection will not receive update state for this entity as long as it's idle.

Parameters
connectionThe connection to idle the entity on
idleIf this should be idle or not

◆ InvokeMany()

void BoltEntity.InvokeMany ( Command  cmd,
int  duration,
CommandCallback  callback 
)
inline

◆ InvokeOnce()

void BoltEntity.InvokeOnce ( Command  cmd,
int  delay,
CommandCallback  callback 
)
inline

◆ IsController()

bool BoltEntity.IsController ( BoltConnection  connection)
inline

Checks if this entity is being controlled by the connection

Parameters
connectionThe connection to check

◆ ModifySettings()

BoltEntitySettingsModifier BoltEntity.ModifySettings ( )
inline

Creates an object which lets you modify the internal settings of an entity before it is attached to Bolt.

Returns
The object used to modify entity settings

◆ OnDestroy()

void BoltEntity.OnDestroy ( )
inlineprivate

◆ OnDisable()

void BoltEntity.OnDisable ( )
inlineprivate

◆ OnDrawGizmos()

void BoltEntity.OnDrawGizmos ( )
inlineprivate

◆ operator UE.GameObject()

static implicit BoltEntity.operator UE.GameObject ( BoltEntity  entity)
inlinestatic

◆ QueueInput()

bool BoltEntity.QueueInput ( INetworkCommandData  data)
inline

Queue an input data on this entity for execution. This is called on a client which is controlling a proxied entity. The data will be sent to the server for authoritative execution

Parameters
dataThe input data to queue

Example: A SimulateController() loop that queues WASD-style movement input.

bool forward;
bool backward;
bool left;
bool right;
public override void SimulateController() {
IPlayerCommandInput input = PlayerCommand.Create();
PollKeys();
input.forward = forward;
input.backward = backward;
input.left = left;
input.right = right;
entity.QueueInput(input);
}

◆ ReleaseControl() [1/2]

void BoltEntity.ReleaseControl ( )
inline

Releases local control of this entity

Example: Releasing control of a player entity when exiting the game.

BoltEntity controlledEntity;
public override void ExitGame() {
controlledEntity.ReleaseControl();
BoltNetwork.server.Disconnect();
}

◆ ReleaseControl() [2/2]

void BoltEntity.ReleaseControl ( IProtocolToken  token)
inline

Releases local control of this entity

Parameters
tokenA data token of max size 512 bytes

Example: Releasing control of a player entity when exiting the game.

BoltEntity controlledEntity;
public override void ExitGame() {
ClientEvent evt = new ClientEvent();
evt.EvtCode = ClientEventCode.EXIT_GAME;
controlledEntity.ReleaseControl(evt);
BoltNetwork.server.Disconnect();
}

◆ RemoveEventCallback< T >()

void BoltEntity.RemoveEventCallback< T > ( Action< T >  callback)
inline

Remove a event callback to this entity.

Type Constraints
T :Bolt.Event 

◆ RemoveEventListener()

void BoltEntity.RemoveEventListener ( UE.MonoBehaviour  behaviour)
inline

Remove an event listern from this entity

Parameters
behaviourThe behaviour to remove

◆ RevokeControl() [1/2]

void BoltEntity.RevokeControl ( )
inline

Revokes control of this entity from a connection

Example: A server-side stun routine that completely revokes control for the length of stun interval.

IEnumerator Stun(BoltEntity entity, float stunInterval) {
var controller = entity.controller;
entity.RevokeControl();
entity.GetState<ILivingEntity>().Modify().stunned = true;
return new WaitForSeconds(stunInterval);
entity.GetState<ILivingEntity>().Modify().stunned = false;
}

◆ RevokeControl() [2/2]

void BoltEntity.RevokeControl ( IProtocolToken  token)
inline

Revokes control of this entity from a connection

Parameters
tokenA data token of max size 512 bytes

Example: A server-side stun routine that completely revokes control for the length of stun interval.

IEnumerator Stun(BoltEntity entity, float stunInterval) {
var controller = entity.controller;
CombatEventData evtData = new CombatEventData();
evtData.stunInterval = stunInterval;
entity.RevokeControl(evtData);
entity.GetState<ILivingEntity>().Modify().stunned = true;
return new WaitForSeconds(stunInterval);
entity.GetState<ILivingEntity>().Modify().stunned = false;
}

◆ SetParent()

void BoltEntity.SetParent ( BoltEntity  parent)
inline

Sets the parent of this entity

Parameters
parentThe parent of this entity

Example: Instantiating a vehicle and taking control of it.

public void SpawnPlayerVehicle(BoltEntity playerEntity) {
var car = BoltNetwork.Instantiate(BoltPrefabs.Vehicle, playerEntity.transform.position, playerEntity.transform.rotation);
car.AssignContro(playerEntity.controller);
playerEntity.RevokeControl();
playerEntity.SetParent(car);
}

◆ SetScope()

void BoltEntity.SetScope ( BoltConnection  connection,
bool  inScope 
)
inline

Sets the scope for the connection passed in for this entity. Only usable if Scope Mode has been set to Manual.

Parameters
connectionThe connection being scoped
inScopeIf this entity should be in scope or not

Example: A coroutine that renders a player invisible to enemies for a given time in seconds.

IEnumerator Stealth(BoltEntity entity, float stealthInterval) {
int team = entity.GetState<IPlayerState>().team;
foreach(BoltEntity adjEntity in BoltNetwork.entities) {
if(adjEntity == entity) continue;
if(!adjEntity.StateIs%ltIPlayerState%gt()) continue;
int adjTeam = adjEntity.GetState<IPlayerState>().team;
if(team != adjTeam) {
entity.SetScope(adjEntity.controller, false);
}
}
yield return new WaitForSeconds(stealthInterval);
entity.SetScopeAll(true);
}

◆ SetScopeAll()

void BoltEntity.SetScopeAll ( bool  inScope)
inline

Sets the scope of all currently active connections for this entity. Only usable if Scope Mode has been set to Manual.

Parameters
inScopeIf this entity should be in scope or not

Example: A method which renders an entity invisible to all active client connections.

public void Disappear(BoltEntity entity) {
entity.SetScopeAll(false);
}

◆ StateIs()

bool BoltEntity.StateIs ( Type  t)
inline

Checks which type of state this entity has

Parameters
tThe type of state to check for
Returns
True if this entity has a state of type TState otherwise false

Example: Discarding attack requests that do not target living entities.

public override void FireOnOwner(BoltEntity entity, BoltEntity target, AttackData attack) {
if(entity.isOwner) {
if(!target.StateIs(typeof(ILivingEntity))) {
return;
}
target.GetState<ILivingEntity>().Modify().health -= attack.damage;
}
}

◆ StateIs< TState >()

bool BoltEntity.StateIs< TState > ( )
inline

Checks which type of state this entity has

Template Parameters
TStateThe type of state to check for
Returns
True if this entity has a state of type TState otherwise false

Example: Discarding attack requests that do not target living entities.

public override void FireOnOwner(BoltEntity entity, BoltEntity target, AttackData attack) {
if(entity.isOwner) {
if(!target.StateIs<ILivingEntity>()) {
return;
}
target.GetState<ILivingEntity>().Modify().health -= attack.damage;
}
}

◆ TakeControl() [1/2]

void BoltEntity.TakeControl ( )
inline

Takes local control of this entity

Example: Spawning a server-side player entity.

void SpawnServerPlayer() {
var entity = Instantiate(BoltPrefabs.Player);
entity.TakeControl();
}

◆ TakeControl() [2/2]

void BoltEntity.TakeControl ( IProtocolToken  token)
inline

Takes local control of this entity

Parameters
tokenA data token of max size 512 bytes

Example: Spawning a server-side player entity and initializing it with a local hotkey configuration.

HotkeyConfig hotkeys;
void SpawnServerPlayer() {
var entity = Instantiate(BoltPrefabs.Player);
entity.TakeControl(hotkeys);
}

◆ ToString()

override string BoltEntity.ToString ( )
inline

String representation of the entity

Example: Logging new entities to the debug log.

public override void EntityAttached(BoltEntity entity) {
Debug.Log("Entity Attached: " + entity.ToString());
}

◆ TryFindState< TState >()

bool BoltEntity.TryFindState< TState > ( out TState  state)
inline

A null safe way to look for a specific type of state on an entity

Template Parameters
TStateThe state type to search for
Parameters
stateEntity to search

Example: Pausing all player entities using TryFindState().

public void FreezeAllPlayers() {
foreach(BoltEntity entity in BoltNetwork.entities) {
IPlayerState state;
if(entity.TryGetState<IPlayerState>(out state)) {
entity.Freeze(true);
state.Modify().pausedByServer = true;
}
}
}

◆ VerifyNotAttached()

internal void BoltEntity.VerifyNotAttached ( )
inlineprivate

Member Data Documentation

◆ _allowFirstReplicationWhenFrozen

internal bool BoltEntity._allowFirstReplicationWhenFrozen = false
private

◆ _allowInstantiateOnClient

internal bool BoltEntity._allowInstantiateOnClient = true
private

◆ _alwaysProxy

internal bool BoltEntity._alwaysProxy = false
private

◆ _autoFreezeProxyFrames

internal int BoltEntity._autoFreezeProxyFrames = 0
private

◆ _autoRemoveChildEntities

internal bool BoltEntity._autoRemoveChildEntities = false
private

◆ _clientPredicted

internal bool BoltEntity._clientPredicted = true
private

◆ _detachOnDisable

internal bool BoltEntity._detachOnDisable = true
private

◆ _entity

internal Entity BoltEntity._entity
private

◆ _entityBehaviourQueryOption

internal QueryComponentOptions BoltEntity._entityBehaviourQueryOption = QueryComponentOptions.UseGlobal
private

◆ _entityPriorityCalculatorQueryOption

internal QueryComponentOptions BoltEntity._entityPriorityCalculatorQueryOption = QueryComponentOptions.UseGlobal
private

◆ _entityReplicationFilterQueryOption

internal QueryComponentOptions BoltEntity._entityReplicationFilterQueryOption = QueryComponentOptions.UseGlobal
private

◆ _persistThroughSceneLoads

internal bool BoltEntity._persistThroughSceneLoads = false
private

◆ _prefabId

internal int BoltEntity._prefabId = -1
private

◆ _sceneGuid

internal string BoltEntity._sceneGuid
private

◆ _sceneObjectAutoAttach

internal bool BoltEntity._sceneObjectAutoAttach = true
private

◆ _sceneObjectDestroyOnDetach

internal bool BoltEntity._sceneObjectDestroyOnDetach = true
private

◆ _serializerGuid

internal string BoltEntity._serializerGuid
private

◆ _updateRate

internal int BoltEntity._updateRate = 1
private

Property Documentation

◆ attachToken

IProtocolToken BoltEntity.attachToken
get

◆ canFreeze

bool BoltEntity.canFreeze
getset

Whether the entity can be paused / frozen

Example: Pausing all entities that can be frozen.

void Pause() {
foreach(BoltEntity entity in BoltNetwork.entities) {
if(entity.canFreeze) {
entity.Freeze(true);
}
}
}

◆ controlGainedToken

IProtocolToken BoltEntity.controlGainedToken
get

◆ controller

BoltConnection BoltEntity.controller
get

If this entity is controlled by a remote connection it contains that connection, otherwise null

Example: Disconnecting a client when they run out of lives.

void OnDeath(BoltEntity entity) {
if(entity.GetState%ltILivingEntity>().lives == 0) {
entity.controller.Disconnect(new ServerMessage("Game Over", "Zero Lives Remaining"));
}
}

◆ controlLostToken

IProtocolToken BoltEntity.controlLostToken
get

◆ detachToken

IProtocolToken BoltEntity.detachToken
get

◆ Entity

internal Entity BoltEntity.Entity
getprivate

◆ hasControl

bool BoltEntity.hasControl
get

Do we have control of this entity?

Example: Implementing a method to turn controlled entities green on the minimap.

public void AddKnownEntity(BoltEntity entity) {
_minimap.AddNewEntity(entity.networkId, entity);
if(entity.hasControl) {
_minimap.entities[entity.networkId].color = Color.Green;
}
}

◆ hasControlWithPrediction

bool BoltEntity.hasControlWithPrediction
get

Do we have control of this entity and are we using client side prediction

◆ isAttached

bool BoltEntity.isAttached
get

If this entity is attached to Bolt or not

Example: Checking if an entity is still attached before trying to modify the state. This can occur if entities are destroyed immediately upon death.

void DealDamage(BoltEntity entity, AttackData atkData) { if(entity.isAttached) { entity.GetState<ILivingEntity>().Modify().hp -= atkData.damage; } }

◆ isControlled

bool BoltEntity.isControlled
get

◆ isControllerOrOwner

bool BoltEntity.isControllerOrOwner
get

◆ isFrozen

bool BoltEntity.isFrozen
get

If this entity is currently paused

Example: Unpausing all frozen entities.

void Unpause() {
foreach(BoltEntity entity in BoltNetwork.entities) {
if(entity.isFrozen) {
entity.Freeze(false);
}
}
}

◆ isOwner

bool BoltEntity.isOwner
get

Did the local computer create this entity or not?

Example: Implementing an authoritative weapon firing method.

void FireWeapon(PlayerCommand cmd) {
if (activeWeapon.fireFrame + activeWeapon.refireRate <= BoltNetwork.serverFrame) {
activeWeapon.fireFrame = BoltNetwork.serverFrame;
state.Fire();
if (entity.isOwner) {
activeWeapon.OnOwner(cmd, entity);
}
}
}

◆ isSceneObject

bool BoltEntity.isSceneObject
get

This is a scene object placed in the scene in the Unity editor

Example: Configuring the state of initial buildings and minions during startup.

public override void BoltStartDone() {
foreach(BoltEntity entity in BoltNetwork.entities) {
if(entity.isSceneObject) {
if(entity.StateIs<IStructure>()) {
StructureFactory.Configure(entity);
}
else if(entity.StateIs<IMinion>()) {
MinionFactory.Configure(entity);
}
}
}
}

◆ list

object IBoltListNode<BoltEntity>. BoltEntity.list
getsetprivate

◆ networkId

NetworkId BoltEntity.networkId
get

The unique id of this entity

◆ next

BoltEntity IBoltListNode<BoltEntity>. BoltEntity.next
getsetprivate

◆ persistsOnSceneLoad

bool BoltEntity.persistsOnSceneLoad
get

Should this entity persist between scene loads

Example: Pausing all persistent entities before changing scenes

public override void SceneLoadLocalBegin(string map) {
foreach(BoltEntity entity in BoltNetwork.entities) {
if(entity.persistsOnSceneLoad) {
entity.Freeze(true);
}
}
}

◆ prefabId

PrefabId BoltEntity.prefabId
get

The prefabId used to instantiate this entity

Example: Cloning an entity with prefabId.

BoltEntity Replicate(BoltEntity entity) {
return Instantiate(entity.prefabId, entity.transform.position, entity.transform.rotation);
}

◆ prev

BoltEntity IBoltListNode<BoltEntity>. BoltEntity.prev
getsetprivate

◆ sceneGuid

internal UniqueId BoltEntity.sceneGuid
getsetprivate

◆ serializerGuid

internal UniqueId BoltEntity.serializerGuid
getsetprivate

◆ source

BoltConnection BoltEntity.source
get

If this entity was created on another computer, contains the connection we received this entity from, otherwise null

Example: Using source to determine if an entity was originally instantiated on a remote host.

bool IsRemoteEntity(BoltEntity entity) {
return entity.source != null;
}