Difference between revisions of "Listening to game events in CS:GO"

From Valve Developer Community
Jump to: navigation, search
(Created page)
 
m (words)
Line 1: Line 1:
CS:GO vscripts API is missing crucial functions for event listening. However, you can still use event data fetched from [[logic_eventlistener]]s in your custom maps. Fetching event data option was added in CS:GO in 2016.
+
CS:GO vscripts API is missing crucial functions for event listening. However, you can still use the event data fetched from [[logic_eventlistener]]s in your custom maps. Fetching event data option was added to CS:GO in 2016.
  
There are multiple ways of acessing the script scope of the eventlistener, which is where the event data are dumped. Only one method will be demonstrated here.
+
There are multiple ways of acessing the script scope of an event listener, which is where the event data are dumped. Only one method will be demonstrated here.
  
 
You can find event data contents in <code>/resources/*events.res</code> files.
 
You can find event data contents in <code>/resources/*events.res</code> files.
Line 8: Line 8:
 
Create your event listener in Hammer Editor, add targetname and enable the <code>FetchEventData</code> keyvalue. Add an output that executes an ''OnGameEvent_'' function in itself with the <code>event_data</code> parameter. Note that the targetname and function names are arbitrary, but it is always good practice to be consistent.
 
Create your event listener in Hammer Editor, add targetname and enable the <code>FetchEventData</code> keyvalue. Add an output that executes an ''OnGameEvent_'' function in itself with the <code>event_data</code> parameter. Note that the targetname and function names are arbitrary, but it is always good practice to be consistent.
  
Example event <code>item_purchase</code>, logic_eventlistener:
+
Example event <code>item_purchase</code>  
 +
 
 +
logic_eventlistener keyvalues:
  
 
<pre>
 
<pre>
Line 14: Line 16:
 
EventName:  item_purchase
 
EventName:  item_purchase
 
FetchEventData: Yes
 
FetchEventData: Yes
 +
</pre>
 +
 +
logic_eventlistener output:
  
 +
<pre>
 
OnEventFired > item_purchase > RunScriptCode > ::OnGameEvent_item_purchase(event_data)
 
OnEventFired > item_purchase > RunScriptCode > ::OnGameEvent_item_purchase(event_data)
 
</pre>
 
</pre>
Line 38: Line 44:
 
After following instructions on installation and setting up required event listeners, you can get player info from their script scope, and get player script handles from their userids using <code>VS.GetPlayerByUserid</code>.
 
After following instructions on installation and setting up required event listeners, you can get player info from their script scope, and get player script handles from their userids using <code>VS.GetPlayerByUserid</code>.
  
Example code that prints the Steam names of players killing and dying:
+
Example code that prints Steam names of players in the <code>player_death</code> event:
  
 
<source lang=cpp>
 
<source lang=cpp>
Line 65: Line 71:
 
</source>
 
</source>
  
 +
== External links ==
 +
* [https://github.com/samisalreadytaken/vs_library vs_library]
 +
* [https://blog.counter-strike.net/index.php/2016/06/15010/ Release Notes for 6/28/2016]
  
 
== See also ==
 
== See also ==
Line 70: Line 79:
 
* [[L4D2 EMS/Appendix: Game Events|L4D2 Game Events]]
 
* [[L4D2 EMS/Appendix: Game Events|L4D2 Game Events]]
 
* [[VScript]]
 
* [[VScript]]
 
== External links ==
 
* [https://github.com/samisalreadytaken/vs_library vs_library]
 
* [https://blog.counter-strike.net/index.php/2016/06/15010/ Release Notes for 6/28/2016]
 

Revision as of 16:00, 21 May 2020

CS:GO vscripts API is missing crucial functions for event listening. However, you can still use the event data fetched from logic_eventlisteners in your custom maps. Fetching event data option was added to CS:GO in 2016.

There are multiple ways of acessing the script scope of an event listener, which is where the event data are dumped. Only one method will be demonstrated here.

You can find event data contents in /resources/*events.res files.

Setting up

Create your event listener in Hammer Editor, add targetname and enable the FetchEventData keyvalue. Add an output that executes an OnGameEvent_ function in itself with the event_data parameter. Note that the targetname and function names are arbitrary, but it is always good practice to be consistent.

Example event item_purchase

logic_eventlistener keyvalues:

targetname: item_purchase
EventName:  item_purchase
FetchEventData: Yes

logic_eventlistener output:

OnEventFired > item_purchase > RunScriptCode > ::OnGameEvent_item_purchase(event_data)

In your script:

// Your event function
function OnItemPurchase(data)
{
	ScriptPrintMessageChatAll(data.weapon + " is purchased.")
}

// Global function binding
::OnGameEvent_item_purchase <- OnItemPurchase.bindenv(this)

Getting player userid, SteamID and Steam names

As mentioned before, the API to get these information is not available. The workaround to getting userids is listening to an event that dumps a userid which was triggered by a known player; then getting SteamIDs from the player_connect event data using these associated userids.

You can simplify all of this by using vs_library, a third party vscript library that handles it for you.

After following instructions on installation and setting up required event listeners, you can get player info from their script scope, and get player script handles from their userids using VS.GetPlayerByUserid.

Example code that prints Steam names of players in the player_death event:

VS.AddEventCallback("player_death", function(data)
{
	local victim = VS.GetPlayerByUserid(data.userid)
	local attacker = VS.GetPlayerByUserid(data.attacker)

	// initial names
	local vicName = "NULL"
	local attName = "NULL"

	// either of players can be null if they were not found
	if( victim )
	{
		vicName = victim.GetScriptScope().name
	}

	if( attacker )
	{
		attName = attacker.GetScriptScope().name
	}

	printl(attName + " KILLED " + vicName + " USING " + data.weapon)
})

External links

See also