PDA

View Full Version : [C++ Guide] Create A Custom User Command



mager1794
07-07-09, 01:26 AM
Make A Custom User Command

I. Introduction
II. Moving Forward
III. How To Do This
IV. A Basic Template
V. Getting to the Real Stuff

Introduction

Welcome to my tutorial on creating a custom user command. A user command is a relativily easy script to make.
Its strength is also its weakness though, creating this form of user command does not allow you to recieve arguments.
So basically your command cannot be this "#Teleport x y z" but it can be "#Teleport" and they're sent to a designated
coords. In this tutorial were gonna make your most basic but popular command, a mall portal command.


Moving Forward

Now of course when writing this script we have to think of everything possible. Such as do we want them to be able to
teleport while in combat, probably not. Maybe we want them to have to pay for this also it really all depends on what
you want. Now in my opinion if your gonna make them pay don't limit it to being out of combat only. Now enough of this
thinking process we all want to move on to our script and i've already got the thinking finished for you. Were gonna
create a mall portal command with a combat limit, and a price, not that i'd suggest it on your server but the point of
this tutorial is to get you on your way to making your own.


How To Do This

Now anyone who has never written in C++ is expecting this to be pretty hard probably, but its fairly easy and making simple
scripts like this is a magnificent way to start learning. For this script we will be using what is called a Server Hook, which is
basically a set of occurences defined inside the core that happen so often they figured why not just let people add their own
code(paraphrasing of course). For this script specifically though we will be performing a OnChat Server hook.


A Basic Template

I'm gonna give you our base example and then descrive it in this section


void PlayerCommands(Player * pPlayer, uint32 Type, uint32 Lang, const char * Message, const char * Misc)
{

}

void SetupPlayerCommands(ScriptMgr * mgr)
{
mgr->register_hook(SERVER_HOOK_EVENT_ON_CHAT, &PlayerCommands);
}you see void PlayerCommand, that is our main function, the arguments on the left are what we will use to create our command.
then as you see directly under it is SetupPlayerCommands(ScriptMgr * mgr), thats the function that we register inside our setup
files, and under it is where the magic is done. Thats where we use the ScriptMgr to register a hook on a certain server hook and to a special function.




Getting to the Real Stuff

Now that we've gotten all that out of the way its time to get down into the actual coding of it. first off lets just create function to
teleport.

void PlayerCommands(Player * pPlayer, uint32 Type, uint32 Lang, const char * Message, const char * Misc)
{
pPlayer->SafeTeleport(mapid, instance, x, y, z, o);
}to find out the mapid, instanceid, x, y, z, and o; go ingame (as a GM) and type .gps and write down the info.
Now with that info anytime we talk we teleport and we don't want that so we need to set it to check for our
command message only.


void PlayerCommands(Player * pPlayer, uint32 Type, uint32 Lang, const char * Message, const char * Misc)
{
if(Message == "#Mall" || Message == "#mall")
{
pPlayer->SafeTeleport(mapid, instance, x, y, z, o);
}
}If you notice that its in there twice, the reason is to not hassle players to have to deal with accidently forgetting a caps,
now that we got it check for our User Command we want to make sure people don't cheat and use this to save
themselves from death.


void PlayerCommands(Player * pPlayer, uint32 Type, uint32 Lang, const char * Message, const char * Misc)
{
if(Message == "#Mall" || Message == "#mall")
{
if(!pPlayer->CombatStatus.IsInCombat())
{
pPlayer->SafeTeleport(mapid, instance, x, y, z, o);
}
}
}There now our players can't cheat to survive, but we don't want them to be able to teleport from anywhere in the world
to a spot filled with items for just anything. We gotta charge them.


void PlayerCommands(Player * pPlayer, uint32 Type, uint32 Lang, const char * Message, const char * Misc)
{
if(Message == "#Mall" || Message == "#mall")
{
if(!pPlayer->CombatStatus.IsInCombat())
{
if(pPlayer->GetUInt32Value(PLAYER_FIELD_COINAGE) => 100000)
{
pPlayer->SetUInt32Value(PLAYER_FIELD_COINAGE, pPlayer->GetUInt32Value(PLAYER_FIELD_COINAGE)-100000);
pPlayer->SafeTeleport(mapid, instance, x, y, z, o);
}
}
}
}There now our User Command is fully built and ready for use, now with the knowledge you've learned go our and
create a bunch of commands for your own private use.

I hope you guys enjoyed what you learned

Sdyess94
10-07-09, 08:26 PM
Thanks Mager, I've been having to look at scripts that were already made, which is helpful, but its a lot easier with a tut.

+rep

EDIT: Shouldnt a bool be used?

mager1794
12-07-09, 07:44 PM
Its not required to have a bool but its runs alot better, wasn't really thinking when i made this tutorial

Veldor
14-11-09, 03:16 PM
oops saw that there is no questions allowed :) nice work