This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
4rpl:overview [2021/01/14 18:15] – virgilw | 4rpl:overview [2025/02/14 14:57] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
< | < | ||
+ | |||
+ | :!: To edit files you will need an editor. Read how to get started with [[creating and editing 4rpl scripts]]. | ||
====== Scripting ====== | ====== Scripting ====== | ||
Line 6: | Line 8: | ||
Scripting in CW4 uses a custom language, **4RPL**. It is stack-based, | Scripting in CW4 uses a custom language, **4RPL**. It is stack-based, | ||
- | 4RPL commands either read data from the //stack// or place data on the // | + | 4RPL commands either read data from the //stack// or place data on the // |
- | 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 <code 4rpl>2 4</ | + | 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 |
+ | |||
+ | <code 4rpl> | ||
+ | 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 [[cw4: | An easy way to familiarize yourself with 4RPL is to use the [[cw4: | ||
- | As an illustration, | + | As an illustration, |
<code 4rpl> | <code 4rpl> | ||
- | 2 4 + | + | 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. [[4rpl: | + | The code above puts 2 numbers on the stack, the '' |
+ | |||
+ | :!: Note that 4RPL is a // | ||
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. | 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. | ||
<code 4rpl> | <code 4rpl> | ||
- | | + | 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 ((For more information on the game coordinate system and why we use **x** and **z** coordinates, | ||
+ | |||
+ | |||
+ | ===== 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 " | ||
+ | |||
+ | <code 4rpl> | ||
+ | # 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 | ||
</ | </ | ||
- | 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 ((For more information on the game coordinate system and why we use `x` and `z` coordinates, | ||
===== Warp Notation ===== | ===== 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 | + | 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 ==== | ||
<code 4rpl> | <code 4rpl> | ||
- | 12 23 100 AddCreeper | + | 12 23 100 AddCreeper |
- | | + | AddCreeper(12 23 100) # Does the same thing as the line above. |
+ | |||
+ | 1 (2 3 +) 4 # Demonstrate wrapping affects all stack locations. | ||
+ | 2 3 + 1 4 # This line is the same as the one above. | ||
</ | </ | ||
===== Conditionals ===== | ===== Conditionals ===== | ||
- | 'If' | + | **If** statements in 4RPL consist of an **if**, optional **else** and a closing |
<code 4rpl> | <code 4rpl> | ||
Line 49: | Line 74: | ||
endif | endif | ||
</ | </ | ||
+ | |||
+ | |||
+ | |||
+ | Note: See [[4rpl:Data Types]] for comparison between dissimilar types and type conversion. | ||
+ | |||
+ | |||
+ | |||
+ | ===== Variables ===== | ||
+ | The stack is not the only place to store information in 4RPL, it is possible to take items from the stack and store them in // | ||
+ | |||
+ | <code 4rpl> | ||
+ | 3 4 + ->result | ||
+ | "3 plus 4 is" <-result TraceAllSP | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | Note: See [[4rpl:Data Types]] for comparison between dissimilar types and type conversion. | ||
+ | |||
+ | |||
+ | |||
+ | ===== Functions ===== | ||
+ | 4RPL supports user-defined functions that can be called from anywhere in the script. Functions are declared at the end of a script and are formatted as **: | ||
+ | |||
+ | <code 4rpl> | ||
+ | 4 2 @Function1 Trace # prints " | ||
+ | @Function2 | ||
+ | |||
+ | :function1 | ||
+ | add # adds together 2 items on the stack | ||
+ | 5 add # adds an extra 5 to that | ||
+ | |||
+ | :function2 | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | Read more about functions on the pages for [[4rpl: | ||
+ | |||
===== Loops ===== | ===== Loops ===== | ||
- | 4rpl support | + | 4RPL supports |
- | For an example of a 'do' | + | For an example of a **do** loop see: [[4rpl: |
- | For an example of a 'while' | + | For an example of a **while** loop see: [[4rpl: |
- | ==== Comments | + | \\ |
- | Comments in 4RPL can be either | + | |
- | Comments | + | ---- |
+ | ===== Working with CPACKs ===== | ||
+ | When a CPACK is imported, it does not overwrite any scripts that might be on disk. So if there are scripts on disk those will get jammed back into the CPACK whenever a compile is done (and a compile is done during finalization). | ||
+ | <WRAP width center twothirds> | ||
+ | < | ||
+ | %HOMEPATH%\Documents\My Games\creeperworld4\creeperworld4\editor\map2\cpacks\[CPACKNAME] 92415695-49d6-4e97-852b-64493e76233b\scripts\ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | If you load map as a regular player and then enter edit mode, the game will stick the scripts into the _UNKNOWN project directory. If there are already matching scripts present at that location, they will not be overwritten (importing | ||
+ | |||
+ | The case of on-the-fly opening the editor in a map you are playing is no different -with one exception: The game doesn' | ||
+ | |||
+ | If you want to grab the scripts from a map (like an FPS map) and you want to make sure you have the latest scripts, you should either create a project for the map and open it from the project page, or you should clean your _UNKNOWN directory before you open the map on the fly. | ||
+ | |||
+ | Basically, just deleting the _UNKNOWN directory is the easiest way to clean it | ||
- | <code 4rpl># This is how we add two of the three numbers | ||
- | # that are on the stack | ||
- | # Below is code # | ||
- | 2 4 5 add # adds 4 and 5 to get 9</ | ||
- | ==== Symbol Aliasing ==== | + | ---- |
- | Many arithmetic operators | + | |
- | ^ Operator ^ Symbol ^ | + | ===== Code translator |
- | | ADD | + | | + | |
- | | SUB | - | | + | |
- | | MUL | * | | + | |
- | | DIV | / | | + | |
- | | MOD | % | | + | |
- | | AND | && | | + | |
- | | OR | %%||%% | | + | |
- | | NOT | ! | | + | |
- | | POW | %%^%% | | + | |
- | | GT | > | | + | |
- | | GTE | >=| | + | |
- | | LT | < | | + | |
- | | LTE | %%<=%% | | + | |
- | | EQ | == | | + | |
- | | NEQ | != | | + | |
- | + | ||
- | ==== Code translator ==== | + | |
[[https:// | [[https:// | ||
For those struggling to master 4RPL's post-fix format, this may be a useful tool. It can be obtained from [[https:// | For those struggling to master 4RPL's post-fix format, this may be a useful tool. It can be obtained from [[https:// | ||