Quote:
Originally Posted by Imperialistic
Instead of creating a new thread I'm just going to bump this, sowwwy.
I have been trying to sharpen my scripting skills lately and I ran into a problem with this.. The script works fine and everything but I don't understand why we have to put the quotation marks around the "message" or else it wont register. I believe there is another way to read the player chat but I'm unsure of it.
|
Because it's using
tokenize, which splits a string at a delimiter (a space if you don't specify one).
PHP Code:
temp.array = "/tell cbk1994 hello there".tokenize();
// array is {"/tell", "cbk1994", "hello", "there"}
tokenize won't escape at delimiters inside quotation marks, so by placing the message inside quotes it doesn't get split at spaces and the entire message is contained in
tokens[2]. Otherwise, only the first word of the message is contained in that index of the array.
The alternative is to use
string.substring(startIndex, length) which simply takes part of a string:
PHP Code:
"abcd".substring(1) // bcd
"abcd".substring(0, 1) // a
"abcd".substring(0, 3) // abc
"abcd".substring(1, 1) // b
If I'm using a simple command with one parameter, say "/echo [message]", then we can easily use substring:
PHP Code:
if (player.chat.starts("/echo ")) {
temp.msg = player.chat.substring(6);
}
By chopping off the first 6 characters ("/echo "), we're left with only the message.
However, this doesn't work as well for commands with multiple parameters. For the command "/tell [account] [message]", the number of characters to chop off the front of the player's chat to get the message depends on how long the account is, which varies. We can use
tokenize to split at spaces, take the length of the second token (the account), and use that to determine the number of characters to chop off to get the message:
PHP Code:
if (player.chat.starts("/tell ")) {
temp.tokens = player.chat.tokenize();
temp.acc = temp.tokens[1];
temp.msg = player.chat.substring(6 + temp.acc.length() + 1); // "/tell " + account length + space after account
}
The problem with doing this for commands that take multiple parameters is that all parameters except the long one must have a fixed number of spaces (words).
In other words, using the script above, I couldn't say "/tell Fidel Castro hi there!". Even though "Fidel Castro" is an actual account, there is no way for the command to know that because it has a space (some accounts contain spaces). It will try to send the message "Castro hi there!" to account "Fidel".
Using tokenize we can just use:
Quote:
/tell "Fidel Castro" "hi there!"
|
It's possible to craft some creative solutions to this problem, but in practice there's usually a better way to implement these kind of commands (typing in another player's account is pretty annoying).