My Second Addon

From EventScripts Community Encyclopedia


Tutorial: My Second Addon

Requires: ES 2.x
Difficulty: Easy
Author: Saul Rennison
Contributors: ~
Length: 5 mins

Tutorial Overview

Table of Contents

Contents

Description

In this Tutorial, we will create a simple addon for EventScripts 2.0 (AKA ESP / ESPy). The addon will simply set the players health when they say /sethealth using Playerlib and the es.regsaycmd function. Please make sure you have a basic knowledge of the Python language, for people that can already program, then I would start off with this tutorial.

Tutorial Content

Start off

Open your favorite Python editor, if you don't have one, I recommend Notepad++ or PSPad; or, if you have enough money ActiveState Komodo. You can also just use Notepad or any text editor you like. Create a new document, and add the 2 following lines:

import es
import playerlib

Start the main script

load() event

After adding the import lines, we will start by adding the load() event.

def load():
     if not es.exists('saycommand', '/sethealth'):
          es.regsaycmd('/sethealth', 'healthyaddon/sethealth')

In the snippet above, we called es.exists() to see if the say command /sethealth exists, and then if it does not, register the say command.

/sethealth command

Just after the load() event, add this:

def sethealth():
     health = es.getargv(1)
     userid = es.getcmduserid()
     player = playerlib.getPlayer(userid)
     player.set('health', health)

In the sethealth() function, we are getting the first parameter using es.getargv(1) (health), the userid using es.getcmduserid(), getting the player object from playerlib.getPlayer(userid) and then setting the health using the set() method on the player object.

Save the addon

Now we have completed the addon, save it as healthyaddon.py under:

../cstrike/addons/eventscripts/healthyaddon/

Load up srcds, and enter:

es_load healthyaddon

You should get in the console:

Loaded: healthyaddon

Now, join the server and say:

/sethealth 200

Your health should now be set to 200!

Conclusion

You will now (hopefully) have a satisfactory understanding of how EventScripts works with Python, how basic playerlib works and command-based EventScripts programming works.

Thanks for reading

blog comments powered by Disqus