Scripting Events: Difference between revisions

From Soldat2 Wiki
Jump to navigation Jump to search
mNo edit summary
m (move to Category:Modding)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
The main method for reacting to gameplay events in Soldat 2 scripting is by registering ''listeners''. This is done by calling the <code>AddListener()</code> function, usually on the <code>Eventor</code> object.
The main method for reacting to gameplay events in Soldat 2 scripting is by registering ''listeners''. This is done by calling the <code>AddListener()</code> function, usually on the <code>Eventor</code> object.
[[Category:Modding]]


== Eventor Events ==
== Eventor Events ==
Line 6: Line 8:
The event types that are used with Eventor are defined by the <code>Events</code> enum, which currently contains:
The event types that are used with Eventor are defined by the <code>Events</code> enum, which currently contains:
* None
* None
* New_Connection
* New_Connection (GlobalPlayerConnectionEvent)
* Created
* Created
* Destroyed
* Destroyed
Line 12: Line 14:
* Add_Player
* Add_Player
* Remove_Player
* Remove_Player
* Player_Joined
* Player_Joined (GlobalPlayerEvent has .Player)
* Player_Assigned
* Player_Assigned
* Player_Left
* Player_Left
Line 21: Line 23:
* Impacted
* Impacted
* Weapon_Selected
* Weapon_Selected
* Trigger
* Trigger (GlobalTriggerEvent)
* Match_Waiting
* Match_Waiting
* Match_Warmup
* Match_Warmup
* Match_Started
* Match_Started
* Match_Ended
* Match_Ended
* Hit
* Hit (GlobalHitEvent)
* Camera_Target_Changed
* Camera_Target_Changed
* Camera_Created
* Camera_Created
Line 40: Line 42:
* Construct_RemoveBody
* Construct_RemoveBody
* Properties_Changed
* Properties_Changed
* Item_Dropped
* Item_Dropped (GlobalItemEvent has .Item and .Holder)
* Item_Picked
* Item_Picked (GlobalItemEvent has .Item and .Holder)
* AssignedNetworkId
* AssignedNetworkId
* Changed_Team
* Changed_Team
Line 54: Line 56:
* 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.
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 by the engine. For example, something equivalent to this is used in the official <code>Climb.cs</code> script to detect when any player gets within 1.5 range of a checkpoint:
<syntaxhighlight lang="C#">
void FixedUpdate() // runs 10 times per second
{
    for (int i = checkpoints.Count - 1; i >= 0; i--)
    {
        foreach (Player player in Players.Get.GetAlive())
        {
            // for each (player,checkpoint) pair, check if the distance between them is less than 1.5
            if ((player.controlled.transform.position - checkpoints[i].transform.position).magnitude < 1.5f)
            {
                checkpoints.RemoveAt(i);
                lastCheckpointPlayer = player;
                break;
            }
        }
    }
}
</syntaxhighlight>

Latest revision as of 11:48, 10 June 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 (GlobalPlayerConnectionEvent)
  • Created
  • Destroyed
  • Died
  • Add_Player
  • Remove_Player
  • Player_Joined (GlobalPlayerEvent has .Player)
  • Player_Assigned
  • Player_Left
  • Player_Changed_Team
  • Player_Unassigned
  • Level_Loaded
  • Rules_Loaded
  • Impacted
  • Weapon_Selected
  • Trigger (GlobalTriggerEvent)
  • Match_Waiting
  • Match_Warmup
  • Match_Started
  • Match_Ended
  • Hit (GlobalHitEvent)
  • 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 (GlobalItemEvent has .Item and .Holder)
  • Item_Picked (GlobalItemEvent has .Item and .Holder)
  • 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)

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 by the engine. For example, something equivalent to this is used in the official Climb.cs script to detect when any player gets within 1.5 range of a checkpoint:

void FixedUpdate() // runs 10 times per second
{
    for (int i = checkpoints.Count - 1; i >= 0; i--)
    {
        foreach (Player player in Players.Get.GetAlive())
        {
            // for each (player,checkpoint) pair, check if the distance between them is less than 1.5
            if ((player.controlled.transform.position - checkpoints[i].transform.position).magnitude < 1.5f)
            {
                checkpoints.RemoveAt(i);
                lastCheckpointPlayer = player;
                break;
            }
        }
    }
}