[Resource]-Torquescript Learners Guide-[WIP]

Author Topic: [Resource]-Torquescript Learners Guide-[WIP]  (Read 8387 times)

Advanced Chatbot Tutorial


So making your chatbot say something when you say a specific thing wasn't enough for you? Well, don't worry my friend! I will hope that you have read my previous Chatbot tutorial and hope you are ready for this new challenge.

I will explain before-hand what we will be covering in our tutorial. A chatbot that just says something is highly boring, to you, and to me. We need our chatbot to actually be useful for something. And for our chatbots to be useful, we will need to use input.

Input

Input is a very special thing. Input is like taking things that the user (you) enters. If you've previously read ¥ola's tutorial, then you would understand that input can be used for many things. Input is used in a function to make functions useful for working. Functions with input take the input you entered, and work some things with the input you've entered. After it's done, it usually returns a different value or string, or something different.

Input can also be used in chatbots. Which is why, today we will be making a chatbot that takes 2 numbers we've entered, and add them both, and repeat the answer back in our chat! Sounds pretty cool right? First we will need to write down a default code template for our chatbot:

Code: [Select]
package Chatbot
{
    function clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
    {
          parent::clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
    }
};

Then we will add an if statement. But in this if statement, we can't just use "%msg $=". Since we are going to make our chatbot receive input from the user, we are going to have to be a little more advanced here.

getWord() Function

getWord() is a special function, as this pre-defined function has 2 arguments: the source, and the word number. The source is like the word we want the function to examine. Lets say we want our function to examine: "Hello World". For that, we would define the following string:

Code: [Select]
%word = "Hello World!";
Then we add in a getWord function

Code: [Select]
%word = "Hello World!";
getWord(%word,0);

Hm, weird. There is a second argument in the getWord function. That is the word number. The word number is typically the number of the word we want to return. if we said "getWord(%word,0);", it would return the "Hello" part of %word. That is because the word number always starts at 0, being the first word in the string/value. Saying "getWord(%word,1);", would return "World!". Why did it put the "!" you ask? Well that's because words in the getWord() function are generally scanned when they are spaced out by spaces.

Creating our Advanced Chatbot

Now that we have an OK idea on what getWord() is, we can implement it into our chatbot. We want our Chatbot to add 2 numbers from input after we've said "add (NUMBER1) and (NUMBER2)". To do that, we must first use getWord() to check if the first word we've said in our message is, "add":

Code: [Select]
package Chatbot
{
    function clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
    {
          parent::clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
          if(getWord(%msg,0) $= "add")
          {
               //code here
          }
    }
};

The current cake we have now is saying, if the first word in our message is "add", execute the code below. We also need to see if the THIRD word in our sentence is "and". We don't really have a way to check for input now. We will check for input later, but now, lets focus on the exact words. Add this:

Code: [Select]
package Chatbot
{
    function clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
    {
          parent::clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
          if(getWord(%msg,0) $= "add" && getWord(%msg,2) $= "and")
          {
               //code here
          }
    }
};

If we used "getWord(%msg,1)" in our sentence, it would check for the 2nd word. We are saving the second word for our input, and the fourth word for our input also.

Now that we have all the data in our if statement, we can move on to the easy part. We are going to set 3 variables. The first two variables will be our input variables, because with variables, we can use getWord() in them to receive input from our message. Add this:

Code: [Select]
package Chatbot
{
    function clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
    {
          parent::clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
          if(getWord(%msg,0) $= "add" && getWord(%msg,2) $= "and")
          {
               %number1 = getWord(%msg,1);
               %number2 = getWord(%msg,3);
          }
    }
};

Yes, we are getting closer to the end. %number1 is now defined as the 2nd word in our message, and %number is defined as the 4th. Also, remember getWord() always starts at 0, 0 being the first word in the sentence. It's something with the engine, don't ask me :/.

Now we will create a variable that is the total of adding both of those variables together. Very simple:

Code: [Select]
package Chatbot
{
    function clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
    {
          parent::clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
          if(getWord(%msg,0) $= "add" && getWord(%msg,2) $= "and")
          {
               %number1 = getWord(%msg,1);
               %number2 = getWord(%msg,3);
               %total = %number1 + %number2;
          }
    }
};

%total is now the added total of %number1 and %number2. Now that we have a variable, everything else can be so simple. All we have to do is call a command to the server to make us say that variable. Also, a thing to remember. If you want to display variables in a string, then you have 2 ways: If you are trying to say just a variable and only a variable: use this:

Code: [Select]
commandToServer('messageSent',%total);
Otherwise, if you wish to display some words in your text, you can switch between displaying variable mode and string mode by using "@ to enter variable mode, and @" to exit back into word mode. An example below is this:

Code: [Select]
commandToServer('messageSent',"Your answer is "@%total@"");
If the variable %total is defined, It will make your character say:

"Your answer is (totalhere)"

So, you can just add any of those at the bottom of where we defined the %total, and it will work successfully. I personally am going to go with the other version, where it displays string. I just like it because it looks more advanced. This is our final code:

Code: [Select]
package Chatbot
{
    function clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
    {
          parent::clientCmdChatMessage(%a, %b, %c, %fmsg, %cp, %name, %cs, %msg)
          if(getWord(%msg,0) $= "add" && getWord(%msg,2) $= "and")
          {
               %number1 = getWord(%msg,1);
               %number2 = getWord(%msg,3);
               %total = %number1 + %number2;
               commandToServer('messageSent',"Your answer is "@%total@"");
          }
    }
};

Well, that's the end of our chatbot. That was really simple, right? Yes, hopefully. Now that you now how to receive input from a chatbot, you can make a chatbot that does games, such as "Guess The Number" games, or to make a chatbot subtract, MULTIPLY, or even DIVIDE your inputs! Yes, with chatbots, you can do so many things..

Up next, Creating a Chatbot Game

Purple, your work is greatly appreciated.

And Ip, me and Slayer already had a session about creating the echo function, including echoing into chat.
Slayer try making a function that will echo Red, Green or Blue randomly  you call it.

getRandom(lower,higher);
That is the method to get a random integer between the lower and higher number.




What
he was VERY beginner, I don't think he's learned loops yet.

Slayer try making a function that will echo Red, Green or Blue randomly  you call it.

getRandom(lower,higher);
That is the method to get a random integer between the lower and higher number.
Well it seems I already am having trouble. Getting commands and functions confused and where everything goes.
I know I need
  • A function
  • Arguments
  • getRandom
  • %red = 0, %blue = 1 ???
  • When I type in function its echo's the random word

Well it seems I already am having trouble. Getting commands and functions confused and where everything goes.
I know I need
  • A function
  • Arguments
  • getRandom
  • %red = 0, %blue = 1 ???
  • When I type in function its echo's the random word

Here:

Code: [Select]
function randomColor()
{
 %random = getRandom(1,3);
 if(%random = 1)
  echo("Red");
 elseIf(%random = 2)
  echo("Blue");
 elseif(%random = 3)
  echo("Green");
}

Here:

Code: [Select]
function randomColor()
{
 %color[1] = "Red";
 %color[2] = "Green";
 %color[3] = "Blue";
 echo(%color[getrandom(1,3)]);
}
Fixed

Fixed
Being conceited isn't going to get you far in life.

He hasn't learned arrays yet and using them will only confuse him further.
I was giving him the code so he can study it and understand what each part does.

Here:

Code: [Select]
function randomColor()
{
 %random = getRandom(1,3);
 if(%random == 1)
  echo("Red");
 elseIf(%random == 2)
  echo("Blue");
 elseif(%random == 3)
  echo("Green");
}

Fixed it for you, you forgot ==

Fixed it for you, you forgot ==
Appreciate it, was on my iPhone, and I knew I would end up screwing something up

Being conceited isn't going to get you far in life.

He hasn't learned arrays yet and using them will only confuse him further.
I was giving him the code so he can study it and understand what each part does.
I actually just watched a video about arrays so I sorta understand them.

Good. Now you understand arrays, something Blockland doesn't have. What [] does in Blockland is variable insertion. %var[1] is the same as %var1. It just inserts whatever is inside [] into the variable name. This is useful because, like in the above example, sometimes you'll have a variable. %var%num won't work, but %var[%num] will. It'll insert the value of %num into the variable name, so if it's 4 it'll resolve to %var4.

Good. Now you understand arrays, something Blockland doesn't have. What [] does in Blockland is variable insertion. %var[1] is the same as %var1. It just inserts whatever is inside [] into the variable name. This is useful because, like in the above example, sometimes you'll have a variable. %var%num won't work, but %var[%num] will. It'll insert the value of %num into the variable name, so if it's 4 it'll resolve to %var4.

I and most of the community refer to them as arrays for lack of a better term.
Apologies for the misunderstanding though.