Likes: 0
Results 1 to 4 of 4
-
07-07-09, 02:26 AM #1
[C++ Guide] Create A Custom User Command
Register to remove this adMake A Custom User CommandI. 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
Code: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); }
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.
Code:void PlayerCommands(Player * pPlayer, uint32 Type, uint32 Lang, const char * Message, const char * Misc) { pPlayer->SafeTeleport(mapid, instance, x, y, z, o); }
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.
Code: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); } }
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.
Code: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); } } }
to a spot filled with items for just anything. We gotta charge them.
Code: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); } } } }
create a bunch of commands for your own private use.
I hope you guys enjoyed what you learned
› See More: [C++ Guide] Create A Custom User Command
-
10-07-09, 09:26 PM #2
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.
EDIT: Shouldnt a bool be used?Last edited by Sdyess94; 11-07-09 at 06:34 PM.
-
12-07-09, 08:44 PM #3
Its not required to have a bool but its runs alot better, wasn't really thinking when i made this tutorial
-
14-11-09, 04:16 PM #4
Register to remove this adoops saw that there is no questions allowed nice work
Last edited by Veldor; 14-11-09 at 04:29 PM.