4rpl Scripting Reference: Variables, Math, Loops, Functions and Units
https://knucklecracker.com/wiki/doku.php?id=4rpl:start#stack
Core Concepts
- Variables: ->IntoVar, <-FromVar, "Var" NotPersist (won't persist across saves), GetType results "STRING INT FLOAT LIST TABLE NULL", IsNull
- a(b) warps to b a, e.g. add(1 2) becomes 1 2 add
Math
- Basic math: add, sub, mul, div, mod, mod2 (different negative results). 1 2 add ->Three / add(1 2) ->Three
- More math: RandInt, RandFloat, asint/asfloat, approximately (float compare), BallisticArcLateral, BallisticArcPos, SignalGenerator (square/saw/etc. waveforms), standard abs/ceil/floor etc.
- Vectors and angles: Vec1, Vec2, Distance, DistanceCell (2D), ShortestAngle, VectorAngleCell, VectorAngle, RotateTowards, RotateTowardsCell, DistancePointLine(point lineStart lineEnd), MoveTowards
- Math constants: PI, TwoPI, TAU, HalfPI, ThreeQuarterPI, ThreeHalvesPI, E, Rad2Deg, Deg2Rad, Vector0 ([0,0,0,0])
- Comparing: > is gt, < is lt, = is eq, neq is not equal, gte is >=, lte, any before + 0 exist, e.g. gte0
Debugging
- Tracing: "something" Trace, ... TraceAll / TraceAllSp (one line, with spaces) / TraceStack (all + type, not removed), ... Trace[2-5], ClearTraceLog
Stack Operations
- Stack commands: dup, dup2, swap, pop, ClearStack, StackSize
- Copying: Copy (tables/lists refer to the same elements), DeepCopy (creates new tables/lists)
Messages
- ADA messages: ShowADAMessage("MyMessage" TYPE), IsADAMessageRead, ShowGameMessage("MyMessage" "Btn0" "Btn1" bPauseGame "FuncCallbackOnBtn") #_DATA 0 or 1 btn
- InterScript messages: SendMsg("channel" "Message"), RegisterforMsg(<-channel <-callbackFunc), GetScriptVar(<-UID <-scriptName <-varName)
Miscellaneous Functions
- Random utilities: GetRandMapCell, IsV3InMap, IsV2InMap, SetFieldRect, Get/Set OrbitalCount, DamageMesh, GetNearest Spre/AirSac/SniperTarget, GenerateEnergy
Creeper
- Creeper control: Get/Set CreeperCutOff, Get/Set/Add AddCreeper{<-cellX <-cellZ <-amt}, ConvertCreeperToAC, ConvertACToCreeper, GetExactCreeperHeight, GetTotalCreeper, Get/Set Flow
Sound
- Playback: PlaySound(<-soundName <-volume), PlaySoundLoop(<-soundName <-volume <-unit). Volume ranges from 0 to 1.
Timers
- Timer functions: Get/Set Timer0-3, SetTimerObeyPause0-3
If Statements and Loops
once ... endonce runs once, useful for startup code.
a b comp if ... [else ... ] endif / if(a b comp) ... [else ...] endif
while a b comp repeat ... endwhile / while repeat(a b comp) ... endwhile
stop start do ... loop / do(5 2) trace(I) loop - #2 #3 #4
break exits a loop it is in when run.
I, J, K: do loop indexes (for nested loops) - 2 0 do 4 2 do6 4 do K J I Trace3 loop ...
Functions
:funcName ... #another function - defines a function that can be called. Note the initial colon.
return - immediately stops running a function and returns to the calling location.
@funcName - calls function named funcName (hardcoded).
"funcName" refcall - calls function with name funcName (from stack).
Units
Self - pushes the unit's ID onto the stack.
CreateUnit(<-CModUID <-pos <-initParamsTable) ->unitUID (CModUID from CPACK manager)
"abcdefgh1234567" ->cmodUID
CreateTable ->initParams
"42" ->initParams{"Foo"}
"HHGTTG" ->initParams{"Bar"}
CreateUnit(<-cmodUID V3(10 5 20) <-initParams) ->unitUIDDestroyUnit(<-UID <-suppressEffect <-userInitiated <-doNotLog)
CreateEffect(<-effectName <-pos <-scale). Effects: sparks, materialize, radial, radial2, explosion, explosion2, explosion3, explosion4, explosion5.
CreateMissile(<-worldPos <-rotation <-targetUnit) ->missileUID CreateMissile(GetUnitPosition(self) V3(-90 0 0) <-targetUnit) ->missile
Get/Set UnitPosition
GetUnitsInRange(<-startPos <-range <-isSquare <-is3D <-requireLOS <-enemyState <-builtState <-imperviousState) ->units
0 ->cellX 15 ->cellY 20 ->range false ->isSquare true ->is3D true ->requireLOS 2 ->enemyState 1 ->builtState 1 ->imperviousState GetTerrain(<-cellX <-cellY) 1 + ->terrain GetUnitsInRange(V3(<-cellX <-terrain <-cellY) <-range <-isSquare <-is3D <-requireLOS <-enemyState <-builtState <-imperviousState) ->units
GetUnitsByType(<-unitType <-builtState) ->units. builtState is an int (0, 1 or 2): 0 = ignore built state, 1 = only find units that are built, 2 = only find units that are not built.
SetUnitPosition(self MoveTowardsAndAvoid(GetUnitPosition(self) <-target <-speed <-minHeight 1 true true true))
https://knucklecracker.com/wiki/doku.php?id=4rpl:start#unit_attributes