BomberUnits
...a Unity learning project
NetEvent< NetDataType > Class Template Reference

This is a convenience wrapper for Photons networked event system. More...

+ Inheritance diagram for NetEvent< NetDataType >:
+ Collaboration diagram for NetEvent< NetDataType >:

Classes

class  UnityEventWithCustType
 It is somehow necessary to create this and get rid of the template parameter. More...
 

Public Member Functions

 NetEvent (Target target=Target.All, bool reliable=true, bool cachedForRoom=false)
 Constructor taking optional settings. More...
 
 NetEvent (NetDataType defaultData, Target target=Target.All, bool reliable=true, bool cachedForRoom=false)
 Optional constructor allowing to give the event default data Invoke() can be called without repeating the default data now. More...
 
void AddListener (UnityAction< NetDataType > callback)
 The given callback will receive data of the defined type.
 
void RemoveListener (UnityAction< NetDataType > callback)
 Removes the given callback from receiving events.
 
void Invoke (NetDataType data, string userID="")
 Sends the event to the set targets, calling the registered callbacks with the given data. More...
 
void Invoke (string userID="")
 This version of Invoke does not require data. The event has to have default data set though!
 
void OnEvent (EventData photonEvent)
 Forwards the Photon Event to the held Unity event, which distributes to registered callbacks.
 

Private Member Functions

 ~NetEvent ()
 The destructor has to remove the registration for Photon event data.
 
int GetSpecificTargetActorNr (string userID)
 Get the actorNr for a specified userID. Returns -1 if the user cannot be found or was not given.
 

Private Attributes

byte eventCode
 
UnityEventWithCustType localEvent
 
Target target
 
NetDataType defaultData
 
bool reliable
 
bool cachedForRoom
 
bool hasDefaultData
 

Detailed Description

This is a convenience wrapper for Photons networked event system.

It primarily allows for quick raising and registering to these events, removing some low-level actions. They can be set up for any Type of data package implementing INetDataPkg for serialization. Usage:

  1. Declare a specific NetEvent with data type to transmit as member of some class.
  2. Instantiate the NetEvent object in Awake/Init, specifying its exact settings in the constructor:
    • Default data to be sent when invoked without handing data.
    • Target (All, Others, Specifc client)
    • Reliability of the transmission
    • Caching in the room for players yet to join
  3. Subscribe to the event by AddListener() where you need it.
  4. Invoke where applicable, handing the data of defined type.

Internally, instances of these classes map the single existing PhotonEvent (identified via ID) to local UnityEvents, which can be comfortably subscribed to.

Type Constraints
NetDataType :INetDataPkg 
NetDataType :new() 

Definition at line 36 of file NetEvent.cs.

Constructor & Destructor Documentation

◆ NetEvent() [1/2]

NetEvent ( Target  target = Target.All,
bool  reliable = true,
bool  cachedForRoom = false 
)
inline

Constructor taking optional settings.

This most importantly registers this instance to receive Photon event data.

Definition at line 60 of file NetEvent.cs.

◆ NetEvent() [2/2]

NetEvent ( NetDataType  defaultData,
Target  target = Target.All,
bool  reliable = true,
bool  cachedForRoom = false 
)
inline

Optional constructor allowing to give the event default data Invoke() can be called without repeating the default data now.

Definition at line 75 of file NetEvent.cs.

Member Function Documentation

◆ Invoke()

void Invoke ( NetDataType  data,
string  userID = "" 
)
inline

Sends the event to the set targets, calling the registered callbacks with the given data.

You can optionally hand the id of a user to send only to that user.

Definition at line 103 of file NetEvent.cs.


The documentation for this class was generated from the following file: