User Tools

Site Tools


4rpl:commands:specialsyntax

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
4rpl:commands:specialsyntax [2021/01/15 00:43] – added a detailed description of how square and curly brackets work Sanian4rpl:commands:specialsyntax [2025/02/14 14:57] (current) – external edit 127.0.0.1
Line 1: Line 1:
 <=[[4rpl:start| Index]] <=[[4rpl:start| Index]]
  
-==== Description ====+====== Special Variable Syntax ======
  
-4rpl supports special syntax for referencing vectors, lists, and tables.+4RPL supports special syntax for referencing global variables, vectors, lists, and tables.
  
 ---- ----
 +===== Global Variables =====
 +<-* ->* -?* --* <-!* ->!* -?!* --!*
  
-==== Vectors ====+=== Description === 
 +All of the [[4rpl:start#variables | variable commands]] can also be used followed by the ''*'' symbol in order to access __global__ variables  
 +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. 
  
-Vectors contain up to 4 fields. These fields can be referenced with .x,.y,.z,.w, .0,.1,.2,.3, or .r,.g,.b,.a. Simply place the '.x' after the variable name for reading or writing.+Global variables **belong to a map** and can be accessed from within different CPACK's. For this reason it is recommended to stick to a unique naming convention to prevent collisions with other CPACK's. 
 + 
 +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 ->*disableMyCustomUnits 
 +</code> 
 +<code 4rpl> 
 +# --CustomUnit-- 
 +if (<-*disableMyCustomUnits) 
 +    SetUnitDebugText(self "Disabled"
 +else 
 +    SetUnitDebugText(self ""
 +    @Operate 
 +endif 
 +</code> 
 +---- 
 +<=[[4rpl:start| Index]] 
 + 
 +---- 
 +===== 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)  
 + 
 + 
 +Simply place the suffix after the variable name for reading or writing.  
 +Suffixes are fungible.
  
 === Example === === Example ===
Line 19: Line 61:
 V3(255 0 0) ->red V3(255 0 0) ->red
 </code> </code>
 +
 +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: 
 +  ?   V4(1,1,1,1) 
 +  : 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, until at 0 it would render the text invisible
  
 ---- ----
  
-==== Lists ====+<=[[4rpl:start| Index]] 
 +---- 
 +===== 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 in a list can be referenced using specific APIS, like [[4rpl:commands:GetListElement|GetListElement]] (and other [[4rpl:start#lists|List APIs]]). They can also be accessed using square bracket notation ''[<-index]'', which is a compact way of writing list-element getters/setters.+<=[[4rpl:start#lists| List Commands]] \\
  
-''[<-index]'' can be written on its own and will translate to ''<-index GetListElementRPL'', effectively getting the element at the given index from list at the top of the stack. If the item on the stack is no list an error will be printed to the console.+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 in a list can be referenced using specific APIS,  
 +like [[4rpl:commands:GetListElement|GetListElement]] (and other [[4rpl:start#lists|List APIs]]).  
 +They can also be accessed using square bracket notation ''[<-index]'', which is compact way of writing  
 +list-element getters/setters.
  
-However, if ''[<-index]'' is preceded by a variable //setter//, ''->variable [<-index]'', it along with the variable setter will translate to ''<-variable <-index SetListElementRPL'', effectively setting element number "index" of the variable to whichever value is on top of the stack. If **variable** is not a list, a warning will be printed to the console.+''[<-index]'' can be written on its own and will translate to ''<-index GetListElementRPL'',  
 +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 ''[<-index]'' is preceded by a variable //setter//, ''->variable [<-index]'',  
 +it along with the variable setter will translate to ''<-variable <-index SetListElementRPL'',  
 +effectively setting element number "index" of the variable to whichever value is on top of the stack.  
 +If **variable** is not a list, a warning will be printed to the console. 
 + 
 +See also: [[4rpl:commands:specialsyntax#coerce table to list]]
  
 === Example === === Example ===
Line 37: Line 112:
 traceallsp(<-list[0]) traceallsp(<-list[0])
 </code> </code>
 +
 +
 +
 +<=[[4rpl:start| Index]]
  
 ---- ----
 +===== Tables =====
  
-==== Tables ====+<=[[4rpl:start#Tables| Table Commands]] \\
  
-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:commands:GetTableElement|GetTableElement]] (and other [[4rpl:start#tables|Table APIs]]). Data can also be accessed using curly brace syntax ''{<-key}'', which is a compact way of writing table-element getters/setters.+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:commands:GetTableElement|GetTableElement]] (and other  
 +[[4rpl:start#tables|Table APIs]]).  
 +Data can also be accessed using curly brace syntax ''{<-key}'', which is a compact way of writing  
 +table-element getters/setters.
  
-''{<-key}'' can be written on its own and will translate to ''<-key GetTableElementRPL'', effectively getting the element at the given key from a table at the top of the stack. If the item on the stack is no table an error will be printed to the console.+''{<-key}'' can be written on its own and will translate to ''<-key GetTableElementRPL'', effectively getting  
 +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 ''{<-key}'' is preceded by a variable //setter//, ''->variable {<-key}'', it along with the variable setter will translate to ''<-variable <-key SetTableElementRPL'', effectively setting the value of 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.+However, if ''{<-key}'' is preceded by a variable //setter//, ''->variable {<-key}'', it along with the  
 +variable setter will translate to ''<-variable <-key SetTableElementRPL'', effectively setting the value of 
 + 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 ===
Line 54: Line 145:
 42 ->table{"SomeName"} 42 ->table{"SomeName"}
 traceallsp(<-table{"SomeName"}) traceallsp(<-table{"SomeName"})
 +</code>
 +
 +==== 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!
 +
 +"Cannon"    V2(0.03 75) 
 +"Mortar"    V2(0.05 25) 
 +"Sniper"    V2(0.02 20) 
 +Table ->units 
 +
 +# method One
 +
 +Do (GetTablecount(<-units) 0)
 + GetUnitsByType (<-units[I] 1) ->unitList
 + <-units{<-units[I]} ->value
 + TraceAllSp ("Found:" GetListCount(<-unitList) <-units[I]  <-value )
 +Loop
 +
 +# method Two
 +
 +GetTableKeys(<-units)->keys
 +Do (Getlistcount(<-keys) 0) 
 + <-keys[I] ->k
 + GetUnitsByType (<-k 1) ->unitList
 + <-units{<-k} ->value
 + TraceAllSp ("Found:" GetListCount(<-unitList) <-k  <-value )
 +Loop
 </code> </code>
  
  
4rpl/commands/specialsyntax.1610671423.txt.gz · Last modified: 2025/02/14 14:56 (external edit)