PDA

View Full Version : How to add more perl functions.


cofruben
07-26-2004, 01:11 AM
Hello all,in my chapter,I will explain the basics of function creation,including mob,zone and other class.This tutorial is very simple,and intended to explain this for people which don't know C++ very well.

First of all.There are two files that you will need to use for adding functions.

-Parser.cpp:Here is where we will add the function.This looks like:
else if (!strcmp(strlwr(command),"save")) { // khuong
if (mob && mob->IsClient()) mob->CastToClient()->Save();
}

-EmbParser.cpp:We need to add here the function "declaration",to make perl know that we have another function to load!.This looks like:

"sub save{push(@cmd_queue,{func=>'save',args=>join(',',@_)});}"

When you use "quest::save();",you are calling the function in parser.cpp called save.You are giving 0 arguments for extra info.

I will teach you how to get those arguments to use later in the function(for example the command quest::summonitem(22); gives 22 as argument,for item id).


Let's get in the function code,in this case is 'save':
else if (!strcmp(strlwr(command),"save")) { // khuong
if (mob && mob->IsClient()) mob->CastToClient()->Save();
}
the first line,just tells the function name,where you need to change "save" to your function name.(it just compares the function used in the perl file,till it gets the correct function with same name).
You won't need to care about it to make your function,just remember to change the name.

In the second line,you can see what the function does.You need to know the basics of c++ for this.if(mob) just is used to know if the client who triggered it exists.For a example,if a timer triggers it,we will have no 'mob',if we hail a NPC and you trigger something,then 'mob' will exist.Then,if(mob->IsClient()),is used to know if the thing who triggered it,was a client.Easy,right?That would prevent crashes,for example if we are using a timer and we try to change it's level,it will crash.So if(mob && mob->IsClient()) is used to know if the thing which triggered it exists,and the thing which triggered it is a client.

If those two are TRUE,then it is going to use the function save to the client ( mob->CastToClient()->Save(); ).

We need to know some things first:
-mob:Is the 'thing' which triggered the action.For example,if we hail a NPC and it has something like if($text=~/hail/i) {quest::say("you hailed me");},the client who triggered it is 'mob'.
-other:is the 'thing' which IS triggered.In the last example,'other' is the NPC.


so if we are using:
mob->CastToClient()->Save();
we are telling the client to save himself.If we want to add an action to the client,we should use CastToClient() between.
Then,if we want to set pvp on client,how we should do it?mob->SetPvP(true) ?
Nah,we need to use mob->CastToClient()->SetPvP(true);,remember it.

It is always good to add some error checks,so it won't crash.

Another example,but now using other class.Let's take a look at doanim function:
else if (!strcmp(strlwr(command),"doanim")) {
if (other) other->DoAnim(atoi(arglist[0]));
}
First,it checks that other(the npc) exists,if so,the NPC(other) will make the animation specified by the argument passed from the perl function.For example,if we use quest::doanim(2); that function will give extra info,in this case, the number 2.We need this number to make the animation specified.

So we need to get that info right?It is very simple.To get an argument,we can use arglist.That is an array,so if we give two arguments,we will take the first with arglist[0],and the second with arglist[1].

If the arguments are integers,we should use the function atoi to prevent errors,because if we give a character instead of a number it can crash.So if we wanna use an integer from the argument given,we will do this: atoi(arglist[0]).If the argument is a string we should use a function in STD library:parms.c_str().


And if we want to get the zone name,we should use zone->GetShortName();.

That easy!!.

Some examples:
else if (!strcmp(strlwr(command),"setheading"))
{
if (other) other->SetHeading(atoi(arglist[0]));
}



else if (!strcmp(strlwr(command),"permaclass")) {//Cofruben:-Makes the client the class specified
mob->CastToClient()->SetBaseClass(atoi(arglist[0]));
mob->CastToClient()->Save(2);
mob->CastToClient()->Kick();
}



else if (!strcmp(strlwr(command),"surname")) { //Cofruben:-Changes the last name.
if (mob && mob->IsClient())
{
mob->CastToClient()->ChangeLastName(arglist[0]);
mob->CastToClient()->Message(15,"Your surname has been changed/set to: %s",arglist[0]);
}
else
mob->CastToClient()->Message(15,"Error changing/setting surname");
}

x-scythe
07-29-2004, 06:53 AM
very nice
this'll help out a lot