Photon Bolt Engine API
Networking Middleware
Public Member Functions | Static Public Member Functions | Properties | List of all members
BoltEntity Class Reference

A game entity within the bolt simulation More...

Inheritance diagram for BoltEntity:

Public Member Functions

BoltEntitySettingsModifier ModifySettings ()
 Creates an object which lets you modify the internal settings of an entity before it is attached to Bolt. 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...
 
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 SetParent (BoltEntity parent)
 Sets the parent of this entity More...
 
void TakeControl ()
 Takes local control of this entity More...
 
void TakeControl (IProtocolToken token)
 Takes local control of this entity More...
 
void ReleaseControl ()
 Releases local control of this entity More...
 
void ReleaseControl (IProtocolToken token)
 Releases local control of 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 RevokeControl ()
 Revokes control of this entity from a connection More...
 
void RevokeControl (IProtocolToken token)
 Revokes control of this entity from a connection More...
 
bool IsController (BoltConnection connection)
 Checks if this entity is being controlled by the connection 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 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 Freeze (bool pause)
 Freeze or unfreeze an entity More...
 
void AddEventListener (UE.MonoBehaviour behaviour)
 Add an event listener to this entity. More...
 
void AddEventCallback< T > (Action< T > callback)
 Add a event callback to this entity. More...
 
void RemoveEventListener (UE.MonoBehaviour behaviour)
 Remove an event listern from this entity More...
 
void RemoveEventCallback< T > (Action< T > callback)
 Remove a event callback to this entity. More...
 
TState GetState< TState > ()
 Get the state if this entity More...
 
bool TryFindState< TState > (out TState state)
 A null safe way to look for a specific type of state on an entity More...
 
bool StateIs< TState > ()
 Checks which type of state this entity has More...
 
bool StateIs (Type t)
 Checks which type of state this entity has More...
 
override string ToString ()
 String representation of the entity More...
 
void DestroyDelayed (float time)
 Destroy this entity after a given delay More...
 
void InvokeOnce (Command cmd, int delay, CommandCallback callback)
 
void InvokeMany (Command cmd, int duration, CommandCallback callback)
 

Static Public Member Functions

static implicit operator UE.GameObject (BoltEntity entity)
 

Properties

PrefabId prefabId [get]
 The prefabId used to instantiate this entity More...
 
BoltConnection source [get]
 If this entity was created on another computer, contains the connection we received this entity from, otherwise null More...
 
IProtocolToken attachToken [get]
 
IProtocolToken detachToken [get]
 
IProtocolToken controlGainedToken [get]
 
IProtocolToken controlLostToken [get]
 
NetworkId networkId [get]
 The unique id of this entity More...
 
bool canFreeze [get, set]
 Whether the entity can be paused / frozen More...
 
BoltConnection controller [get]
 If this entity is controlled by a remote connection it contains that connection, otherwise null 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 isSceneObject [get]
 This is a scene object placed in the scene in the Unity editor More...
 
bool isOwner [get]
 Did the local computer create this entity or not? More...
 
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 persistsOnSceneLoad [get]
 Should this entity persist between scene loads More...
 

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.

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

Member Function Documentation

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

Add a event callback to this entity.

Type Constraints
T :Bolt.Event 
void BoltEntity.AddEventListener ( UE.MonoBehaviour  behaviour)
inline

Add an event listener to this entity.

Parameters
behaviourThe behaviour to invoke event callbacks on
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.

1 public override void Connected(BoltConnection connection) {
2  var playerEntity = BoltNetwork.Instantiate(BoltPrefabs.Player, RandomSpawn(), Quaternion.Identity);
3 
4  playerEntity.AssignControl(connection);
5 }
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.

1 public override void Connected(BoltConnection connection, IProtocolToken token) {
2  var playerEntity = BoltNetwork.Instantiate(BoltPrefabs.Player, RandomSpawn(), Quaternion.Identity);
3 
4  var fingerprint = ((UserInfo)token).fingerprint;
5  PlayerLoadout loadout;
6 
7  if(playerDatabase.Contains(fingerprint, out loadout)) {
8  playerEntity.AssignControl(connection, loadout);
9  }
10  else {
11  playerEntity.AssignControl(connection, new BeginnerLoadout());
12  }
13 }
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.

1 void OnDeath(BoltEntity entity) {
2  var state = entity.GetState<ILivingEntity>();
3 
4  state.Modify().alive = false;
5  state.Modify().DeathTrigger();
6 
7  entity.DestroyDelayed(ServerConfig.DESTRUCT_DELAY);
8 }
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.

1 void Pause() {
2  foreach(BoltEntity entity in BoltNetwork.entities) {
3  if(entity.canFreeze) {
4  entity.Freeze(true);
5  }
6  }
7 }
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.

1 public void RenameEntity(BoltEntity entity, string name) {
2  entity.GetState<IPlayerState>().Modify().name = name;
3 }
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
void BoltEntity.InvokeMany ( Command  cmd,
int  duration,
CommandCallback  callback 
)
inline
void BoltEntity.InvokeOnce ( Command  cmd,
int  delay,
CommandCallback  callback 
)
inline
bool BoltEntity.IsController ( BoltConnection  connection)
inline

Checks if this entity is being controlled by the connection

Parameters
connectionThe connection to check
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
static implicit BoltEntity.operator UE.GameObject ( BoltEntity  entity)
inlinestatic
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.

1 bool forward;
2 bool backward;
3 bool left;
4 bool right;
5 
6 public override void SimulateController() {
7  IPlayerCommandInput input = PlayerCommand.Create();
8 
9  PollKeys();
10 
11  input.forward = forward;
12  input.backward = backward;
13  input.left = left;
14  input.right = right;
15 
16  entity.QueueInput(input);
17 }
void BoltEntity.ReleaseControl ( )
inline

Releases local control of this entity

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

1 BoltEntity controlledEntity;
2 
3 public override void ExitGame() {
4  controlledEntity.ReleaseControl();
5  BoltNetwork.server.Disconnect();
6 }
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.

1 BoltEntity controlledEntity;
2 
3 public override void ExitGame() {
4  ClientEvent evt = new ClientEvent();
5  evt.EvtCode = ClientEventCode.EXIT_GAME;
6 
7  controlledEntity.ReleaseControl(evt);
8  BoltNetwork.server.Disconnect();
9 }
void BoltEntity.RemoveEventCallback< T > ( Action< T >  callback)
inline

Remove a event callback to this entity.

Type Constraints
T :Bolt.Event 
void BoltEntity.RemoveEventListener ( UE.MonoBehaviour  behaviour)
inline

Remove an event listern from this entity

Parameters
behaviourThe behaviour to remove
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.

1 IEnumerator Stun(BoltEntity entity, float stunInterval) {
2  var controller = entity.controller;
3  entity.RevokeControl();
4  entity.GetState<ILivingEntity>().Modify().stunned = true;
5 
6  return new WaitForSeconds(stunInterval);
7 
8  entity.AssignControl(controller);
9  entity.GetState<ILivingEntity>().Modify().stunned = false;
10 }
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.

1 IEnumerator Stun(BoltEntity entity, float stunInterval) {
2  var controller = entity.controller;
3  CombatEventData evtData = new CombatEventData();
4  evtData.stunInterval = stunInterval;
5 
6  entity.RevokeControl(evtData);
7  entity.GetState<ILivingEntity>().Modify().stunned = true;
8 
9  return new WaitForSeconds(stunInterval);
10 
11  entity.AssignControl(controller);
12  entity.GetState<ILivingEntity>().Modify().stunned = false;
13 }
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.

1 public void SpawnPlayerVehicle(BoltEntity playerEntity) {
2  var car = BoltNetwork.Instantiate(BoltPrefabs.Vehicle, playerEntity.transform.position, playerEntity.transform.rotation);
3 
4  car.AssignContro(playerEntity.controller);
5  playerEntity.RevokeControl();
6  playerEntity.SetParent(car);
7 }
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.

1 IEnumerator Stealth(BoltEntity entity, float stealthInterval) {
2  int team = entity.GetState<IPlayerState>().team;
3 
4  foreach(BoltEntity adjEntity in BoltNetwork.entities) {
5  if(adjEntity == entity) continue;
6 
7  if(!adjEntity.StateIs%ltIPlayerState%gt()) continue;
8 
9  int adjTeam = adjEntity.GetState<IPlayerState>().team;
10  if(team != adjTeam) {
11  entity.SetScope(adjEntity.controller, false);
12  }
13  }
14 
15  yield return new WaitForSeconds(stealthInterval);
16 
17  entity.SetScopeAll(true);
18 }
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.

1 public void Disappear(BoltEntity entity) {
2  entity.SetScopeAll(false);
3 }
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.

1 public override void FireOnOwner(BoltEntity entity, BoltEntity target, AttackData attack) {
2  if(entity.isOwner) {
3  if(!target.StateIs(typeof(ILivingEntity))) {
4  return;
5  }
6 
7  target.GetState<ILivingEntity>().Modify().health -= attack.damage;
8  }
9 }
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.

1 public override void FireOnOwner(BoltEntity entity, BoltEntity target, AttackData attack) {
2  if(entity.isOwner) {
3  if(!target.StateIs<ILivingEntity>()) {
4  return;
5  }
6 
7  target.GetState<ILivingEntity>().Modify().health -= attack.damage;
8  }
9 }
void BoltEntity.TakeControl ( )
inline

Takes local control of this entity

Example: Spawning a server-side player entity.

1 void SpawnServerPlayer() {
2  var entity = Instantiate(BoltPrefabs.Player);
3 
4  entity.TakeControl();
5 }
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.

1 HotkeyConfig hotkeys;
2 
3 void SpawnServerPlayer() {
4  var entity = Instantiate(BoltPrefabs.Player);
5 
6  entity.TakeControl(hotkeys);
7 }
override string BoltEntity.ToString ( )
inline

String representation of the entity

Example: Logging new entities to the debug log.

1 public override void EntityAttached(BoltEntity entity) {
2  Debug.Log("Entity Attached: " + entity.ToString());
3 }
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().

1 public void FreezeAllPlayers() {
2  foreach(BoltEntity entity in BoltNetwork.entities) {
3  IPlayerState state;
4 
5  if(entity.TryGetState<IPlayerState>(out state)) {
6  entity.Freeze(true);
7  state.Modify().pausedByServer = true;
8  }
9  }
10 }

Property Documentation

IProtocolToken BoltEntity.attachToken
get
bool BoltEntity.canFreeze
getset

Whether the entity can be paused / frozen

Example: Pausing all entities that can be frozen.

1 void Pause() {
2  foreach(BoltEntity entity in BoltNetwork.entities) {
3  if(entity.canFreeze) {
4  entity.Freeze(true);
5  }
6  }
7 }
IProtocolToken BoltEntity.controlGainedToken
get
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.

1 void OnDeath(BoltEntity entity) {
2  if(entity.GetState%ltILivingEntity>().lives == 0) {
3  entity.controller.Disconnect(new ServerMessage("Game Over", "Zero Lives Remaining"));
4  }
5 }
IProtocolToken BoltEntity.controlLostToken
get
IProtocolToken BoltEntity.detachToken
get
bool BoltEntity.hasControl
get

Do we have control of this entity?

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

1 public void AddKnownEntity(BoltEntity entity) {
2  _minimap.AddNewEntity(entity.networkId, entity);
3 
4  if(entity.hasControl) {
5  _minimap.entities[entity.networkId].color = Color.Green;
6  }
7 }
bool BoltEntity.hasControlWithPrediction
get

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

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; } }

bool BoltEntity.isControlled
get
bool BoltEntity.isControllerOrOwner
get
bool BoltEntity.isFrozen
get

If this entity is currently paused

Example: Unpausing all frozen entities.

1 void Unpause() {
2  foreach(BoltEntity entity in BoltNetwork.entities) {
3  if(entity.isFrozen) {
4  entity.Freeze(false);
5  }
6  }
7 }
bool BoltEntity.isOwner
get

Did the local computer create this entity or not?

Example: Implementing an authoritative weapon firing method.

1 void FireWeapon(PlayerCommand cmd) {
2  if (activeWeapon.fireFrame + activeWeapon.refireRate <= BoltNetwork.serverFrame) {
3  activeWeapon.fireFrame = BoltNetwork.serverFrame;
4 
5  state.Fire();
6 
7  if (entity.isOwner) {
8  activeWeapon.OnOwner(cmd, entity);
9  }
10  }
11 }
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.

1 public override void BoltStartDone() {
2  foreach(BoltEntity entity in BoltNetwork.entities) {
3  if(entity.isSceneObject) {
4  if(entity.StateIs<IStructure>()) {
5  StructureFactory.Configure(entity);
6  }
7  else if(entity.StateIs<IMinion>()) {
8  MinionFactory.Configure(entity);
9  }
10  }
11  }
12 }
NetworkId BoltEntity.networkId
get

The unique id of this entity

bool BoltEntity.persistsOnSceneLoad
get

Should this entity persist between scene loads

Example: Pausing all persistent entities before changing scenes

1 public override void SceneLoadLocalBegin(string map) {
2  foreach(BoltEntity entity in BoltNetwork.entities) {
3  if(entity.persistsOnSceneLoad) {
4  entity.Freeze(true);
5  }
6  }
7 }
PrefabId BoltEntity.prefabId
get

The prefabId used to instantiate this entity

Example: Cloning an entity with prefabId.

1 BoltEntity Replicate(BoltEntity entity) {
2  return Instantiate(entity.prefabId, entity.transform.position, entity.transform.rotation);
3 }
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.

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