WarCraft: Source

 

ExampleRace

Page history last edited by freddukes@... 1 yr ago

Example Race ... My first race 


 

Right, python works by Local Variables... This means that all variables defined inside the script, will stay inside the script. Why you ask? Of course, to make it easier for the script to know where everything is, and save Huge amounts of speed. This also now means, that all your variables to not have to be unique... (Of course there are a few exceptions, which I'll tell you about later...)

 

If you don't know already, python uses things called 'methods' otherwise known function. They act VERY similarly to an Eventscript block... you define a method by calling the command 'def' e.g.

 

def MyMethod():

Now, note 2 different things here compared with ESS Blocks. The first being the brackets, I'll explain what these mean later, but for now, just ignore them. Look at the colon after the ()... A colon (Smile means that the following line will be an indented line... You will only ever have a : at the end of certain lines: such as;

 

for X in X:

while X <condition> X:

if x <condition> x:

def x:

 

These are the most common... You can probably guess what each one does, so I not explain. Right, now lets go back to the brackets thing in myFunction... Now listen closely, this is one of the hardest things you'll EVER learn in python (truely, once you've got your head round this, you'll realise just how easy python is...)

 

now, I said all values are local right... Local  means that they are only recognised WITHIN THE FUNCTION YOU DECLARE THEM!!!... e.g.

def myFunction():
   msg = 'hello'
 
def mySecondFunction():
   es.msg(msg)

 

This code would bring up an error saying 'msg referenced before assignment'. This means that the variable hasn't been assigned yet, and this is true because when you declared msg, it 'scope' (how local/global the variable is) is only inside myFunction. This means I cannot call the variable made in myFunction to another function... That is, unless I tell it to. Now, there are 2 common ways of making a variable recognised in other functions, I will explain them now... Right, the first way, and the 'best' way, is to simply 'pass' the variable from one function to the next. This is useful, because the variable is still only recognised in 2 different functions, so it saves space Wink... Always try and keep the scope of the variables as low as possible. We pass the variable on from one function to the next, by using the brackets Smile such as...

 
def myFunction():
   message = 'hello'
   mySecondFunction(message)
 
def mySecondFunction(msg):
   es.msg(msg)

 

Now.. You may think 'what the hell is he doing'... Right, well to do a method, you just simply type the method out. This is very similar to es_doblock scriptname/block... imagine that... Now to call the method, you just have to use the method name. Simple, yes... Now as you can see, this time, I've typed something inside the brackets. Inside the brackets, is my variable; message... On the sending function... (e.g. the one Passing the variable)... The variable HAS to be the same name as you originally declared it.. e.g. I declared 'hello' to the variable message, thus I must pass the variable name as message. However, on the receiving end, I can call the variable whatever I want... In this case, I've renamed message msg... This is (in my opinion) the hardest way to make a variable global, however, it's the best. When's the best time to use this sort of global variable? When you are simply going from one function to the next. However, if one function does not 'call' (call just means 'doblock'), then there is no way to pass the function, so we must use other means.

 

The second way, (and in my opinion the easier, but slower way..) is to call the command global at the top of the function before you set any variables... lets have a look at that method again, this time though, myMethod will not call mySecondMethod:

def myMethod():
   global message
   message = 'hello'
 
def mySecondMethod():
   es.msg(message)

Now as you can see, in this case, I cannot re-name the variable. This is because that variable has been made global to the script, so the value inside the variable will only be able to be recognised by the same variable name... e.g. if I define message to be hello, msg will contain nothing... The second thing many people get confused about, is when NOT to use a global... the global command will simply make a variable global, when a value inside the variable is changed/set... This means I can read the value, do whatever I want with the value, unless I don't change it, then I don't need a global.. e.g.

def myMethod():
   global message
   message = 'hello'
 
def mySecondMethod():
   if message == 'hello':
       es.msg(message)

As you can see, I haven't changed the value in any way shape or form in mySecondMethod, so I don't need a global... The general rule for if it needs a global or not is if it has a single "=" sign (note in python 2 "==" means 'equalto', whereas one "=" means equals... DONT get these 2 mixed up).

 

Now, so that's one of the hardest things you'll learn in python... not that hard is it ^_^...

 

The last thing I want to talk to you about is ways of grouping information together... Keygroups are slow ways of storing informations... The new ways are: lists, sets and dicts. (there are also keyvalues which act in the exact same way as keygroups, but they're a lot more complex... We'll leave those out for now, you don't need them.) Dicts, (short for dictionarys) are sooooooooo useful once you know how to use them correctly. It's how the information of the users will be stored on the server, it's just as fast as SQLite, (if not faster,) and they're easier to edit/change/save/do what you want to. Imagine dicts as keygroups... We'll say that a dict is the keygroup itself, and then a dict has a 'key'. However, this is where it get's cooler.. whereas a normal keygroup can only have a keygroup -> key -> keyvalue... dicts can have keygroup -> key -> key -> key -> key -> key -> keyvalue Smile... It can contain as MANY keys as you want Smile (This is useful to say store all of the WCS settings in, all your XP in, and each race etc...) with dicts, virtually all information can be kept inside the same dict... now, how do you create a dict? simple

 

my_dict = {}

That is it, you've just created your dict.. now who said python was tough Razz... Okay, let's add things to the dict...

my_dict['my_key'] = 5

You've actualy just assigned a keyvalue to a key Smile yet another thing we can do, we can have dicts as short or long as possible... Here, we've assigned the value of '5' to the root 'my_key'. To access the value again you just do

my_value = my_dict['my_key']

Yes, it really is that simple.. To add 2 keys to my_dict, you can just do this..

my_dict['my_key']['my_secondkey'] = 4

But note... You can only ADD another key on when you've added the PREVIOUS key on.. e.g.

my_dict = {}
my_dict['my_key']['my_secondkey'] = 4
# This is incorrect
 
my_dict = {}
my_dict['my_key'] = {}
my_dict['my_key']['my_secondkey'] = 4
# correct

As you can see, you have to build them up one at a time.. there is an advanced way to do it so you can do it in one line, but lets not confuse you yet Smile... That's it for dicts really... As you can see, they are extremely handy for storing data...

 

The next way, is a list. Lists and sets really are both 'lists' and they virtually do the same thing. For this reason, we'll just teach you lists as they're easier to understand, and more useful to you. Okay, now like a dict, you have to first create your list.. to do this just type:

list = []

This will create a list Smile... Yes it is that simple... Now, a list is only good for... well, holding a list of items.. for example, many people use lists to define the pistols list..

list = ["deagle","usp","glock","fiveseven","elite","p228"]

These are useful for 'for' and 'in' commands... This is a lot faster than saying

 

list = "deagleuspglockfivesevenelitep228"

 

as for example if we wanted to test if usp was in list in the second case.. it'd be...

 

usp == dea.. False

usp == eag.. False

usp == agl.. False

usp == gle.. False

usp == leu.. False

usp == lus... False

usp == usp.. True

 

Imagine if we was trying to test if if something was in p228, it'd take ages.. however, with a list the usp case would be

 

usp == deagle false

usp == usp true...

 

It tests each seperate item individually.. likewise for the for command..

for gun in pistols:
   es.msg("found %s"%gun)

I'll tell you what the %s does now... the %s is a substitute for any string... if you've used es_format or es_formatv before it is virtually the same, only instead of numbers, it's 's'.. Now at the end of the string (e.g. the end of the quotation mark" you have to define that the 'string replacement' starts there... so to define that, you put a %... Now anything that follows the % will be the variable to be replaced into the string... If you have more than one variable, they need to be inside brackets..

for gun in pistols:
   gun2 = 'weapon_%s'%gun
   es.msg("found %s : %s"%(gun,gun2))
   # displays found deagle: weapon_deagle

Now, lets begin to learn the syntax of creating your own race...

 

###################
# Import module
import es
import random
import gamethread
 
import wcs
from wcs import wcs
 
###################
#    PUBLISH INFO
info = es.AddonInfo()
info.name = 'My First Race'
info.author = 'Freddukes'
 
###################
#     RACE INFO
#
RaceName = 'first race'
# This is the name of the mod... We assign it to a global variable for easy reference
# It must be exactly the same name as the file name, including capital letters
race = wcs.Race(RaceName)
race.registerWeaponRestriction('allow only',(['knife','glock','usp']))
 
###################
#    SKILL INFO
# Syntax: registerSkill("skill name", <amount of levels>, <level interval>, "description")
 
race.registerSkill('My first skill',1,1,"A test to see if my skill works")
race.registerSkill("I am god",4,3,"Random chance to put you in god mode")
 
##################
#     Ultimate Info
# Syntax: registerUltimate("ultimate name", <required race level>, <max level>, <Level Interval>, "Description")
 
race.registerUltimate("I am ledgend",8,4,2,"Makes you turn red and everyones screen in a distance goes blury"
 
 
 
def player_spawn(event_var):
   userid = int(event_var['userid'])
   # This returns the integer of the userid... All events return as string, we want the userid as a number (integer)
 
   first_level = wcs.GetLevel(userid, RaceName, 'My first skill')
   # Returns the value of the level 'firstrace' and stores it in first_level
 
   if first_level:
# If first level is True, I.E a positive number
       es.tell(userid,'#green','Look, Your skill worked!'
 
   god_level = wcs.GetLevel(userid, RaceName, 'I am God')
   if god_level:
       wcs_rand = random.randint(1,5)
# gets a random number between 1 and 5
 
       if god_level <= wcs_rand:
           player = wcs.Command(userid)
# Register the player for a custom command skill
 
           player.God(30)
# Turn the player into god mode for 30 seconds
 
 
 
def player_ultimate(event_var):
   # Triggered whenever a user has used his ultimate and the ultimate counter is set to 0
 
   player = wcs.Command(userid)
# Register the player for a custom command skill
 
   player.SetCoolDown('I am ledgend',30)
# Registers the cooldown effect for 30 seconds for the I am ledgend ultimate.
 
   ledgend_level = wcs.GetLevel(userid, RaceName,"I am ledgend")
 
   player.Near(ledgend_level * 50, shake)
# The distance effected is the level multiplied by 50... Everyone inside this distance will now be passed through to the 'shake' function
 
 
 
def shake(userid, attacker):
# Attacker is the person to run the ultimate, userid is the victim who was close to him
 
   player = wcs.Command(userid)
# Register the player for custom comand skill
 
   player.Shake(5,10)
# Shake the victims screen for 5 seconds with a 'violence' of 10
 
   att = wcs.Command(attacker)
# register the attacker for custom command skill
 
   att.Color('red')
# Turn the attacker red
 
   gamethread.delayed(5,att.Color)
   # Reset the color of the user after 5 seconds

 

 

 

 

 

 

 

 

 

Hopefully you can understand how much freedom you've now got. This is just a small example, some libs (even though not useful) will let you send e-mails to people etc. Some Librarys will also let you browse the internet and open sites, download things. With python, the worlds the limit Wink

 

 

 

 

 

 
 

 

 

Comments (0)

You don't have permission to comment on this page.