I think the chatbot tutorial should be refreshed
Making Chatbots
You've probably seen somebody in a server automatically say hi when somebody joins, or somebody just says "get server ip" in the chat, and they automatically talk back with their sentence being the ip of the server. You've probably wondered what that is, and I will explain to you about these mods called Chatbots, and how to make them.
Chatbots are very simple to make, we will first make a chatbot that will make you say "Hey dude!" after you say "Hi Chatbot". The easiest to way to make this is by packaging.
PackagingPackaging is a very useful thing in Blockland. With packaging, you can re-modify / add on to existing functions without having to re-write all the previous data again. Packages are very simple and creating one starts like this:
package PACKAGENAME
{
//code stuff here
};
You now see that creating packages are similar to creating functions in Blockland. But there are a few differences. Packages do not need () to surround the name of the package (as far as I know), all you need to do is type package and then a space with the name of your package after it. Also at the end of the package, is required: a semi-colon. Semi-colons are always needed to end packages. They are not used for functions, only packages and other things that other people might explain.
Modifying a functionThe whole reason we want to create a package, is to modify the
chat function so we can make ourselves say
"Hey Dude!" after we've said "Hi Chatbot". To do this, we just define the function we want to modify in the package. What we want to modify is
clientCmdChatMessage.
That function is the function that is called after we've typed all of our stuff and press ENTER. The clientCmdChatMessage function has quite a few arguments, some of which even I am not familiar with, but all you need to be familiar with is 2 arguments. Here is the function labeled below:
function clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
{
//code here
}
What we need to memorize is
%name and
%msg. If you already know what a variable is, then it should be obvious to you. clientCmdChatMessage is called everytime someone chats. Each variable is always changed every time someone chats. %name is the name of the user who just sent a message, and %msg is the message that they typed. To implement the function into our package, refer to the code down here:
package Chatbot
{
function clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
{
//code here
}
};
ParentingPackages do not only exist for re-modifying functions. They also exist so that we can turn them on and off anytime we like. Packages are useful for torque programmers because whenever we wish to remodify a function, some might not use packages, and once they remodify that function, all the previous data is lost unless they enter it back in. If clientCmdChatMessage was modified without packages, then your chatting system would be broken and you would have to restart Blockland.
The code we have right now is in a package thankfully to me, and we are telling Blockland to remodify the function clientCmdChatMessage. But no, that is not all. We have not entered any code in there yet, so Blockland thinks we want clientCmdChatMessage to do practically nothing. So we have to type in a special little command that will enter all the previous data we've had. That command is called: Parent. And this is how we use it:
package Chatbot
{
function clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
{
parent::clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
}
};
Using that command will re-enter all the previous data that already existed in our function back in. Now we can apply any changes to this function after the Parent command.
If StatementsIf statements exist to execute code when a certain thing happens. If statements are only executed once, but now that we can remodify the clientCmdChatMessage function, any if statement in there will be checked everytime somebody chats.
So guess what we will need to put in the if statement? Come on just guess and come back when you think you've got it.
Have you got it? If not then :[. The answer is we will need our if statement to check if our player is saying "Hi Chatbot". If you've got it then high-five. Unfortunately, we cannot just enter it in like that on our code, we need to enter it in by Torque's laws of syntax.
A way to use if statements to see if the player is saying "Hi Chatbot", is by this:
package Chatbot
{
function clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
{
parent::clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
if(%msg $= "Hey Chatbot!")
{
//code here
}
}
};
Much is to be explained here. Every if statement needs an operator of some sort. the operator I used was
$=. I will present a list of operators below, and what they do:
- == if Value Variable is equal to
- != if Value Variable is NOT equal to
- $= if String Variable is equal to
- && Used to connect 2 operators together. The if statement will run if both of them return true
- || Used to connect 2 operators together. The if statement will run if 1 or the other returns true
There are probably more operators out there but i'm tired so I'll stop it here for tonight.
The reason I used $= in our code, is because %msg always returns a string, and $= is used for connecting strings.
Only use == when you are connecting values.
So basically, when Blockland looks over the code, it sees our package, then makes our package, then it sees that we want to re-define the function clientCmdChatMessage, so it does that for us, and then it sees the parent command, so we just told it to write in all the previous data that was in it before we defined it, then it sees the if statement, and understands to execute any code when a message that any player sent is equal to "Hey Chatbot!".
Don't you feel pretty accomplished for getting this far? So do I. But there is 1 slight problem. If a message that ANY player sent is equal to "Hey Chatbot!". We probably don't want that, and we only want the if statement to run if WE said that line. So all we need to do is add in another operator in our if statement and add a little command:
package Chatbot
{
function clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
{
parent::clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
if(%msg $= "Hey Chatbot!" && %name $= $Pref::Player::Netname)
{
//code here
}
}
};
As explained before, the
&& is used to connect 2 operators. So when the console looks at the if statement, it will check if the message that a player sent is equal to "Hey Chatbot", and it will check to see if the name of the player who sent that message is also equal to the name of $Pref::Player::Netname. $Pref::Player::Netname is the same as the username you set on Blockland when you first registered. Everytime you change your username, $Pref::Player::Netname is changed with it also.
Making our player talkYes, not much more to do now! All we need to do now is call a function to make a player talk! As you've (hopefully), read in Pacnet's tutorial, we need to send a message to the server to make us talk. Here is the command we need to send:
package Chatbot
{
function clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
{
parent::clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
if(%msg $= "Hey Chatbot!" && %name $= $Pref::Player::Netname)
{
commandToServer('messageSent',"Hey dude!");
}
}
};
And yes, that is basically it. messageSent is a command we have to put in commandToServer to tell the server we want our player to talk. So basically, all you have to do is execute this little code through the means of a small script, or a plain add-on, and then, most importantly, you must type in the console:
activatePackage(packagename);
deactivatePackage(packagename);
ActivatePackage turns your mod on, like i've said before: Packages can be enabled or disabled. DeactivatePackage turns your mod off, so the command wont work anymore.
Up next: Advanced Chatbot Tutorial