Difference between revisions of "Writing Advanced Modules In C"

From ASSS Wiki
Jump to: navigation, search
m
(added knowledge)
Line 16: Line 16:
  
 
<pre>
 
<pre>
//example code goes here
+
typedef struct ThisIsData
 +
{
 +
Player *p;
 +
int number;
 +
} ThisIsData;
 +
 
 +
local int timerfunc(void *vp) //vp is void pointer, just an address that can point anywhere
 +
{
 +
ThisIsData *tid=(ThisIsData*)vp; //we know it points to our data
 +
 +
if(tid->number == 10)
 +
{
 +
//if it worked anything in here will work too
 +
}
 +
 
 +
return 0; //return 1 if you want timer to run again, or 0 if you want it to be removed
 +
}
 +
 
 +
anotherfunction()
 +
{
 +
ThisIsData *tid=amalloc(sizeof(ThisIsData)); //we must allocate memory because anything in this function is destroyed when it ends
 +
 
 +
tid->number=10
 +
 
 +
ml->SetTimer(timerfunc,1000,100,tid,0); //now set timer to activate in 1000 centiseconds, then repeat every 100. we are also sending the address of tid
 +
}
 
</pre>
 
</pre>
  
Line 26: Line 51:
  
 
<pre>
 
<pre>
//example code goes here
+
local void examplecommand(const char *command, const char *params, Player *p, const Target *t)
 +
{
 +
char *sentence=strdup(params); //strdup clones a string because strtok replaces " " with a \0
 +
char* word=strtok(params," "); //strtok reads everything until next \0 into word
 +
 +
while(word) //while word != null
 +
{
 +
if(!strnicmp(word,"a=",2))
 +
{
 +
//do stuff if first 2 letters of word are 'a' then '='
 +
 
 +
word+=2; //like move start of word 2 letters forward
 +
int check=atoi(word); //then read a number
 +
}
 +
else if(!strnicmp(word,"bc=",3))
 +
{
 +
//do stuff if first 3 letters of word are 'b' then 'c' then '='
 +
 
 +
word+=3; //make sure you move it 3 letters and not 2
 +
int check=atoi(word); //then read a number
 +
}
 +
else if(!stricmp(word,"-de"))
 +
{
 +
//do stuff if word is "-de"
 +
}
 +
word=strtok(NULL," "); //advance to next word, or set word to null if none left
 +
}
 +
}
 
</pre>
 
</pre>
  

Revision as of 20:25, 28 December 2010

This tutorial explains how to write advanced modules in C. It is assumed you know how to code and are familiar with how the ASSS code works.

This tutorial is a continuation of Writing Modules In C.


Some useful references:

http://qnxcs.unomaha.edu/help/product/neutrino/lib_ref/summary.html

http://www.cplusplus.com/reference/


Passing Data To Timers

Write me!

typedef struct ThisIsData
{
	Player *p;
	int number;
} ThisIsData;

local int timerfunc(void *vp) //vp is void pointer, just an address that can point anywhere
{
	ThisIsData *tid=(ThisIsData*)vp; //we know it points to our data
	
	if(tid->number == 10)
	{
		//if it worked anything in here will work too
	}

	return 0; //return 1 if you want timer to run again, or 0 if you want it to be removed
}

anotherfunction()
{
	ThisIsData *tid=amalloc(sizeof(ThisIsData)); //we must allocate memory because anything in this function is destroyed when it ends

	tid->number=10

	ml->SetTimer(timerfunc,1000,100,tid,0); //now set timer to activate in 1000 centiseconds, then repeat every 100. we are also sending the address of tid
}


Passing Multiple Arguments To Commands

Commands do not have to be in order. Write me!

local void examplecommand(const char *command, const char *params, Player *p, const Target *t)
{
	char *sentence=strdup(params); //strdup clones a string because strtok replaces " " with a \0
	char* word=strtok(params," "); //strtok reads everything until next \0 into word
	
	while(word) //while word != null
	{
		if(!strnicmp(word,"a=",2))
		{
			//do stuff if first 2 letters of word are 'a' then '='

			word+=2; //like move start of word 2 letters forward
			int check=atoi(word); //then read a number
		}
		else if(!strnicmp(word,"bc=",3))
		{
			//do stuff if first 3 letters of word are 'b' then 'c' then '='

			word+=3; //make sure you move it 3 letters and not 2
			int check=atoi(word); //then read a number
		}
		else if(!stricmp(word,"-de"))
		{
			//do stuff if word is "-de"
		}
		word=strtok(NULL," "); //advance to next word, or set word to null if none left
	}
}


Creating Callbacks

Write me!

//example code goes here


Creating Interfaces

Cover overwriting existing interfaces to replace old modules. Write me!

//example code goes here


Sending Packets To Players

Cover position packets, weapon packets, clientset stuff, etc. Write me!

//example code goes here


Using Advisors

Write me!

//example code goes here


Creating Advisors

Write me!

//example code goes here