View Single Post
  #13  
Old 07-27-2012, 04:38 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Imperialistic View Post
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(01// a
"abcd".substring(03// abc
"abcd".substring(11// 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(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).
__________________
Reply With Quote