FANDOM


This tutorial was created by rrvs331 on the original RealmCrafter forums.

So, after learning the scripting language myself, and putting out a script generator, I have decided that I will contribute my two cents, and create a tutorial on how to script. Now this tutorial will start from the very basics, as this is meant for complete beginners with no knowledge of bvm yet. Obviously I can't compile a single tutorial of everything to do with scripting at once, therefore I will be breaking the tutorial into numerous parts, thus the reserved spaces. This won't happen overnight, so please be patient as there might be gaps between posts. 10/9/09-Part One is posted

10/30/09-Part Two is posted

11/22/09-Part Three is posted

Part 1 - The Basics of Scripting Edit

So, you want to learn what scripting is about? Well, you have to start from the ground up, hence this very basic tutorial. Open up the RC Script editor. Click on file>new. Lets edit the code a little bit. Type in what you see here, and move on. 

Using "RC_Core.rcm"
; New Project (3)
; Date/Time: 9:03:33 PM on 10/9/2009
; By Rushil on VALUED-CB7D4C82

Function Main()
     Player = Actor()
     Target = ContextActor()
   
      D = OpenDialog(Player, Target, "Example Script")
              DialogOutput(Player, D, "Welcome to Scripting! ")
              DoEvents(5000)
             CloseDialog(Player,D)
End Function 

What does this mean? First: Using "RC_Core.rcm" This is always going to be in your script, it tells the script editor that you are using a file that has a bunch of commands in it, such as DialogOutput.

Next: ; New Project (3) Why is there a semicolon in front of this and the next two lines? A semicolon indicates that the line is a comment. A comment is for your eyes only, your game will just ignore it. Thus, use comments to explain your code, so you know what your doing.

Third:Function Main() This line of code will always be there, just ignore it for now, I will explain what it does, and can do in a later tutorial.

Fourth:Player = Actor() This is another standard line in a script, almost all scripts have this. What does it do? Well, it basically says that we are going to name the user of the script(the player of your game) Player. Thus, we could name Player anything we wanted, eg: Person = Actor(), jfksldf = Actor(), whatever floats your boat is fine.

Next Target = ContextActor() What is this? The target is the person your player is interacting with. Not all scripts require this, however. This line is used in pretty much all scripts though, such as quests, dialog shop, etc. This is in essence the same concept as the Player = Actor(), the ContextActor could be named anything such as, Harry= ContextActor().

Sixth D = OpenDialog(Player, Target, "Example Script") This is where all the above lines come together and start to make sense(hopefully) Lets dissect this line carefully. First you have "D =" What does this mean? D = is basically naming the dialog box you are opening D. Thus you could name it anyhting you want, but D is fine. "OpenDialog" This is a command that you will have to memorize if you want to get anywhere in RC Scripting. OpenDialog opens a dialog box and is used a lot. "(Player, Target, "Example Script")" Player is used to describe the Actor()- which is the person playing your game(see above for description) Target is used to describe the ContextActor()-which is the NPC who is talking to the player)(see above for description) "Example Script" is what the title of the dialog box will be-the text will be visible to the player. So basically what this is saying is that to open a dialog box named D between the player and an NPC and that the title of the dialog box is Example Script.

Next: DialogOutput(Player, D, "Welcome to Scripting! ") DialogOutput is another command which you will need to know. What this command does is to output(display on screen) the text to the Player. What does the (Player, D, "Welcome to Scripting! ") do? Well, the Player is...... The Actor()! Bet you never guessed that did you? D is the name of your dialog box, as you can see in the above explanation. "Welcome to scripting" is the text that will be outputed to the player. Whenver you use DialogOutput you need a DialogOutput(Player-or whatever you named your Actor(), followed by the dialog boxes name, "text")

That is the basic formula for a DialogOutput.

Next: CloseDialog(Player,D) CloseDialog should be self-explanatory by now. It is a command to close a dialog box. Whenver you open a dialog box, YOU MUST CLOSE IT. To close a dialogbox you must say CloseDialog(Player-or whatever you named your Actor(), and then the name of the dialog box you are closing, in this case D)

Next: DoEvents(5000) What does this do? DoEvents is a command that basically pauses the script. It is there to pause the script so that the player can see the dialog. Otherwise the player wouldn't even see anything, because the dialog box would just open the immediately close. If you want to see what I mean, take out this line and run your code. You can either use this or have an iput option to pause in a dialog box. DialogInput will be covered in the next tutorial. The time is in milliseconds(so 1000 milliseconds is one second) So 5000 is 5 seconds.

Finally: End Function The last line of code is another line that I will explain in a later tutorial, all you need to know is that it needs to be there, and you can't put any code after it, or it will not run.

What should I know by now? If you really want to be a successful scripter, then I would reccomend memorizing how to do this, and then practicing it. The only way you will learn how to script is to practice, practice, and practice some more. Going in to the next tutorial, I expect you to know how to make a simple dialog script off the top of your head. Now, I know you won't get it right away, nobody does, but at least try. Even if you have to look back try and see if you can recreate this script, and if your not successful try again. When you finally get it, it will have all been worth it.


What do I do know? In this section, I will assign a "homework" assignment which will be based on the tutorial, which i expect you to code yourself. This tutorials homework assignment? Open a dialog box with the title, "Hello Game". Next, output to the player "Welcome to my game". Also output, "this is a homework assignment". If you can successfully do this, consider yourself a master of this tutorial, and be ready to move on. If not, like I said, keep trying, and you will get it!

How do I use this script?

Set it as the right-click script of an NPC

Part 2 - Conditional statements and accepting input: Edit

Welcome to part 2 of the complete scripting guide. In this part you will expand on the simple dialog script you created in part one, so that you can accept input! First, open up the simple dialog script we made in part one. Edit it so that it looks like this. Don't copy and paste the code, make sure you type it all out, no matter how tedious it is. 

Using "RC_Core.rcm"
Function Main()

   Player = Actor()
   Target = ContextActor()
                D = OpenDialog(Player, Target, "Dialog box")
                DialogOutput(Player, D, "Welcome to this dialog box")                 
                Result = DialogInput(Player, D, "Choice 1|Choice 2|Choice 3", "|")
                If Result = 1 Then
                    DialogOutput(Player, D, "You clicked Choice 1!")
                    DialogInput(Player, D, "Goodbye", "|")
                    DoEvents(2500)
                    CloseDialog(Player, D)       
               ElseIf Result = 2 Then
                    DialogOutput(Player, D, "You clicked Choice 2!")
                    DialogInput(Player, D, "Goodbye", "|")
                    DoEvents(2500)
                    CloseDialog(Player, D)       
                Else
                    DialogOutput(Player, D, "You clicked Choice 3!")
                    DialogInput(Player, D, "GoodBye", "|")
                    DoEvents(2500)
                    CloseDialog(Player, D)
                EndIf
           End Function

 Now we will start to learn what all this code does! Mini Lesson 1: Variables What are variables? Variables are what you use to store values like numbers, words, and sentences. There are many types of variables, but the one we will be focusing on today is called an integer. An integer stores a numerical value like 1 or 130. When we want to have a variable we first have to declare it. To declare a variable first you must give it a name, such as x or result. You must then make it equal to something. Once you have done that, the variable is able to be used.

Now lets look at the first new line of code.

Result = DialogInput(Player, D, "Choice 1|Choice 2", "|")

This is the first new line of code. What are we doing here? Well, first we are declaring a variable called result. Result is an integer that can either have the value 1 or 2. If the player chooses Choice 1, then Result will have the value of 1. If they choose Choice 2 then Result will equal 2. If they choose choice 3 then Result will equal 3. 

If Result = 1 Then
        DialogOutput(Player, D, "You clicked Choice 1!")  ;You should already know what these 4 lines do
        DialogOutout(Player, D, "Goodbye", 255,2)   
        DoEvents(2500)
       CloseDialog(Player, D)

 In the line above we are declaring an if statement. An if statement says that if 

(this) happens Then do this. So unless the condition(in this case Result=1) is met, the code won’t run. If Result does indeed equal one, then the code will run. So, for example, if we wanted to have different dialogs based on what choice the player made we could use an if statement. We can do this because the variable Result is equal to whatever choice the player chose. The way to make an if statement is like this:

If (variable=....) Then

This is how you start if statements. If statements are very useful, and you will find yourself using them very often in scripting. Instead of using if three times for the DialogInput choices you can(and should) you use ElseIf

ElseIf (variable=...) Then

The Else statement is run when the condition isn't met for any of the other statements. Usually Else is used as the last part of the if statement. Else When you use an Else statement you don't need to put any condition next to it. You can just leave it as Else. Finally, we have the last new line of code: EndIf The EndIf is used to signify that the if statement is over, and to continue with the code. This is required at the end of all if statements. If you do not put this, your code will not run. So always remember it!

What should I know by now? Going in to the next tutorial, you should know how to make a simple dialog script that also accepts input.

What do I do know? This tutorials homework assignment? Expand on your first homework assignment. Open a dialog box with the title, "Hello Game". Next, output to the player "Welcome to my game". Next give the player 3 choices, Exit, Wait, and More. If the player chooses Exit then close the dialog box. If the player chooses Wait, then pause the dialog box for 10 seconds. If the player chooses more, then output the line, "You clicked on More!" Reminders: Don't forget, that after the player clicks on his choice, he doesn't want to be stranded in the dialogbox. Make sure you close the box after a certain period of time. If you can successfully do this, consider yourself a master of this tutorial, and be ready to move on. If not, like I said, keep trying, and you will get it!

How do I use this script? Once again, to use this script, just set it as the right click script of an NPC. 

Part 3 - Loops loops and more loops Edit

Welcome to part 3 of the complete scripting guide. In part 3 you will learn about loops and some more conditional statements. Type up a new script so that it looks like this. Don't copy and paste the code, make sure you type it all out, no matter how tedious it is. 

Using "RC_Core.rcm"
Function Main()
Repeat
  Player = Actor()
  Target = ContextActor()
  D = OpenDialog(Player, Target, "Dialog box")
  DialogOutput(Player,D,"What do you want to do?")
  X=DialogInput(Player,D,"Keep Going|Stop!","|")
  Select X
  Case 1
    DialogOutput(Player,D,"Ok!")
  Case 2
    DialogOutput(Player,D,"I will stop now!")
   End Select
  DoEvents(1000)
  CloseDialog(Player, D)
Until X=2
End Function

 Mini Lesson 2: Repeat Until Repeat-this is your first loop. A loop repeats the code inside of it until its exit condition is met. What is an exit condition? An exit condition would be something like this: Until X=1. So basically our code is this: 

Repeat 
do this code
Until x equals to 0

So for X to equal to 0, you would have to choose Stop, because if you noticed, I set X to equal our DialogInput!So until you choose choice 2(stop) the code wil keep on looping! How do I create an endless loop? Lets say you wanted to trap your player in an endless loop, just change this: 

Until X=2 
To:
Forever

A forever loop, will go on....forever! It will never stop unless you have this command in your code: Return. The return command immediately stops your script, no matter what loop its in, or where it is. For example: 

Using "RC_Core.rcm"
Function Main()
Repeat
  Player = Actor()
  Target = ContextActor()
  D = OpenDialog(Player, Target, "Dialog box")
  DialogOutput(Player,D,"What do you want to do?")
  X=DialogInput(Player,D,"Keep Going|Stop!","|")
  Select X
  Case 1
    DialogOutput(Player,D,"Ok!")
  Case 2
    DialogOutput(Player,D,"I will stop now!")
    Return
  Default
    Return
   End Select
  DoEvents(1000)
  CloseDialog(Player, D)
Forever
End Function

 Watch what happens now! As soon as you click on stop, the script ends, and the dialog box closes. What happens when you enter in this code? You should see a dialog box open up and say Watch this. Then after a second it will close [you won't see it close(because it happens so fast)], but you will see how the dialog box clears.

Now lets dissect this script: First: Since you just learnt about loops we will go straight to the next piece of code. X=DialogInput(Player,D,"Keep Going|Stop!","|") I want you to notice how instead of using the word, Result=DialogInput, I used X instead. This is to stress that you don't need to use result, you can use whatever variable name you want. Next: Select X What is this? This is the beginning of a switch(also called select) statement. A switch statement takes a value, in this case x, and runs certain code based on its value. In this example, if x=1 then the code under case 1 will be run, if x=2 then the code in case 2 will be run. The default condition is like the else condition in if statements, it only runs if none of the other cases are met. So if x=3 or x=4 then the code in the default case is run.So now you know about the repeat loop and the switch statement. Time to learn some more! So lets say you didn't like the two ways I showed you, to make the above script. You thought that they were too long, and want to learn a new way to do it. Type this in: 

Using "RC_Core.rcm"
Function Main()
X=0
While X<2
  Player = Actor()
  Target = ContextActor()
  D = OpenDialog(Player, Target, "Dialog box")
  DialogOutput(Player,D,"What do you want to do?")
  X=DialogInput(Player,D,"Keep Going|Stop!","|")
    DialogOutput(Player,D,"Ok!")
  DoEvents(1000)
  CloseDialog(Player, D)
Wend
End Function

 The code is much more condensed now! The only new lines of code: While X<2 This is pretty self explanatory. It says that while X is less than 2 then do the following code. Wend is just the end of the loop. For example, with if statements you used EndIf's, with a while statement you use Wend. There is one difference though. I set X=0 before the while statement. Why did I do that? X needs to be declared before it can be used, so I had to create the X before I could use it.

What should I know by now? If you really want to be a successful scripter, then I would reccomend memorizing how to do this, and then practicing it. The only way you will learn how to script is to practice, practice, and practice some more. Going in to the next tutorial, I expect you to know how to make a simple dialog script off the top of your head. Now, I know you won't get it right away, nobody does, but at least try. Even if you have to look back try and see if you can recreate this script, and if your not successful try again. When you finally get it, it will have all been worth it.

What do I do know? This tutorials assignment? Open a dialog box with the title, "Loop". Next, output to the player "YOU ARE IN AN ENDLESS LOOP". Give them 5 choices, of which only one of them will exit them from the loop(using a switch statement). If you can successfully do this, consider yourself a master of this tutorial, and be ready to move on. If not, like I said, keep trying, and you will get it!

How do I use this script? Set it as the right-click script of an NPC