This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
4rpl:commands:specialsyntax [2021/01/14 16:43] – old revision restored (2021/01/14 10:17) Karsten75 | 4rpl:commands:specialsyntax [2025/02/14 14:57] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ~~NOTOC~~ | ||
< | < | ||
- | ==== Description | + | ====== |
- | 4rpl supports special syntax for referencing vectors, lists, and tables. | + | |
+ | 4RPL supports special syntax for referencing | ||
---- | ---- | ||
+ | ===== Global Variables ===== | ||
+ | <-* ->* -?* --* <-!* ->!* -?!* --!* | ||
+ | |||
+ | === Description === | ||
+ | All of the [[4rpl: | ||
+ | instead. Global variables do not belong to one single script, reading or writing to a global variable will | ||
+ | modify the same exact value from any script. | ||
+ | |||
+ | Global variables **belong to a map** and can be accessed from within different CPACK' | ||
+ | |||
+ | Global variables may have the same names as local variables, there is no interference between them. | ||
+ | |||
+ | One example global variables may be used for is storing values which should always be the same across all units. | ||
+ | That way it isn't necessary to send a message to each individual unit. | ||
+ | |||
+ | === Examples === | ||
+ | <code 4rpl> | ||
+ | # --SomeScript-- | ||
+ | true -> | ||
+ | </ | ||
+ | <code 4rpl> | ||
+ | # --CustomUnit-- | ||
+ | if (< | ||
+ | SetUnitDebugText(self " | ||
+ | else | ||
+ | SetUnitDebugText(self "" | ||
+ | @Operate | ||
+ | endif | ||
+ | </ | ||
+ | ---- | ||
+ | < | ||
+ | |||
+ | ---- | ||
+ | ===== Vectors ===== | ||
+ | === Description === | ||
+ | |||
+ | Vectors contain up to 4 fields. | ||
+ | These fields can be referenced with a suffix that implies the use of the vector | ||
+ | |||
+ | * .x .y .z .w (coordinates) | ||
+ | * .r .g .b .a (color) | ||
+ | * .0 .1 .2 .3 (index) | ||
+ | |||
- | ==== Vectors ==== | + | Simply place the suffix |
- | Vectors contain up to 4 fields. These fields can be referenced with .x, | + | Suffixes are fungible. |
=== Example === | === Example === | ||
- | <code 4rpl> | + | |
+ | <code 4rpl> | ||
+ | V4(1 2 3 4) ->vector | ||
traceallsp(< | traceallsp(< | ||
42 -> | 42 -> | ||
V3(255 0 0) ->red | V3(255 0 0) ->red | ||
</ | </ | ||
+ | |||
+ | A brief note on color values: | ||
+ | |||
+ | Color is represented by 4 values, each of the first 3 the intensity of one of the primary colors (Red, Green and Blue) and the 4th value the Alpha channel. Alpha values indicate transparency or opacity. All of these values are floating-point values ranging from 0 to 1, with higher values truncated to 1. | ||
+ | |||
+ | Example: | ||
+ | ? | ||
+ | : is white. | ||
+ | ? V4(1 0 0 1) | ||
+ | : is red. | ||
+ | ? V4(0 1 0 1) | ||
+ | : is green. | ||
+ | ? V4(0 0 1 1) | ||
+ | : is blue. | ||
+ | | ||
+ | Combining different values result in any other intermediate color. | ||
+ | |||
+ | Changing the 4th field to a lesser value would increase the transparency, | ||
---- | ---- | ||
- | ==== Lists ==== | + | < |
- | Lists are collections of data. They are similar to arrays but they do not have a fixed length. They can have items added or removed. The items in a list can be referenced using specific APIS, like GetListElement (and other List APIS). They can also be accessed using bracket notation ' | + | ---- |
+ | ===== Lists ===== | ||
+ | |||
+ | < | ||
+ | |||
+ | Lists are collections of data. They are similar to arrays but they do not have a fixed length. | ||
+ | They can have items added or removed. The elements | ||
+ | like [[4rpl: | ||
+ | They can also be accessed using square | ||
+ | list-element getters/ | ||
+ | |||
+ | '' | ||
+ | effectively getting the element at the given index from a list at the top of the stack. | ||
+ | If the item on the stack is no list an error will be printed to the console. | ||
+ | |||
+ | However, if '' | ||
+ | it along with the variable setter will translate to ''< | ||
+ | effectively setting element number " | ||
+ | If **variable** is not a list, a warning will be printed to the console. | ||
+ | |||
+ | See also: [[4rpl: | ||
=== Example === | === Example === | ||
- | <code 4rpl> | + | |
- | " | + | <code 4rpl> |
+ | CreateList ->list | ||
+ | " | ||
traceallsp(< | traceallsp(< | ||
</ | </ | ||
+ | |||
+ | |||
+ | |||
+ | < | ||
---- | ---- | ||
+ | ===== Tables ===== | ||
- | ==== Tables | + | <=[[4rpl: |
- | Tables are collection of named data. They are sometimes called dictionaries in other languages. They contain data made up of some string name and some value. A value can be quickly looked up by its name. Data in a map can be referenced with GetTableElement (and other Table APIs). Data can also be accessed using curly brace syntax ' | + | |
+ | Tables are collection of named data. They are sometimes called dictionaries in other languages. | ||
+ | They contain data made up of a string name (the key) and a value. A value can be quickly looked up by its | ||
+ | name/key. | ||
+ | Data in a table can be referenced with [[4rpl: | ||
+ | [[4rpl: | ||
+ | Data can also be accessed using curly brace syntax | ||
+ | table-element getters/ | ||
+ | |||
+ | '' | ||
+ | the element at the given key from a table at the top of the stack. If the item on the stack is not a table, | ||
+ | an error will be printed to the console. | ||
+ | |||
+ | However, if '' | ||
+ | variable setter will translate to ''< | ||
+ | that key in the variable to whichever value is on top of the stack. If **variable **is not a table, | ||
+ | a warning will be printed to the console. | ||
=== Example === | === Example === | ||
- | <code 4rpl> | + | |
+ | <code 4rpl> | ||
+ | CreateTable ->table | ||
42 -> | 42 -> | ||
traceallsp(< | traceallsp(< | ||
</ | </ | ||
+ | ==== Coerce table to list ==== | ||
+ | It is possible to iterate over the elements in a table by coercing the table into a list. The following shows two equivalent methods to extract data from a table. Method Two is slightly more efficient. | ||
+ | === Example === | ||
+ | <code 4RPL> | ||
+ | #Make sure some units are on the map when testing! | ||
+ | |||
+ | " | ||
+ | " | ||
+ | " | ||
+ | Table -> | ||
+ | |||
+ | # method One | ||
+ | |||
+ | Do (GetTablecount(< | ||
+ | GetUnitsByType (< | ||
+ | < | ||
+ | TraceAllSp (" | ||
+ | Loop | ||
+ | |||
+ | # method Two | ||
+ | |||
+ | GetTableKeys(< | ||
+ | Do (Getlistcount(< | ||
+ | < | ||
+ | GetUnitsByType (<-k 1) -> | ||
+ | < | ||
+ | TraceAllSp (" | ||
+ | Loop | ||
+ | </ | ||