Tutorial 2 (Variables) - Aerll/rpp GitHub Wiki
Variables give you the ability to name any kind of value. In r++ values also have a type, which we need to specify when introducing a new variable. For example 1
has a type int
and 1.69
has a type float
. There are a couple more of them, but for now we are only interested in int
, as this is the type, which represents a tile.
For example:
int tile = 0;
Will introduce a new variable with a name tile
that holds a value 0
. After this line, you can use tile
instead of 0
.
Note: names can contain only the following characters:
[a-z]
,[A-Z]
,[0-9]
and:
. Additionally they can't start with a digit, and they can't be any of the keywords with a special meaning, for example:int
,float
,insert
etc.
Important thing to note, is that you can't create 2 variables with the same name.
The following code will throw an error:
int tile = 0;
int tile = 1;
Now, if we go back to the example from the previous tutorial, we can modify it to use a variable, like this:
#include "base.r"
#output "generic_unhookable.rules"
int tile = 0;
AutoMapper("Sweeper");
NewRun();
Insert(tile);
Seems like it's not really helpful, but trust me it is. Let's try to write something that makes a good use of it.
Let's say, that we want to write an automapper, which removes gold 1x1 unhooks. To do this we will use Replace
:
Replace(1, 0);
This will tell the automapper to replace all tiles 1
with an empty tile.
There are 8 tiles that we want to remove, therefore we need to write the following:
Replace(8, 0);
Replace(9, 0);
Replace(23, 0);
Replace(24, 0);
Replace(25, 0);
Replace(39, 0);
Replace(40, 0);
Replace(41, 0);
But wait a minute, what if we changed our mind? Let's say, that we no longer want it to remove gold 1x1 unhooks, but instead replace them with a silver tile. Normally we would need to manually change all 0
's to 1
's, which is of course completely doable in this case. However, imagine if this automapper was very complex, with hundreds of 0
's all over the place. Wouldn't be so easy anymore.
Well then, let's just name that value and use the variable instead:
int tile = 0;
Replace(8, tile);
Replace(9, tile);
Replace(23, tile);
Replace(24, tile);
Replace(25, tile);
Replace(39, tile);
Replace(40, tile);
Replace(41, tile);
Now if we change our mind, we only need to change the value of tile
.
Note: this example is meant to prove a point and it's definitely not the best way to go about making such automapper.
#include "base.r"
#output "generic_unhookable.rules"
AutoMapper("Gold Digger");
NewRun();
int tile = 0;
Replace(8, tile);
Replace(9, tile);
Replace(23, tile);
Replace(24, tile);
Replace(25, tile);
Replace(39, tile);
Replace(40, tile);
Replace(41, tile);