User Tools

Site Tools


crpl:crpltutorial:interactive:randomemitter

<-CW3 Home <-CRPL Home <-Interacive tutorials

Creating a flying emitter

In this tutorial I'll cover how to make a flying emitter that flies to a randomly chosen spot, deploys a load of creeper and then repeats the procedure until you end the map.

Prerequisites

Creating the script file

First you must access the custom map section from the front screen int the projects sector.

  1. Enter the desired name of the project
  2. Create the project

Press the edit map button to access all the map editing functions.

  1. First select the unit tab in the Map Editor interface.
  2. Then press the scripts button, it'll bring up the scripts UI.
  3. In the Scripts UI, first type the name of the script, my given name is MovingEmitter.
  4. Press the create button to create the script.
  5. The script will now show up in the Scripts menu, press the edit button next to the script to start editing. It's here you'll need to have some kind of text editor installed, please refer to this for more information.

Coding the script

Now when everything is set up, it's time to start scripting. At this point I hope you have went through the start page of the CRPL page, if not, do it now! It explains the basics of the language so you understands the fundamentals, but don't worry if you doesn't get everything there, I'll cover as much as possible here.

So right now, if you did everything right, and have a text editor set up, then you should have a mostly empty page with just some gibberish at the top, you can remove that part if you want or keep it, it doesn't really make a difference.

But where to start?

I will start with showing off the first two rows of the code:

$Speed:3
$Strength:50

In these two rows you have defined all the properties for the emitter, or not really, but all the values that you'd need to enter in manually! Also when defining numbers like that you'll allow changes to happen out of the code, isn't that fantastic?!

But ok, how does this work?

First off you need to understand the concept of variables, and now you'd that you had listened under your algebra lessons in school, but it's fine, I'm here to attempt to teach you what this means. A variable is the concept of “naming” values and then store them so you can use them later. You can also alter the values in variables rather freely and this makes it lots more easier to keep check on all the numbers you'll be handling while programming. Also this makes user input and calculations possible in your code.

So, how do i actually define a variable and then give it a value?

$Speed

The code above defines the variable named Speed, it's dead simple, just add a $ before a name that CAN'T be separated my a space, it has to be another symbol, and you'll have your variable defined, but for it to work you have to assign it something, let's say a number!

$Speed:3

There, now the variable have a name AND a number stored, the number of 3. Now the variable is initialized and ready to use, and I'll cover later how to change this outside of the script file.

So with that knowledge you can now see that with the two lines of code i wrote earlier, i defined two variables and then assigned them different numbers, and these variables are going to be used later on in the script.

The next part of the script is the following code:

if (GetQueuedMoveCount 0 eq)

OMG SO MUCH NEW STUFF!!

Yeah that line introduced much new stuff, but don't panic, I'll explain everything, and for persons who have coded before, this should be really easy to grasp.

The first thing you see here is the small little word if, and it's quite self explanatory what it means, it checks if something is true, but how?

That's where the things inside the parentheses comes in, depending if the things inside the parenthesis works together, then it's true what says in there and the small if says “Hey this seems correct, then i better start doing something!”

But how do we know what's written in there and how the if statement sees it's true, it's rather easy actually. What we simply do is put in two values and check if they are true. So we have the two values of 0 and the word GetQueuedMoveCount. 0 is quite self explanatory what it means, but the other stuff, what does it mean?

The GetqueuedMoveCount is a command that'll correspond to the number of current moves you have queued in the movement queue. What that tells you is whenever you create a move, it's stored in a queue and that command corresponds to the number of move commands currently in the queue.

The last little thing int that row is eq, and what it simply does is that it takes the two last values given in the core and says if they are the same or not. If they are the same, it'll give you the true value and if they're no the same then they'll give you the value false.

Those two values will then in turn tell the if statement if it should do anything or just stay there and look pretty.

So what happens when the if statements gets the true value, what defines what to happen?

The definition of the things that'll happen is everything below the if statement and the statement called endif, and all the code looks like this in the current script:

if (GetQueuedMoveCount 0 eq)
QueueMove(RandCoords <-Speed)

SetCreeper(CurrentCoords <-Strength)
endif

So we have a lot of new stuff, but from what is mentioned earlier and from the names of the commands, this should be rather easy to figure out what is means.

QueueMove(RandCoords <-Speed)

that simple row of line is what creates a movement command and moves it to a random location. In the parenthesis i entered two things, RandCoords and <-Speed.

RandCoords gives the move a random x coordinate and a random y coordinate and the <-Speed line gives the number of the Speed variable and sets it as the speed the emitter will move in.

The next row with something in it is:

SetCreeper(CurrentCoords <-Strength)

In that code you create creeper. To create creeper you have to give it coordinates to spawn at and the amount of creeper to spawn there. To give the coordinates i put the command CurrentCoords down and that'll give me the current coordinates of our emitter and after that i put down the value of our Strength variable.

The last row is:

endif

That row simply states that the code after this is not affected by the outcome of this earlier if statement.

That was all the code needed and here you have it with all the parts together:

$Speed:3
$Strength:50

if (GetQueuedMoveCount 0 eq)
QueueMove(RandCoords <-Speed)

SetCreeper(CurrentCoords <-Strength)
endif

And now to make the script actually effect a unit!

Attach the script to a unit

In the image above i show a very crucial part, the compiling of a script! Now when you have very nicely have created and saved your script, just press the button to compile and it'll be performed automatically but the client, no worries!

  1. Press this button to tell the maker to create and CRPL core and then press b wherever you'd like to place it.
  2. Double click on the CRPL core to select it and bring up its properties.
  3. Press the default button here and scroll down until you find the CustomEmitter and pick it, you'll now get the texture of an emitter attached you your CRPl core.
  4. Find the script you recently created and press add, it'll now add the script to the core.
  5. Select your script and then the two variables you defined in the script show up here, now you can change this units values very easily and without the need to get into the code again.
  6. Press this to apply any changes made to the values you have there.

This was all for me for this time, hope you enjoyed and learned something! :)

Extra

Only read this if you want some extra information.

If you declare a variable as floating point, so:

$Speed:3.0

Instead of:

$Speed:3

You can use floating point values in the game when choosing the variables! (#5 of the last image above)

crpl/crpltutorial/interactive/randomemitter.txt · Last modified: 2020/05/29 09:16 by Sanian