Loops and Arrays
When I read here and there it seems that loops freak people out. They shouldn’t. Running a loop in your game is actually quite simple.
Actually they make things a lot simpler and can save you lots of time.
Let’s take for example an automapping feature (yes, like the one in Swords and Sorcery)
Automapping means that when a player visits an area, the game automatically uncovers that area in the game map. To do this, a variable (actually, an array) is used to say whether each point on the map has been seen or not.
Each time the player moves, a command in a script says that at the current location, the area has been seen by the player.
if (global.currentmap = 1) global.seenmap1 [global.posx, global.posy] = 1;
There is a line like the one above for each map in the game (there are 30) that is run each time the player moves forward (and changes location).
The array global.seenmap1 can be read as follows : Seenmap1 = 1 (is true) for the current value of global.posx and global.posy (the current location).
For map 2, I simply replace seenmap1 by seenmap2 and currentmap =1 by =2.
When the player types (M) to see the map, a loop is used to check for each combination of global.posx and global.posx that exists in the map and if it is seen, it draws the walls and terrain that exist on that spot.
The loop uses « for » and goes as follows :
if (global.currentmap = 1)
{
for (i=5; i<=21; i+=1)
{
for (j=5; j<=21; j+=1)
{
if (global.seenmap1 [i,j] = 1) overmapread();
}
}
}
i checks the pos.x coordinates. For i=5 it will check each value of j (being placed where it is in i,j it corresponds to the posy coordinate) between 5 and 21 by applying :
if (global.seenmap1 [i,j] = 1) overmapread().
If the if () condition is met, then the spot was seen and can be drawn. overmapread() checks what elements are on that spot to draw on the map (grass, forest, water, …) and draws them.
Then it will repeat for i=6, 7… and so on so as to check and accordingly draw each point that was seen on the map.
You’ve now seen how () work. Let’s look at how it’s structured.
for (i=5; i<=21; i+=1)
{
do this ;
do that ;
also do this while you’re at it.
}
It works a lot like the if statement does, only it needs a bit more information to work. It needs :
- a loop start value (i=5),
- when to end the loop (when i reaches 21)
- and how it gets from 5 to 21 (i+=1, meaning by adding 1 each time). If that was 5, it would apply the instructions in the brackets for i=5, then i=10, then i=15, then i=20.
You also should understand the workings of arrays. A two dimensional array :
global.seenmap1 [global.posx, global.posy] = 1
can be read as follows : global.seenmap1 for global.posx and global.posy = 1. A different way to say it then it was above but it says the same thing.
One dimensional arrays are even simpler. They are the same with just one argument.
Example : global.monsterstrength [1]=20 ;
This one is read as follows : the monster strength for monster #1 is equal to 20. Then you can use global.monsterstrength [1] in a function to help calculate the damage done to your character by monster #1 in a fight.
Other details :
I use posx, not x, because x is a built-in variable that always corresponds to the associated object’s location on a map. This doesn’t apply to Swords and Sorcery but I didn’t want to risk confusion so I used that instead (same goes for y).
In global.monsterstrength global means that the variable or array will be made available from any script or function in the game. The variable name without global means it will only be stored for the use of the current script.
Questions, comments or tutorials of your own ? Post or reply on the forum ! J

