Object Pascal basics

TURBU

Object Pascal basics

 

DISCLAIMER:  This is not meant to be a comprehensive tutorial on writing Object Pascal code.  There are plenty of those available on the Internet.  What it does do is give you a bit of information to get you up to speed on the fundamentals.

 

The scripting system in TURBU is based on Object Pascal.  Object Pascal is an all-purpose programming language that provides a good balance between ease of use and raw power.  Writing programs or scripts in Object Pascal is based on using instructions to manipulate data.  The data is set up as variables, which are a lot like variables in algebra: a letter or word that refers to a bit of information whose value is not always the same.  In algebra, variables are used in place of numbers.  In programming, however, a variable can represent any kind of information: a number, a sentence, an image, a sound, and so forth.  There are six basic types of variables used in TURBU's script engine:

 

  • Integer:  A whole number, with nothing after the decimal point.
  • Real: Any real number, including decimals.
  • Boolean: A "switch" value which can be either true or false.
  • String:  A group of letters.  This can represent a word, a sentence, a paragraph, or almost any amount of text.  If you use text in your code, to assign to a string variable or send to a routine that needs a string, (there'll be more on routines later,) you have to put the text inside 'single quotes' (not "double quotes").  So:  'Hello everybody!' is good. "Hello everybody!" is bad, and so is Hello everybody! without quotes of any kind.  NOTE: Make sure to only use ordinary, straight quotes, not the curly "smart quotes" that word processing programs like to create.
  • Array:  A group of variables of the same kind that are all grouped together.  Picture a row of mailboxes, with each one only big enough to hold one envelope.  Every mailbox is exactly like every other mailbox except for two things: their location, and what they contain.  This is how arrays work; such a group of mailboxes would be referred to by programmers as an "array of envelopes", since it's not the boxes themselves that are important, but what they hold.
  • Object:  An object is a group of variables that are not all of the same kind but are all grouped together.  Objects can be made up of integers, booleans, strings, arrays, other objects, or other data types that are beyond the scope of this tutorial.  Objects also contain their own code for managing their data members.

 

Referring to variables

 

Variables are referenced by a name.  A variable name is a single word or letter.  It can also contain numbers and the underscore character, if the programmer wishes to use them, but the first character in the name must be a letter.  It's best to use descriptive names that are easy to read and understand.  For example, the variables that make up a Hero object have names such as "name", "level", "exp", "hp", "maxHp" and so on.  How you refer to a variable depends on where the variable is located.

A variable that stands on its own (that is not part of an array or an object) is referred to simply by its name.

A variable that is part of an array is referred to by the array's name, plus the specific variable's index in square brackets.  For example, Switch #21 would be switch[21], or hero #5 would be hero[5].

A variable that is part of an object is referred to by the object's name, plus a dot (period), plus the variable's name.  So the amount of EXP that hero #5 has would be found at hero[5].exp and the amount of money the party currently has is party.money.

 

Manipulating variables

 

Now that you know how to refer to variables by name, all that's left is to understand how to do things with them.  There are two basic things that can be done with a variable:  It can be manipulated directly with operators, or passed to a routine as input.

 

Operators:

Most of the operators should be familiar to anyone who took math classes in school.  + adds two things together.  - subtracts one value from another.  * multiplies two values, and / divides. The word mod is also used as an operator.  It produces the remainder when one number is divided by another.  And parenthesis ( ) can be used to enforce a certain order of operations.

 

There are two other important operators: = and :=.  An equals sign is used to test variables to see if one is equal to the other.  It produces a boolean (true or false) value, and is commonly used in IF..THEN statements, such as if switch[7] = true then.  Equals does not set a variable equal to a certain value.  Let me repeat that.  The = operator DOES NOT set a variable to a certain value in Object Pascal.  That's a very easy mistake for new Object Pascal programmers to make, especially if they've used BASIC or C/C++ in the past.  To set a variable to a value, you use the assignment operator, colon-equals.  For example:  variable[10] := party.money;

 

There are five other comparison operators, in addition to =.  They are: < (less than), <= (less than or equal to), > (greater than), >= (greater than or equal to), and  <> (not equal to).  These all give boolean results.  For example, if x is an integer variable whose value is currently 5:  x > 4: true.  x > 6: false.  x > 5: false.  x >= 5: true. x < 6: true. x <= 4: false. x = 5: true. x = 6: false. x <> 6: true.

 

Routines:

A routine is a block of code that does a specific thing and runs when its name is "called" by the program code.  Routines often require variables as input, and then do work on those variables.  For example, the showMessage routine requires a string variable.  It takes the text in that string and displays it in the in-game message box.  So, showMessage(hero[3].name); would open the message box and output hero #3's name in it.

There are two basic types of routines, procedures and functions.  A procedure simply takes its inputs, if any, and executes its code, while a function takes its inputs, executes its code, and produces a result, which is a variable of a certain type.  The showMessage routine mentioned above is a procedure.  There's a similar routine, however, called inputNumber, which takes an integer as input, describing how many digits the number to be input should have, then opens a message box with a number selecter in it.  When the user inputs a number, the inputNumber routine sends this number back to whatever code called it as its return value.

Function return values are handled as if the entire function was a variable.  So, to call inputNumber to have it produce a 3-digit number and store that number in variable #38, you would write: variable[38] := inputNumber(3);  Likewise, a routine that belongs to an object is referred to just like a variable that belongs to an object: by the object's name, a dot, and the routine's name.  So, to equip hero #1 with item #28, (assuming that item #28 is a type of weapon or armor,) you would use: hero[1].equip(28);

  

End of line semicolon

 

You may have noticed that the examples of actual lines of code used above all end with a semicolon ; character.  This is an essential part of Object Pascal syntax.  The semicolon is necessary to tell the computer where each command ends and the one after it begins.  There are a few very specific exceptions which we won't get into here, but as a general rule, it's essential to end every line of code with a semicolon.

 

That's it... for now

 

This tutorial has barely scratched the surface of Object Pascal or RPG Script.  It will be expanded somewhat as the scripting feature becomes more important.  If you really want to learn more about the language, run a web search for "Pascal tutorial" or "Delphi tutorial".  More information about the variables, objects and routines available in RPG Script and the TURBU game engine can be found in the RPG Script Reference section.

This help file was created with the free trial version of HelpScribble.