Scripting Events: Difference between revisions
No edit summary |
No edit summary |
||
Line 53: | Line 53: | ||
Not all necessary game events are currently accessible with just the Eventor object, methods for adding listeners to other useful events are listed below: | 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: <code>GameChat.instance.OnChat.AddListener()</code>, only takes a callback function as an argument, required signature for callback is <code>void cb_func(Player, string)</code> | * Player chat events: <code>GameChat.instance.OnChat.AddListener()</code>, only takes a callback function as an argument, required signature for callback is <code>void cb_func(Player, string)</code> | ||
If an event is not available for a specific gameplay element you are interested in responding to, it is also possible to use a polling approach to detect that event by using either the <code>Update</code> or <code>FixedUpdate</code> functions which are called periodically. |
Revision as of 15:50, 16 January 2023
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 isvoid cb_func(Player, string)
If an event is not available for a specific gameplay element you are interested in responding to, it is also possible to use a polling approach to detect that event by using either the Update
or FixedUpdate
functions which are called periodically.