Python
From TibiaAuto Wiki
Contents |
To The Reader
This Tutorial assumes that you have atleast basic knowledge of python. If not, there are pleanty of tutorials on the web, but this is not the place to learn the basics. Once you know the basics, come back here. The tutorial will start with basic python usage, so once you know the basics you'll be fine.
IDLE
IDLE is the integrated developer enviroment for python. To open IDLE, click on Python Scripts on the main TA window. In the new window that opens, click on IDLE, and idle will open. IDLE will allow you to run any python code and immediately see the output. Try print 1+1, you should get 2. Also, from IDLE you can open a new window to script in. Click file-->New Window and a blank window will come up. Save it as "scriptname".py, and you've got a python script which does absolutely nothing.
The Basics
There is a basic skeleton that all TA scripts must have to function properly.
class ModuleSkeleton:
def getName(self):
return "Skeleton"
def getVersion(self):
return "1.0"
def getFunDef(self,nr):
if (nr==0): return (0,1000,self.skeletonFunction);
return ();
def getConfigParam(self,nr):
return ();
def skeletonFunction(self,params):
return();
tibiaauto.registerPlugin(ModuleSkeleton);
The first thing to realise is that TA uses python classes to organise scripts, so we must first define a class: class ModuleSkeleton: does this for us.
Next come 4 functions that TA uses to get information about the script. getName() getVersion() getFunDef() getConfigParam()
TA will call each of these functions, and if one is missing the script wont load.
getName(self): is just a function which returns the name which will show up in the TA python window.
getVersion(self): is also just a simple function which returns the version of the script.
getFunDef: is responsible for telling TA three things. The line if (nr==0): return (0,1000,self.skeletonFunction); is the line that actually returns the information to TA itself. The first integer in the return statement tells TA how to run the script. 0 defines the script as a "period" script, which means it will be run repetitively. If that number where to be 1, it would be a message script. The second integer sets how often the script is to be run in milliseconds, so 1000 will set the script to run every 1 second. The thrid thing TA needs to know is which function it needs to run, so we tell it the function whcih is self.skeletonFunction in this example.
getConfigParamself,params): is a function that loads parameteters into a list. Ill talk about these later.
The firth function is the function that is called when we run the script. Its name must be the same as the function we told TA to run in getFunDef()
Finally, tibiaauto.registerPlugin(ModuleSkeleton) registers the class as a TA script. You don't need to know exactly what it does, just remember that the class name is what goes between the ().
Hello World
Now that we know how to make a TA script, lets make a script that does something. The first thing we need to know is how to make your character say something. To do this, we use the built in functions of TA. A whole list can be found on the wiki, but they won't mean much to you unless you know what your looking for. The build in functions are divided into seven modules:
- tareader
- tasende
- tamap
- taregexp
- taalice
- taitem
- takernel
Its pretty easy to figure out what they do, and if you can't they're explained under the scripting wiki. The module well be focusing on first is the tasender module, namely because we need to "send" a message to tibia. To call a module, we must first import it using "import <modulename>". However, these seven modules are loaded at startup, so theres no need to import them. To import our own modules that we will build later on, you will need to use "import". Once the module is imported, to access a function within the module we use <modulename>.<functionname>. The function we want to use is say(), and its located in TA sender, so to say something we use tasender.say('string'). Now we want to put this into our script somewhere where it will be called, so we put it into the function that we told TA to call.
class ModuleHelloWorld:
def getName(self):
return "Hello World"
def getVersion(self):
return "1.0"
def getFunDef(self,nr):
if (nr==0): return (0,1000,self.hello);
return ();
def getConfigParam(self,nr):
return ();
def hello(self,params):
tasender.say('hello world, im botting');
return();
tibiaauto.registerPlugin(ModuleHelloWorld);
Now load the script and enable it. Your character will start saying hello world, im botting every one second.

