User Tools

Site Tools


4rpl:overview

This is an old revision of the document!


4RPL Scripting Language

Scripting

Scripting in CW4 uses a custom language, 4RPL. It is stack-based, similar to an HP calculator or the Forth programming language.

4RPL commands either read data from the stack or place data on the stack. Data (arguments) on the stack are in LIFO (Last In, First Out) order. That means the argument that was placed on the stack last will be the first to be retrieved. Imagine this as a stack of coins. If you put a coin on the top, then the first coin to be taken off will also be the one that was last placed on the stack.

You can place arguments on the stack by typing them, or executing an instruction that will push arguments on to the stack. For instance, you can type

2 4

and then these two numbers will be pushed onto the stack when your script executes.

An easy way to familiarize yourself with 4RPL is to use the Console in the Mission Editor. Let's use this to illustrate the next few examples.

As an illustration, imagine you want to add two of the numbers you entered above. The instruction to perform addition is add. You can also simply use a “+” rather than the 'add' command.

   2 4 +
   TraceAllSP

The code above puts 2 numbers on the stack, the `add` (or +) command took the 4 and the 2 (last-in, first out) added them and pushed the result (6) back on the stack. TraceAllSp pops (takes/removes) all elements on the stack, puts a space in between them and prints them to the Console. A '6' is now printed on the console.

Part of 4RPL is the commands that manipulate map objects, such as creeper, terrain and units. Here is a simple script to put some creeper on a specific cell on a map.

  12 23 100 AddCreeper  

Run this once in the console and then unpause the game and let the game simulation run a few frames. You will see a stack of 100 deep creeper at map coordinate x=12 and z=23 1).

Warp Notation

Warp notation is an optional notation that allows the use of parentheses to change the order that things are written. The use of parentheses is called warp notation since it warps, or moves, things around. Parentheses can be added anywhere in 4rpl and will move the command that proceeded the opening parenthesis to the location of the closing parenthesis. This can be useful for making some expressions look more like functions in some other languages

Example

   12 23 100 AddCreeper
   AddCreeper(12 23 100) #Does the same thing as the line above.

Conditionals

'If' statements in 4rpl consist of an 'if' an option 'else' and a closing 'endif' command. The 'if' command pops the item from the top of the stack and evaluates it as a boolean (true or false, not 0 or 0). If the item is not true, then execution jumps to either the 'else' or the 'endif' instruction in the script.

if ( GetGameUpdateCount 900 >= )
   TraceAllSp("More than 30 seconds of game time have passed")
else
   TraceAllSp("Less than 30 seconds of game time have passed")
endif

Loops

4rpl support both “do/loop” and “while/repeat” looping constructs.
For an example of a 'do' loop see: do
For an example of a 'while' loop see: [ [4rpl:commands:while]]

Comments

Comments in 4RPL can be either a whole line or a partial line. The comment terminates when a line ends. Comments are indicated by the “hash” character (#),

# This is how we add two of the three numbers
# that are on the stack
# Below is code         #  Below is a comment
2 4 5  add              # adds 4 and 5 to get 9

Symbol Aliasing

Many arithmetic operators can be substituted with shorthand symbols.

Operator Symbol
ADD +
SUB -
MUL *
DIV /
MOD %
AND &&
OR ||
NOT !
POW ^
GT >
GTE >=
LT <
LTE <=
EQ ==
NEQ !=

Code translator

https://github.com/Arin112}Arin112 wrote a code translator that can translate in-fix 2) code to the 4RPL post-fix notation.

For those struggling to master 4RPL's post-fix format, this may be a useful tool. It can be obtained from github and is accompanied by many samples. Of course, it should be noted that the code and translation will only be up-to-date as long as the repository is maintained.

1)
For more information on the game coordinate system and why we use `x` and `z` coordinates, see map coordinate system
2)
Infix notation is the notation commonly used in arithmetical and logical formulae and statements. It is characterized by the placement of operators between operands—“infixed operators”—such as the plus sign in 2 + 2.
4rpl/overview.1610648156.txt.gz · Last modified: 2021/01/14 13:15 by virgilw