Scripting Events

From Soldat2 Wiki
Revision as of 15:41, 16 January 2023 by AL (talk | contribs)
Jump to navigation Jump to search

The main method for reacting to gameplay events in Soldat 2 scripting is by registering listeners. This is done by calling the AddListener() function, usually on the Eventor object.

Eventor Events

In the case of Eventor listeners, you will need to specify both the event type to listen for, as well as a function to be called when the event occurs. Callback functions should have the signature void cb_func(IGameEvent).

The event types that are used with Eventor are defined by the Events enum, which currently contains:

  • None
  • New_Connection
  • Created
  • Destroyed
  • Died
  • Add_Player
  • Remove_Player
  • Player_Joined
  • Player_Assigned
  • Player_Left
  • Player_Changed_Team
  • Player_Unassigned
  • Level_Loaded
  • Rules_Loaded
  • Impacted
  • Weapon_Selected
  • Trigger
  • Match_Waiting
  • Match_Warmup
  • Match_Started
  • Match_Ended
  • Hit
  • Camera_Target_Changed
  • Camera_Created
  • Powerup_Started
  • Powerup_Ended
  • Bullet_Created
  • Bot_Assigned
  • Flag_Captured
  • Flag_Returned
  • Flag_Grabbed
  • Use_Weapon
  • Construct_AddBody
  • Construct_RemoveBody
  • Properties_Changed
  • Item_Dropped
  • Item_Picked
  • AssignedNetworkId
  • Changed_Team
  • Request_Spawn
  • End_Condition
  • Reserved

A full example of an AddListener call is Eventor.AddListener(Events.Died, OnPlayerDied);. These calls are usually placed within the Awake() function of the script, which is executed when the script is loaded. Note that each AddListener call should be paired with a RemoveListener call (usually in the OnDestroy() function) to ensure that the script does not continue to respond to events after it has been disabled. See the Unity manual entry for the MonoBehaviour class for other special functions.

Other Events

Not all necessary game events are currently accessible with just the Eventor object, methods for adding listeners to other useful events are listed below:

  • Player chat events: GameChat.instance.OnChat.AddListener(), only takes a callback function as an argument, required signature for callback is void cb_func(Player, string)