runtest
Joined: 05 Aug 2006 Posts: 98
|
Posted: Thu Jun 28, 2007 12:01 pm Post subject: How To Create A Limited Chat Command |
|
|
For you vets, this is just a detailed version of bcast with a restriction timer. I thought the timer would be most helpful to new people.
| Code: |
-- Guide To Creating A Chat Command --
I would like to start this tutorial with these words. I am not an expert, I am not perfect and most of all I love a little good constructive criticism. So drop me a line at runtest.druid@hotmail.com. I promise, I will not bite… Much. Now, this is a really simple command, and CWO, thanks for the idea. We are creating a simple bartering program, when used will display text to every person online. Whenever you are scripting something it is a good idea to plan it out. I am going to step you through making this command one piece at a time.
1. Placing your command in the right folder. (Important)
2. Making a simple Program.
3. Making the program broadcast.
4. Making the program show your name.
5. Making the program show your name and text.
6. Adding style.
7. Adding a timer so the person can only broadcast every thirty seconds.
This is a really simple command that vets will say, “Why did you write a tutorial on
this?” I will reply with something like, “Because new people do not find this so easy.” So there. Anyhow this tutorial will start now.
Step One:
This part is very important. Pol is setup so that commands are limited to certain command levels (cmdlevels) by folder. If you navigate to scripts\textcmd you will see six folders. There are six command levels one higher than the last.
· Test
· Admin
· Gm
· Seer
· Couns
· Player
If you put a command script in the player folder, players up to test will have access to it. If you put the command in the Gm folder Gm and up will have access to it and so on and so forth. The script is activated when the ‘.’ Command is used in game. IE: You want to use the ‘consider’ script used in player thus a player would type ‘.consider’. Ok enough of that. Just know we are going to be working in the Player folder. So please start your server and login. Yes, we are going to be doing this while you are logged in and the server is running.
Now to set things up to make life easier, if you have never compiled before or just want a simpler way of doing things.
1. Navigate back to your scripts folder.
2. Right click ‘ecompile.exe’ then click make shortcut. Throw that shortcut on your desktop.
We have to compile our .src scripts everytime we change them because the server
does not read the scripts but the .ecl that is outputted. We do not have to restart the server though, when you recompile a script just type ‘.unloadall’ in game.
So when I put in big words after a script change COMPILE run the compiler and when I put UNLOAD that means run a ‘.unload’ in game.
Back in your Player folder create a new file called ‘barter.src’.
Tip: Whatever the file is called will be your command in game. IE: If we named the file ‘cow’ it would be .cow.
Open the .src with your favorite text editor and prepare for the invasion of step two.
Step Two:
Insert the code between the lines into ‘barter.src’.
_____________________________________________________________
use uo;
Program my_barter_program()
Endprogram
_____________________________________________________________
COMPILE & UNLOAD
If you type ‘.barter’ in game you will notice nothing happens. That is because we did not tell it to do anything. Let me explain the code above.
Use uo; Tell the script to use uo functions.
Program This is what we use to define our program.
Endprogram Tells the program to terminate after it is finished.
What you see is what you get. So for more fun go to step three.
Step Three:
Now we want the program to broadcast, but broadcast what? Lets start with a simple string. I am most definitely a chicken. A string is simple a chain of words or numbers that hold no mathematical function, IE: I am a chicken. Will not be calculated or taken away from. If you are still confused I suggest reading Racalac’s E-Script guide in the DOCs folder.
This is mind numbingly stupid. Insert this line:
Broadcast( “I am most definitely a chicken.” );
Under this line:
Program my_barter_program()
COMPILE & UNLOAD
Wow that was simple, Broadcast is a very simple command that does what it sounds like, it broadcast. I must admit though, having it broadcasting chicken play is not the best things for all shards. So we need to have it do more.
Step Four:
Make your script look like this.
_____________________________________________________________
use uo;
Program my_barter_program( me )
Broadcast( me.name );
Endprogram
_____________________________________________________________
COMPILE & UNLOAD
More than one question may be going through your mind. Like, are you a wizard this is like magic? No, no, just a simple, super human. When we insert the word me in there we are referencing the object that used the script, in this case the player. Me could actually be almost any word. Me could be cow, horse, Douglas_Adams, it does not matter. We are just storing what is accessing the script.
The second change is me.name. Remember me is just what is accessing the script. Name is the name of whatever is accessing the script. For example, lets say we created an item with the same code like a ring. When the ring accessed it whatever the rings name was would broadcast like, ‘a wedding ring’. For more on this access ‘index.html’ in your DOCs folder and lookup ‘Pol Object Class Reference”.
Step Five:
Lets delve a step further; we want to record the next variable accessing our script. That would be the text the player is going to type. Let me explain:
When we access the script the first thing stored is who accessed it. Thus me is who or what accessed it. We use the .name class reference to access the string name of who or what it is. The second things stored when the command is typed is the text. We are going to store that variable as cat for fun.
Make your code look like this.
_____________________________________________________________
use uo;
Program my_barter_program( me, cat )
Broadcast( cat );
Endprogram
_____________________________________________________________
C & U
Now in game type ‘.barter Hello World’. Viola, it will now print out text, but now someone can say they are stay or imitate other people. This is not good. So we are going to use the + operator to add the strings together.
_____________________________________________________________
use uo;
Program my_barter_program( me, cat )
Broadcast( me.name + cat );
Endprogram
_____________________________________________________________
C & U
We added them like numbers, that’s all. Though, it is still a little lacking. Lets add a little style.
Step Six:
Tip: If you look in ‘scripts\include’ you will see a file called ‘client.inc’ which contains a REALLY nice list of animation, sounds and text fonts numbers, which you can use. I am going to be putting what we will use here.
CLIENT.INC
// Font Colours.
const FONTCOLOR_RED := 38;
const FONTCOLOR_GREEN :=68;
const FONTCOLOR_BLUE := 90;
const FONTCOLOR_MAUVE := 30;
const FONTCOLOR_LAVENDER := 20;
const FONTCOLOR_BLACK := 1;
const FONTCOLOR_BROWN := 46;
const FONTCOLOR_YELLOW := 53;
// Fonts
const FONT_MAX := 0x09; // Highest font.
const FONT_MIN := 0x00; // Lowest font.
const FONT_BOLD := 0x00; // Bold text.
const FONT_SHADOW := 0x01; // Text with shadow.
const FONT_BOLD_SHAD := 0x02; // Bold text with shadow.
const FONT_NORMAL := 0x03; // Normal (default).
const FONT_GOTHIC := 0x04; // Gothic.
const FONT_ITALIC := 0x05; // Italic
const FONT_SMALL_DARK := 0x06; // Small dark letters.
const FONT_COLOR := 0x07; // Colorful font.
const FONT_RUNE := 0x08; // Rune font (only capitals).
const FONT_SMALL_LIGHT := 0x09; // Small light letters.
They spell colors like a Brit.
Lets put these to good use.
Make your code look like the code between the lines.
_____________________________________________________________
use uo;
Program my_barter_program( me, cat )
Broadcast( "Barters - " + me.name + ": " + cat );
Endprogram
_____________________________________________________________
C & U
This is a big improvement, all we did here is add more strings, and yes the blank spaces inside the quotations are strings also. In programming zeros are also counted. This is a string “ “. Strings are usually in quotations unless they are already variables like cat and me. Now, to add a little flare using font and color variables.
_____________________________________________________________
use uo;
Program my_barter_program( me, cat )
Broadcast( "Barters - " + me.name + ": " + cat, font := 0x02, color := 53 );
Endprogram
_____________________________________________________________
C & U
Now that is style, it looks and acts nice. But, you are inevitably banging your head off a desk right now saying, Runtest, you super human! How did you do that? It is quite simple my good man.
We used the := operator no not == that’s different. Using this we set the left hand variable - font and color - to the number on the right. That is how we set a variable. The == if for declaring a statement.
Last Step:
That was a lot of work in retrospect if you did not know how to do it. Though are chat command looks pretty sweet in use, it can quickly become annoying with - drum rolls - spammers. So lets slow them down a bit with a time restriction. I am going to paste the code below and explain it very thoroughly. Mainly because when I started, this was my biggest stepping stone, timers’.
____________________________________________________________
use uo;
Program my_barter_program( me, cat )
var Time := ReadGameClock();
var WaitTime := GetObjProperty( me, "#WaitTime" );
if(Time >= WaitTime )
Broadcast( "Barters - " + me.name + ": " + cat, font := 0x02, color := 53 );
SetObjProperty( me, "#WaitTime", ReadGameClock() + 30 );
Else
SendSysMessage( me, "You must wait 30 seconds between each barter.",3,65 );
Endif
Endprogram
_____________________________________________________________
C & U
Ok, heres how this breaks down with the new code in place.
Steps of Code!
1. Use Uo; //Duh
2. Setup our program.
3. Setup what time it is now.
4. Read our Wait Time defined later.
5. Check to see if the time now is bigger than or equal to our Wait Time.
6. If it is then Broadcast and…
7. Set our player with a new wait time which is the time now + 30 seconds.
8. If Time is Smaller than Wait Time then send a message telling the player he/she has to wait.
9. End the statement.
10. End the program.
Here is a little description of each bit of code used in our entire tutorial. All these
and more can be found in your DOCs folder, open up index.html and see for yourself. READ THE DOCS PEOPLE! Oh, one more thing, I learned to do this a long time ago using the trashinsert.src and trashremove.src. :p
Use UO;
This use command tells the script to look in what .em file for the operators we are going to use like Broadcast().
Program()
Since we are using a text command in this instance, uo will pass a check for the character and the text that has been entered. This is around the same principle for items or anything else that will run the script.
Var
Var is used for setting up variables. Picture a variable like an empty jar. We can put whatever we want into it like, letters, number and script paths. In this cast we are storing numbers.
ReadGameClock()
This operator returns the game time; this time has no actual meaning and is used for the purpose of game timing. It has no relation to real life time or time at all for that matter. Time, is just an illusion. Lunch time doubly so.
GetObjectProperty()
This operator tells the script to get a CProp stored on a UOObject. Yes, even your character is a UOObject in this case referred to as me. We are getting the “#WaitTime” that we store later while at the same time in this particular script, declaring the variable WaitTime.
If() Else Endif < > - + * /
These are called statements. We are simply checking to see if a statement is true and if so continue if not a.k.a Else then do something ‘Else’ the endif tells the script that the statement has now ended. In this case we are saying if the Time at this very moment is greater than the time we recoreded earlier after the last message, then go ahead and message again, Else Send a message saying they have to wait 30 seconds.
Broadcast()
Now come on… This is simple there are commas after the string that we want to Broadcast to denote font and font color.
SendSysMessage()
Send a system message to whoever is defined, in this case the player accessing the script. Later I will show you how to make a very simple message system using this operator.
Endprogram
This ends our program for now. Good-bye program.
Well this concludes another one of my long drug out newbie programming tutorials, yeah vets, you can eat this, no, really, paper is fiber, and it helps a lot.
By Runtest
|
As I have said before, I am not an expert though I would love feedback from anyone who might think this helps or, on the other hand, does not. |
|