• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Using Chat Commands

Level 21
Joined
Jan 5, 2005
Messages
3,515
Using Chat Commands

This tutorial will contain three tutorials when finished, all relating to doing stuff using chat commands. You may have seen on maps people being able to type –help to bring up a help menu or –kick (player colour) to kick a player. This tutorial will tell you how to do all those things you have seen and possibly more. I decided to make this tutorial after discovering how to do what is described in Part 1, which I think is very important and useful to know, so I thought I would share the knowledge. I’ve done my best to explain it and I hope it’s understandable, any things I should add, change or reword, please tell me. Important things and Key Terms are underlined; there is also a list of Key Terms relevant to that tutorial at the end of each one.

Contents:

Part 1 - Using Substrings With an Unknown Value
Part 2 - Using Substrings With a Known Value (Coming Soon)
Part 3 – Chat Commands To Do Common Functions (Coming Soon)

Part 1 - Using Substrings With an Unknown Value

This tutorial is concerned with using unknown substrings within a chat string, and using these substrings to set variables, run processes or do just about anything you can in GUI.

First Things First - What is a substring?

A substring is a word, string of words or a value within a string which has been isolated because it is relevant to a specific process. For example you may have seen maps where the players can enter “–Gold X” into chat to receive X amount of gold (I wish :p ) or something similar. In that example the activator is the command “–Gold” which means the system looks for a player typing –gold as an event. The substring of interest is the value entered for X, and this is what needs to be isolated and manipulated to give the player the gold they want.

Next Up – Putting it to use:

This kind of substring manipulation is useful specifically when dealing with numbers or a value that cannot be from a known set of value. What this means is that you wouldn’t use this to kick players because you already know that you are only going to have 12 possible players and you know their colours and numbers. You would however, use this to deal with numbers; this is because a user can enter any number from 0 to the memory limit of wc3, to have that many logical comparisons would be just silly! So we have to isolate the number entered. This can also be used for saved games if you have one of those crazy systems where you enter a code and this loads up a character based on that code. I don’t know how they do it, but I’m guessing it starts like this.

The Event – Learn By Example:

I’m going to explain how substrings like this work by taking you through an example, the exact same one described above, to give a player a certain amount of gold when they enter –Gold followed by a value.

To start of, all you do is make a normal event using the activator you want with a space after the word used for your activator. So for gold the activator will be “-Gold ”. Notice how there is a space after Gold? This space is very important and must be included to work! The entered chat message should be set as a string. So you should have something like this, just with your preferred activator:

  • Events
    • Player - Player 1 (Red) types a chat message containing -Gold as A substring
You can of course add as many players as required, or tweak the event to your liking. Once you have this you are ready to get into the hard part.

Things Get Tricky - Isolating The Substring:

Right, it was easy so far, just a little reading and some easy event; well this is where things get confusing, so I’m going to do my best to simplify this. The command used to isolate the integer from the string is this:

(Integer((Substring((Entered chat string), 7, 11))))

This command is repeated for pretty much everything so you better get used to it :p This command will find the (sub)string written after –Gold in the message entered by the user and convert it into a number. The first thing that to note here is that this is function results in getting a number, which it extracts from a string. The function “Convert String to Integer” is used here as the first part which can be seen by the first bracket containing the word “Integer”, I stress once again this function gets an integer from a string! The next part depicted by the “Substring” bracket tells us we are interested in isolating a substring, i.e. we are interested in isolating part of a string. The next part “Entered chat string” tells us we are looking for a substring within the entered chat string. Still with me? I hope so. The next two numbers tell us where to look in the string by showing which place the string will be read from and which place it will finish reading at. For example:

“-Gold ” contains 6 characters, including all the letters, the dash and the space (always count the space!). In the example command I have just shown the string is read from place 7 to place 11. This means anything that is entered after the “-Gold ” is read without the “-Gold ” bit. So if a person entered “-Gold 12345” the system will have isolated 12345 as an integer, because that number was from place 7 to 11 in the chat string.

As you can see in the example command I have used the places ranging from 7 to 11; it would be possible to use places ranging from 7 to anything depending on how big the number you want. If someone has got the place range of 7-11 and a person types in less than 5 digits (5 being the difference between 7 and 11) the system will still only register the digits entered, so it would read 123 as 123, not some crazy 000123 number or something. Basically you can set the second number to whatever you want so long as the first number is relevant to the place after the activator and, importantly, the space after it. It is possible to make the length of the string effectively limitless by modifying the command to look like this:

(Integer(Substring((Entered chat string), 7, (Length of (Entered chat string)))))

By changing the number (was 11) to the string length of the typed in chat message you can make the substring limitless. This is useful when dealing with substrings which could be potentially any length.

If you have understood this you should now know how to get an integer from the substring of interest that was entered with the chat string. Using the same method it is also possible to get words from a substring, in which case you would not need the initial “(Integer” part as you will be extracting a string from a string, not an integer from a string. The main thing to remember is that the two numbers read the chat message from the place where the substring of interest starts to where it finishes. That’s the bulk of this over and the hardest part (to understand and explain) behind us, now we can do something with it!

Action – Putting it to use:

Hopefully now you understand how this command works, but if not this example of how you would use the command will explain it better than I can:

  • Oh Noes
    • Events
      • Player - Player 1 (Red) types a chat message containing -Gold as A substring
    • Conditions
    • Actions
      • Player - Add (Integer((Substring((Entered chat string), 7, 11)))) to (Player((Player number of (Triggering player)))) Current gold
As you can see, when the player types in “-Gold ” and an amount it adds that gold amount to the player who typed in the message. As you can see in this example the gold that can be added is limited to 5 figures, but as described above it can be changed to whatever you want. This type of system can be used in many different ways; a good example is Ralle’s banking system where I’m guessing he did something like this.

Condition – Making sure the Activator is right!

One thing that’s flawed in this (so far) is that is uses the “as A substring” event which means if a person types –Gold anywhere in a chat message it will try to give the person the value written from places 7-11, even if that were a word instead of a number. This isn’t such a problem here, but in other situations it can be. To counter this a condition is used that checks that the activator is written at the start of the chat string and not in the middle somewhere:

  • Conditions
    • (Substring((Entered chat string), 1, 6)) Equal to –Gold
Using a similar command to the one described we can do a string comparison and compare the word in places 1-6 (or however long you activator is, including the space) with the activator. By doing this it ensures that it will only run the action when the activator is at the start of the string.

Conclusion

The final, completed trigger looks like this:

  • Oh Noes
    • Events
      • Player - Player 1 (Red) types a chat message containing -Gold as A substring
    • Conditions
      • (Substring((Entered chat string), 1, 6)) Equal to -Gold
    • Actions
      • Player - Add (Integer((Substring((Entered chat string), 7, 11)))) to (Player((Player number of (Triggering player)))) Current gold
You will, of course, be able to tweak this as necessary.

Using substrings like this you can do many things, hopefully now you have the basics you can do anything you want with substrings, Good Luck!

Key terms:
(Note: most of these I made up, but it helps me explain it so they are useful to know)

Activator – the word or command at the start of the chat message that the system looks for as an event for the trigger.
Substring of Interest – the value or word within the entered chat message, usually straight after the activator, that needs to be isolated and manipulated.
Place Range – the range between which the substring of interest is read.
 
Last edited by a moderator:
Level 21
Joined
Jan 5, 2005
Messages
3,515
So Dan, when typing in "-gold fuck" it will crash the game cause "fuck" is not a number.

gold fuck, haha funny how thats the word you chose...nah only joking :p. well i have looked into this because i though exactly the same thing but when i tested it typing in a word didnt make it crash, it just didnt affect the system at all, it doesnt add money or anything, i think it ignors it. i dunno if this has been true in the past and been changed in a patch but when i tested it, and testing it again right now, i found no problem. because i didnt find a problem and i dont know how to fix the problem if there was one, i decided not to include it, but if it may cause an unforseeable problem it would be something i could include.


Another thing is tags. Man please use more than just the
  • tag :P. Use some bolds here and there and use the [SIZE] tag[/QUOTE]
  • [quote="ragingspeedhorn, post: 272456"]As Ralle suggested the tutorial needs a few adjustments here and there, especially the looks of it, itøs very plain and boring and made me want to skip it all.[/QUOTE]
  • i will try my best with this, im not great at text formatting and i will just add some heading or something to make it more reader friendly.
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
Well...

A) any non-numerical characters found in text parsed by the warcraft S2I function will result in the whole string being regarded as being equal to 0

B) you forgot to detatch your signature from the main post ><

C) It's a good idea to check if SubString( 0 (ahem 1 in GUI), n ) is equal to your message ("-gold " in this case), to prevent "Hello -gold 1" and other useless commands. It doesn't really make a difference right here, but this tutorial is about chat commands in general, and people who don't get in the habit of verifying a valid command will find odd bugs occuring, like "I like -life" killing the unit (since it would set its life to 0)

D) Try to abuse trigger tags more; they're prettier than text (for actual GUI triggers), and alot of what you're writing is triggers anyhow.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,255
A - I think it removes some non numbers from the scoop and thus in some cases it will not return 0 if numbers are mixed in, so "-gold fuck" definatly will not crash the map.
He gets it to only trigger with -gold thus comment C is invalid.

Well so far seems very usefull, although you may with to include a jass method of doing each of the topics but that is not vital.
This should definatly be approved once it is complete if the rest is of such quality.

Like mentioned, you may want to liven it up a bit and make it easier to read by removing the block of text feel, since "noobs" hate the block of text feel on average and so people who most need it might be discouraged to read it.
 
Top