Adding Script Events to sys

From The DarkMod Wiki
Jump to: navigation, search

Originally written by Ishtvan on http://forums.thedarkmod.com/topic/1582

Btw, if anyone's curious how to add new "sys" events, i.e., events you call with $sys.<eventname> from the script, they're located here:

/src/game/script/script_thread.cpp (and .h)

Just add them there as you would add any other script events. See iddevnet.com under "11/11/04 Adding a new event" for a guide to adding script events. Here is a quick overview of how to add new event to "sys":

  • in script_thread.cpp add: const idEventDef EV_Thread_Print( "print", EventArgs('s', "text", ""), EV_RETURNS_VOID, "Prints the given string to the console.");
  • in script_thread.cpp under CLASS_DECLARATION( idClass, idThread ) add: EVENT( EV_Thread_Print, idThread::Event_Print )
  • add a method to idThread class: void idThread::Event_Print( const char *text )
  • add an entry to tdm_events.script (doom_events.script in DOOM 3): scriptEvent void print( string text );

This way you can add events that are global, for example as a frontend for accessing a global object contained in game_local.

The "sys" object is an artificial "global object" within the scripting language. The scripting language only sees actual spawned entities, and the global object. So you can't have abstract objects (like container classes contained within an entity or something). Therefore if you want an object to call functions without spawning an entity, the "sys" object is a good choice. (It's not a real object in the sdk either, it's just seen that way by scripting, and it can only call methods/events on the idThread class).

For an example of a script event attached to an entity other than "sys" see "wasDamaged" event on idPlayer.