User Tools

Site Tools


This is an old revision of the document!

~~DISCUSSION~~ <!--Comments visible to editors only can be placed in HTML comments --> <-CW3 Home<- CRPL Home

Code Examples

Code examples contributed by map makers and coders alike. Feel free to submit your own - it will be much appreciated.

Manage Treasure Chests in Credits Mission

# Goodie.crpl
# Created on: 9/10/2013 8:36:17 PM
# ------------------------------------------


	GetCoresWithVar("name" "Player") pop ->player

if (Distance(CurrentCoords GetUnitAttribute(<-player CONST_COORDX) GetUnitAttribute(<-player CONST_COORDY)) lte(2))
	Destroy(Self 1)

	CreateUnit("CRPLCORE" CurrentCoords) ->fadingPoints
	AddScriptToUnit(<-fadingPoints "FadingPoints.crpl")
	SetScriptVar(<-fadingPoints "FadingPoints.crpl" "pointAmt" <-score)

Contributed by VirgilW

Image Spinner

Specify a rate and which image to rotate. Positive rotation rates are counterclockwise and the rotation rate is in radians per frame.


# Make a part spin around
Self <-imageToRotate GetImageRotation ->currentRotation
<-currentRotation <-rotationRate add ->currentRotation
Self <-imageToRotate <-currentRotation SetImageRotation

# This version is more terse and more efficient, but harder to read.
#Self <-imageToRotate Self <-imageToRotate GetImageRotation <-rotationRate add SetImageRotation

Contributed by VirgilW

Can be used for making a blinking light. Changes the transparency of an image from fully transparent to fully opaque in a cycle. The red, green, and blue values can be used to tint the color of the image.


	self <-name <-customImageName SetImage
	self <-name -0.01 SetImagePositionZ

<-active if
	<-alpha 0 lt if
		0 ->alpha
		<-rate abs ->rate
		<-alpha 255 gt if
			255 ->alpha
			<-rate abs -1 mul ->rate

	<-alpha <-rate add ->alpha

	self <-name <-red <-green <-blue <-alpha SetImageColor
	self <-name 0 0 0 0 SetImageColor

Contributed by VirgilW

Keep track of unit UIDs

You have some main unit which creates children units and you want to be able to keep track of these children: In this case, you get the UID when you create the other unit. For instance:

# Creates a bullet object
	"CRPLCore" CurrentX CurrentY CreateUnit ->unit
	<-unit "WBWBullet.crpl" AddScriptToUnit
	<-unit "WBWBullet.crpl" "targetX" <-targetX SetScriptVar
	<-unit "WBWBullet.crpl" "targetY" <-targetY SetScriptVar
	<-unit "main" "Custom2" SetImage
	<-unit "main" -0.01 SetImagePositionZ
	<-unit "main" self "barrel" GetImageRotation SetImageRotation

Contributed by VirgilW

Find player units matching specified criteria

You'd like to find player units that match some criteria: In this case, you can use the GetUnitsInRange function. For instance:

# Finds the nearest unit in range.
# Returns the unit uid of the closest unit, or 0 if no unit is in range
99999999 ->closestDistance
0 ->closestUnit
CurrentCoords <-range GetUnitsInRange ->unitCount
<-unitCount neq0 if
	<-unitCount 0 do
		CurrentCoords <-unit CONST_COORDX GetUnitAttribute <-unit CONST_COORDY GetUnitAttribute Distance ->d
		<-d <-closestDistance lt if
			<-d ->closestDistance
			<-unit ->closestUnit

The GetUnitsInRange function pushes all of the UIDs of units within the specified radius to the stack. Then, it pushes the count to the top of the stack. If there are no units in range, a single “0” is pushed to the stack. So, you can look at the top of the stack to know how many other things are on the stack, and you know that each of them if a unit UID. A “do” loop becomes useful for iterating through and popping the items from the stack. In the above example, I'm looking for the closest player unit. So, I just get all within range then run through them calculating the distance to each once. Along the way I remember the one that is closest. Note that I could have made a much more sophisticated comparison criteria. I could, for instance, have added the unit's ammo to the distance so that I selected based on a combination of closeness and how much ammo the unit had on board.

Contributed by VirgilW

Locate unit based on value of variable

You want to find the UID of some other CRPLCore based on the value of a variable: Take the following example:

"name" "Gun" GetCoresWithVar ->unitCount
<-unitCount if
   <-unitCount 0 do
      <-unit CONST_MAXHEALTH 10 SetUnitAttribute
      <-unit CONST_HEALTH 10 SetUnitAttribute

Here, we are looking for any CrplCore that has a script with a var called “name” that is set to a value of “Gun”. This allows us to add scripts to CrplCores, create a variable like this at the top of the script


, then get objects that have that string value. Since this is an input var in the script, it can be edited by the map author without having to touch the script. Of course the GetCoresWithVar also works with any variable, so you could use it to find Cores that have set some flag, etc.

Contributed by VirgilW

Moving creeper wall

# MoveWall.crpl
# Created on: 2/4/2013 8:00:55 PM
# Author: J
# This script creates a wall of creeper on the left side of the map that moves forward every x frames
# You may choose in-game if the wall should keep going or release the creeper at the end
# If RepeatAtEnd=2 the creeper is released at the right side of the map while a new (empty) wall will run over the map
# If you choose to enable KeepCreeperOut, it will try to keep all other creeper out of the wall
# Spacing is the number of cells the KeepCreeperOut will keep 100% clean of creeper 
# ------------------------------------------

	<-InitialDelay Delay false ->stop 0 ->stepx <-MoveDelay ->stept
	MapHeight 0 do 0 I <-InitialWallHeight SetCreeper loop

<-stop not if
	MapHeight 0 do
		#move creeper back from the left side
		<-stepx neq0 if
			<-stepx I <-stepx 1 sub I GetCreeper AddCreeper
			<-stepx 1 sub I 0 SetCreeper
		#move creeper back from the right side
		<-stepx MapWidth 1 sub neq if
			<-stepx I <-stepx 1 add I GetCreeper AddCreeper
			<-stepx 1 add I 0 SetCreeper
		<-KeepCreeperOut if
			#keep creeper out of the wall, left side
			<-stepx 3 gte if
				<-stepx 3 sub <-Spacing sub I <-stepx 2 sub <-Spacing sub I GetCreeper AddCreeper
				<-stepx 2 sub <-Spacing sub I 0 SetCreeper
			#keep creeper out of the wall, right side
			<-stepx <-Spacing add MapWidth 4 sub lte if
				<-stepx 3 add <-Spacing add I <-stepx 2 add <-Spacing add I GetCreeper AddCreeper
				<-stepx 2 add <-Spacing add I 0 SetCreeper
	<-stept 1 sub ->stept
	#move creeper forward if stept is 0
	<-stept eq0 if
		<-stepx MapWidth 1 sub eq if
			<-RepeatAtEnd eq0 if
				true ->stop
			<-RepeatAtEnd neq0 if
				<-MoveDelay ->stept
				0 ->stepx
				<-RepeatAtEnd 1 eq if
					MapHeight 0 do
					0 I MapWidth 1 sub I GetCreeper AddCreeper
					MapWidth 1 sub I 0 SetCreeper loop
			<-MoveDelay ->stept
			MapHeight 0 do
				#move the creeper in the wall forward
				<-stepx 1 add I <-stepx I GetCreeper AddCreeper
				<-stepx I 0 SetCreeper
				#move the creeper in front of the wall forward
				<-KeepCreeperOut if
					<-stepx <-Spacing add MapWidth 5 sub lte if
						<-stepx 4 add <-Spacing add I <-stepx 3 add <-Spacing add I GetCreeper AddCreeper
						<-stepx 3 add <-Spacing add I 0 SetCreeper
			<-stepx 1 add ->stepx

Contributed by J

Terrain Rail Walker

# RailWalker.crpl
# ------------------------------------------


	# Initialize our last position since we don't have one.
	-1 ->lastCellX 
	-1 ->lastCellY 

#If we aren't moving, choose a new location.  The location will be a neighboring cell.
GetQueuedMoveCount eq0 if
	@ChooseNewCell if
		<-chosenX <-chosenY <-SPEED QueueMove
		CurrentX ->lastCellX
		CurrentY ->lastCellY

	-1 ->chosenX
	-1 ->chosenY

	# Get a list of the neighbors that we can move to.
	# This call returns the coordinate pairs on the stack.

	# Choose a random location within the list
	0 <-count RandInt ->randCellNumber

	# Go through the list and pop all of the coordinates off the stack.
	# As we pass the coordinates that we chose in our random number above, remember them.
	# Those are the coordinates we will be returning.
	<-count 0 do
		I <-randCellNumber eq if
			<-x ->chosenX
			<-y ->chosenY
	# Return if we chose a new location
	<-chosenX -1 neq

	#Check the four neighboring cells to see if they are acceptable terrain heights.
	0 ->count
	CurrentX 1 add ->cx CurrentY ->cy @CheckCell		#Right
	CurrentX ->cx CurrentY 1 sub ->cy @CheckCell		#Up
	CurrentX 1 sub ->cx CurrentY ->cy @CheckCell		#Left
	CurrentX ->cx CurrentY 1 add ->cy @CheckCell		#Down

	# By default, we won't return the last cell coordinates.  This is so the patrolling unit
	# doesn't return back to where it came from immediately.  But, if the only choice is to return
	# to the previous cell, then that is what we have to do.
	<-count eq0 if 
		1 ->count

	#Check to see if the cell we are looking at is the last cell, if so ignore.
	<-cx <-lastCellX neq <-cy <-lastCellY neq or if
		# Check if the target cell is an acceptable terrain height.  If so, push the 
		# coordinates to the stack and increment count.
		<-cx <-cy GetTerrain ->terrainHeight
		<-terrainHeight <-MIN_TERRAIN_HEIGHT gte <-terrainHeight <-MAX_TERRAIN_HEIGHT lte and if
			<-count 1 add ->count

Contributed by virgilw

Circular move pattern

Sometimes you want a core to be moving in a circle around a point. Attach this script to make that happen. Change precision to improve movement or make it move counter-clockwise (high values). Remove all “asfloat” and both “0 Round” to make the circle one cell smaller downwards and to the right (4 cells as center instead of 1).

# moves the core in a circle around x and y with a set speed
# ------------------------------------------
	0.0 ->step
GetQueuedMoveCount eq0 if
	<-step cos <-size asfloat mul <-x asfloat add 0 Round <-step sin <-size asfloat mul <-y asfloat add 0 Round <-speed QueueMove
	<-step <-precision add ->step

Contributed by J

Connect totems to win

Here's are two scripts that when used, blow up all enemy structures when enough totems are connected. The first script is used to create totems on the map and stores their ID's, so they can be read by the second script. Script 1:

# Author: J
# ------------------------------------------
	"TOTEM" CurrentCoords CreateUnit ->totemUID
	"totem" ->unitName
	4 delay self 0 Destroy

Script 2:

# Author: J
# ------------------------------------------

	 2 delay
	 "unitName" "totem" GetCoresWithVar dup ->totemcount 0 do
		 <-scriptname "totemUID" getScriptVar
		 "totem" I concat ->!

0 ->activecount
<-totemcount 0 do
	 "totem" I concat <-! dup ->unitUID CONST_AMMO GetUnitAttribute
	 0 gt if
		 <-activecount 1 add ->activecount
		 <-unitUID CONST_AMMO <-unitUID CONST_AMMO GetUnitAttribute <-ammodrain sub SetUnitAttribute

# if all totems are active, blow up all enemy units (insta-win)
<-activecount <-totemsrequired gte if
	 0 0 99999 GetEnemyUnitsInRange 0 do
		 dup Self neq if
			 2 Destroy
	 Self 2 Destroy

ammodrain: how much ammo it drains from the totem, use values between 0 and 0.05. totemsrequired: how many totems you need to coonect to win, use a number between 1 and how many totems you put on the map with script 1. scriptname: how you named script 1.

Contributed by J

Unit Providing Script

This script allows the user to set a type of unit, number of units and energy cost to collect/power the core it's attached to.


if (not(GetUnitAttribute(Self CONST_ISBUILDING)))
	SetBuildLimit(<-unitToGive <-numToGive)
	Destroy(Self 1)

	if (GetUpdateCount eq (0))
		SetUnitAttribute(Self CONST_ISBUILDING TRUE)
		SetUnitAttribute(Self CONST_BUILDCOST <-buildCost)
		SetUnitAttribute(Self CONST_MAXHEALTH <-buildCost)
		SetUnitAttribute(Self CONST_HEALTH 0) 	
		SetUnitAttribute(Self CONST_CREATEPZ FALSE)

Contributed by VirgilW (posted by teknotiss)

Change Creeper or Anti-Creeper Colors

The following code would show how to make the creeper red at the start of a custom map, but keep the AC to whatever the player has set. (Untested… but it will work, right? :) )

   GetCreeperColors ->acB ->acG ->acR ->creeperB ->creeperG ->creeperR
   160 ->creeperR
   0 ->creeperG
   0 ->creeperB
   SetCreeperColors(<-creeperR <-creeperG <-creeperB <-acR <-acG <-acB)

Contributed by VirgilW

Wrap Creeper around the edges of the map

The following code will allow creeper to flow around the edges of the map. That makes the map feel like a globe. Best use on terrain, that is seamless.

Add this code to a tower. As long as this tower exists, the creeper will leave the map at the left and enter at right, and vise versa.

The parameters can be used to activate or deactivate left-right or top-bottom wrap.

# Author: knattertc
# ------------------------------------------

# Init Vars
MapWidth 1 sub ->right
0 ->left
0 ->top
MapHeight 1 sub ->bottom

# Wrap Left to Right
<-wrapLeftRight neq0 if
	<-bottom <-top do
		# Get Creeper on left and right side - add them up - devide by 2
		<-left I GetCreeper <-right I GetCreeper add 2 div ->mid 
		<-left I <-mid SetCreeper
		<-right I <-mid SetCreeper

# Wrap Top to Bottom
<-wrapTopBottom neq0 if
	<-left <-right do
 		# Get Creeper on top and bottom - add them up - devide by 2
		<-top I GetCreeper <-bottom I GetCreeper add 2 div ->mid 
		<-bottom I <-mid SetCreeper
		<-top I <-mid SetCreeper

Contributed by knattertc

crpl/crpltutorial/code_examples.1381485615.txt.gz · Last modified: 2014/10/01 15:02 (external edit)