Script Addon Authoring
|
Creating your First Script Addon Before creating your first script addon, it is highly recommended that you watch the 7-minute MugMod script screencast:
This guide assumes you are roughly familiar with how EventScripts works, though overtime it will evolve to include new scripter information. This walkthrough will take you from start to finish in creating an example addon, and will also help in converting an existing (old-style) script pack to an addon.
1. Create Your Addon's File First, create a folder in cstrike/addons/eventscripts labeled 'myaddon': cstrike/addons/eventscripts/myaddon
cstrike/addons/eventscripts/myaddon/es_myaddon.txt
2. Learn about Events and Blocks Events are easy. If you had a file called 'player_say.cfg', then you would use 'event player_say' in your es_myaddon.txt instead. Blocks are pretty easy too - they're the non-event .cfg's of an addon. They can be called much the same way .cfg's are called, but instead of using 'exec events/myfile.cfg', you would use 'es_doblock myaddon/myblock'. For example, if your original script pack had something like this:
if (event_var(text) equalto "test") then exec events/test_win.cfg if (event_var(text) notequalto "test") then exec events/test_lose.cfg
es_xmsg Hooray, you said test!
es_xmsg You didn't say test. ...then the new format in es_myaddon.txt would be like this:
event player_say { if (event_var(text) equalto "test") then es_doblock myaddon/test_win if (event_var(text) notequalto "test") then es_doblock myaddon/test_lose } block test_win { es_xmsg Hooray, you said test! } block test_lose { es_xmsg You didn't say test. } However, one beautiful thing about using an addon instead of a scriptpack is that you can use things like 'do' and 'else' as well. This allows you to skip an 'if', which improves the performance of the script:
event player_say { if (event_var(text) equalto "test") do { es_doblock myaddon/test_win } else do { es_doblock myaddon/test_lose } } block test_win { es_xmsg Hooray, you said test! } block test_lose { es_xmsg You didn't say test. } This does tend to make the script more complicated for the writer, though - so if you are uncomfortable with this you can stick to 'if/then' until you get more comfortable using if/do/else.
block load { es_xsetinfo myaddon 1 es_makepublic myaddon } You can also use create an 'unload' block. This block is executed whenever the script addon is unloaded. You can use this to zero out variables that are no longer needed (frees up memory if you have long strings stored in variables), delete unnecessary keys, keygroups, etc. Be a responsible scripter and free up as much memory as you can. ;)
block load { es_xsetinfo myaddon 1 es_makepublic myaddon } block unload { es_xsetinfo myaddon 0 }
Add events to your script addon just as you would to a .cfg - just remember not to use a .cfg at the end of the event name. ;)
block load { es_xsetinfo myaddon 1 es_makepublic myaddon } block unload { es_xsetinfo myaddon 0 } event player_say { if (event_var(text) equalto "test") do { es_doblock myaddon/test_win } else do { es_doblock myaddon/test_lose } } event player_jump { es_msg Player event_var(es_username) jumped! }
Now you can write in blocks that you referred to in your events. Just for the sake of clarity later, you should add your blocks in the same order they're referred to in your events. That said, it really doesn't matter what order events and blocks are put into the file.
block load { es_xsetinfo myaddon 1 es_makepublic myaddon } block unload { es_xsetinfo myaddon 0 } event player_say { if (event_var(text) equalto "test") do { es_doblock myaddon/test_win } else do { es_doblock myaddon/test_lose } } event player_jump { es_msg Player event_var(es_username) jumped! } block test_win { es_xmsg Hooray, you said test! } block test_lose { es_xmsg You didn't say test. }
The BIGGEST thing you'll run into when using script addons is syntax problems. Brackets, tabs, unreachable code, parenthesis, and quotes can all be checked by using a VERY handy tool that Chun has made available here: Eventscripts Optimiser by Chun
7. Get Your Script Addon Running
es_load myaddon Note that there are no directory names in here - the script is loaded simply by referring to the name of your script due to the directory/file structure of script addons.
es_unload myaddon If you want to leave your plugin loaded, but you want to temporarily disable it, you can use: es_disable myaddon ...and enable it later with: es_enable myaddon This is perfect for scripts you want to disable that track data with strings or keygroups you don't want to lose - i.e. when a 'fun' map is loaded, you may want to disable a script without losing the data so you can enable it again the following map and have it pick up where it left off.
Situation 1: "I want this script ALWAYS running."
Situation 2: "I only want this script to run for certain maps."
Since server.cfg is executed before the map-specific .cfg, it will unload each time a new map starts, then load if you've added 'es_load myaddon' to the map's .cfg.
If you want to execute this from rcon:
bind x "rcon es_load myaddon"
bind x "rcon es_unload myaddon"
bind x "rcon es_disable myaddon" and bind x "rcon es_enable myaddon"
bind x "ma_rcon es_load myaddon"
bind x "ma_rcon es_unload myaddon"
bind x "ma_rcon es_disable myaddon" and bind x "ma_rcon es_enable myaddon"
Original forum post for EventScripts v1.0 that added Script Addon support
--Faaip 20:39, 16 July 2006 (EDT) |
