This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
crpl:crpltutorial:code_examples [2014/04/07 07:51] – [Frames to minutes] Clean0nion | crpl:crpltutorial:code_examples [2022/01/09 00:42] – added Make a non-moving unit only buildable in void Grabz | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ~~DISCUSSION~~ | + | |
- | < | + | |
< | < | ||
====== Code Examples ====== | ====== Code Examples ====== | ||
+ | |||
Code examples contributed by map makers and coders alike. Feel free to submit your own - it will be much appreciated. | Code examples contributed by map makers and coders alike. Feel free to submit your own - it will be much appreciated. | ||
You can also view the scripts in any map: make a copy of the map you want to inspect, rename the copy " | You can also view the scripts in any map: make a copy of the map you want to inspect, rename the copy " | ||
+ | |||
+ | <wrap hi> | ||
===== Manage Treasure Chests in Credits Mission ===== | ===== Manage Treasure Chests in Credits Mission ===== | ||
Line 757: | Line 759: | ||
</ | </ | ||
< | < | ||
+ | |||
+ | =====Survival Pod===== | ||
+ | Attach to a core to create a survival pod. Survival pods must be rescued. | ||
+ | |||
+ | < | ||
+ | if (not(< | ||
+ | FailMission | ||
+ | Destroy(Self 2) | ||
+ | endif | ||
+ | if ( not(< | ||
+ | AscendToOrbit | ||
+ | true -> | ||
+ | PlaySound(" | ||
+ | SetPopupText("" | ||
+ | ShowGameEventTag(" | ||
+ | endif | ||
+ | |||
+ | if (< | ||
+ | GetUnitTransformPosition ->z ->y ->x | ||
+ | # | ||
+ | CreateEffect(8 <-x <-y -1 2 2 0.04) | ||
+ | else | ||
+ | SetPopupText(GetUnitAttribute(Self CONST_HEALTH) "/" | ||
+ | SetPopupTextX(0) | ||
+ | endif | ||
+ | |||
+ | :Awake | ||
+ | if (GetUpdateCount eq (0)) | ||
+ | SetUnitAttribute(Self CONST_ISBUILDING TRUE) | ||
+ | SetUnitAttribute(Self CONST_BUILDCOST 30) | ||
+ | SetUnitAttribute(Self CONST_HEALTH 0) | ||
+ | SetPopupTextAlwaysVisible(true) | ||
+ | SetPopupTextY(-12) | ||
+ | endif | ||
+ | |||
+ | :Destroyed | ||
+ | CreateUnit(" | ||
+ | AddScriptToUnit(< | ||
+ | if (< | ||
+ | SetScriptVar(< | ||
+ | else | ||
+ | SetScriptVar(< | ||
+ | SetScriptVar(< | ||
+ | SetScriptVar(< | ||
+ | SetScriptVar(< | ||
+ | |||
+ | CreateUnit(" | ||
+ | AddScriptToUnit(< | ||
+ | |||
+ | ShowGameEventTag(" | ||
+ | |||
+ | endif | ||
+ | </ | ||
+ | < | ||
+ | |||
+ | =====PodMessage===== | ||
+ | Shows a message on the map that goes away. Used by the SurvivalPod script above. | ||
+ | |||
+ | < | ||
+ | $solidTime: | ||
+ | $message:" | ||
+ | $red:255 | ||
+ | $green:255 | ||
+ | $blue:255 | ||
+ | |||
+ | once | ||
+ | < | ||
+ | SetText(< | ||
+ | SetTextColor(< | ||
+ | endonce | ||
+ | |||
+ | if (GetUpdateCount gte(< | ||
+ | Destroy(Self 0) | ||
+ | endif | ||
+ | |||
+ | if (GetUpdateCount gt(1)) | ||
+ | if (GetUpdateCount gte(< | ||
+ | < | ||
+ | else | ||
+ | 255 ->alpha | ||
+ | endif | ||
+ | |||
+ | SetTextColor(< | ||
+ | SetImageColor(Self " | ||
+ | endif | ||
+ | |||
+ | :Awake | ||
+ | SetUnitAttribute(Self CONST_CREATEPZ FALSE) | ||
+ | SetUnitAttribute(Self CONST_TAKEMAPSPACE FALSE) | ||
+ | SetUnitAttribute(Self CONST_SUPPORTSDIGITALIS FALSE) | ||
+ | SetUnitAttribute(Self CONST_NULLIFIERDAMAGES FALSE) | ||
+ | SetUnitAttribute(Self CONST_COUNTSFORVICTORY FALSE) | ||
+ | |||
+ | SetScreenMode(TRUE) | ||
+ | SetTextY(-5) | ||
+ | SetTextSize(2) | ||
+ | SetScreenPixelCoordX(ScreenWidth div(2)) | ||
+ | SetScreenPixelCoordY(135) | ||
+ | |||
+ | SetImage(Self " | ||
+ | SetImageScaleX(Self " | ||
+ | SetImageScaleY(Self " | ||
+ | SetImageColor(Self " | ||
+ | |||
+ | PlaySound(" | ||
+ | </ | ||
+ | < | ||
+ | |||
+ | =====GetClosestUnitToCoords===== | ||
+ | A customizable method to grab a nearest unit of any kind. Specify what types of units you want to grab, at what position, and with what radius. Strings are used for clarity to grab units that you need, you can delete or comment out units you wont be using once you copy the code. The code contains 3 methods that are commented for use on the battle-field. Contains two additional convenience methods. | ||
+ | < | ||
+ | #Warning: use this convenience function sparingly, tens of CRPL Cores | ||
+ | #running this all at once every frame can slow the game down. | ||
+ | |||
+ | #Notation: x y f1 - i1 | ||
+ | #eg. CurrentCoords 10 @GetClosestUnitToCoords ->unit | ||
+ | #If all units are outside of the maxDistance or none found returns integer value ' | ||
+ | : | ||
+ | asfloat -> | ||
+ | asfloat ->gcu_y | ||
+ | asfloat ->gcu_x | ||
+ | StackSize -> | ||
+ | |||
+ | # Reference: https:// | ||
+ | # Place Unit Types that you /WANT/ to look for here. | ||
+ | # Simply comment '#' | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | #" | ||
+ | " | ||
+ | " | ||
+ | # ===== | ||
+ | |||
+ | #Finding the boundary stack size (how many elements were added to stack) | ||
+ | #and then forming a list from the units we want. | ||
+ | StackSize -> | ||
+ | CreateList -> | ||
+ | < | ||
+ | -> | ||
+ | < | ||
+ | loop | ||
+ | |||
+ | # | ||
+ | -1 -> | ||
+ | 999999 -> | ||
+ | |||
+ | #retrieve /all/ nearby units | ||
+ | |||
+ | # x y distance boolean(true-square, | ||
+ | # unit checks have been ordered in perceived processing complexity | ||
+ | <-gcu_x <-gcu_y < | ||
+ | < | ||
+ | < | ||
+ | -> | ||
+ | < | ||
+ | #get the distance | ||
+ | < | ||
+ | #is that distance closer? | ||
+ | < | ||
+ | #do we care about this unit type? | ||
+ | < | ||
+ | #set our new baseline distance and unit | ||
+ | < | ||
+ | < | ||
+ | endif | ||
+ | endif | ||
+ | loop | ||
+ | endif | ||
+ | |||
+ | < | ||
+ | |||
+ | #Function :UnitCoords | ||
+ | #Simple convenience method. Takes a unit and returns the coordinates. | ||
+ | #' | ||
+ | #Notation: n1 - x y | ||
+ | #eg. < | ||
+ | :UnitCoords | ||
+ | ->u | ||
+ | <-u CONST_COORDX GetUnitAttribute | ||
+ | <-u CONST_COORDY GetUnitAttribute | ||
+ | |||
+ | #Function : | ||
+ | #This function an object, then a list and returns a boolean value. | ||
+ | #Notation: L1 n1 - b1 | ||
+ | #eg. <-myList < | ||
+ | : | ||
+ | -> | ||
+ | -> | ||
+ | false -> | ||
+ | |||
+ | < | ||
+ | < | ||
+ | true -> | ||
+ | break | ||
+ | endif | ||
+ | loop | ||
+ | |||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | |||
+ | |||
+ | ===CRPL generating CRPL=== | ||
+ | This CRPL will generate CRPL with custom program (if it is in the map scripts) | ||
+ | < | ||
+ | CreateUnit(" | ||
+ | AddScriptToUnit(< | ||
+ | </ | ||
+ | <sub> Stolen from Teknotiss(who took it from virgil), where pyramids make golems. I simply changed golem to unit. Added by cpaca, known as Karma' | ||
+ | |||
+ | ===== Make a non-moving unit only buildable in void ===== | ||
+ | This code will make it so a unit can be only built in void and not on terrain. It only works properly for stationary units. | ||
+ | |||
+ | * This is a global script, so you have to add it in Package Manager -> Global Control -> Pre Update. | ||
+ | * **Tick the Run when paused box**, then in one of the UNITGUID fields paste the GUID of one or more void only units. | ||
+ | * The script does not work in editor, you can finalize the map, back out then open the finalized map and test there if it works. | ||
+ | * The unit must have the "Can build/land anywhere" | ||
+ | |||
+ | < | ||
+ | $UnitGUID0:"" | ||
+ | $UnitGUID1:"" | ||
+ | $UnitGUID2:"" | ||
+ | $UnitGUID3:"" | ||
+ | $UnitGUID4:"" | ||
+ | |||
+ | GetBuildUnit -> | ||
+ | |||
+ | #Only do something if state has changed | ||
+ | < | ||
+ | FALSE -> | ||
+ | FALSE -> | ||
+ | |||
+ | 5 0 do | ||
+ | " | ||
+ | <-val StringLength gt0 <-val < | ||
+ | TRUE -> | ||
+ | endif | ||
+ | <-val StringLength gt0 <-val < | ||
+ | TRUE -> | ||
+ | endif | ||
+ | loop | ||
+ | |||
+ | #If a void only unit was selected, and the last selection was not a void only unit | ||
+ | < | ||
+ | FALSE SetAllLegalUnitCells | ||
+ | < | ||
+ | TRUE UseLegalUnitCells | ||
+ | #If a non void only unit was selected, and the last selection was a void only unit | ||
+ | else < | ||
+ | FALSE SetAllLegalUnitCells | ||
+ | FALSE UseLegalUnitCells | ||
+ | endif endif | ||
+ | endif | ||
+ | |||
+ | #Save current state | ||
+ | < | ||
+ | |||
+ | #Initialize | ||
+ | :once | ||
+ | GetBuildUnit -> | ||
+ | @RebuildVoidCellsList | ||
+ | |||
+ | : | ||
+ | #Build list of void cells | ||
+ | CreateList -> | ||
+ | GetMapSize ->sizeZ ->sizeX | ||
+ | <-sizeZ 0 do | ||
+ | < | ||
+ | #If terrain is void, add coordinates to list | ||
+ | I J GetTerrain eq0 if | ||
+ | < | ||
+ | endif | ||
+ | loop | ||
+ | loop | ||
+ | </ |