Page 2 of 2

Re: Get values of other pawns through simkin command?

Posted: Sat Aug 02, 2008 12:25 pm
by QuestOfDreams
Juutis wrote:
Graywolf wrote:Those "for each in" commands are very convenient.
Could you explain a little what this command does and how to use it? I read about it in the Simkin docs but couldn't quite understand it. The example they have doesn't really help... :?
I'll try to explain: If you have a list/array of items you can loop through its elements either by starting with the first index and go on until you reach a max. index, e.g. simply an array of numbers

Code: Select all

for itemindex=0 to 10
{
    // you may use 'itemindex' to access an element in your itemlist
    itemlist[itemindex] = itemlist[itemindex] + 1;
}
or you can loop over all elements by using

Code: Select all

for each item in itemlist
{
    // 'item' is an element of your itemlist
    item = item + 1;
}
Using the 'for each in' method you don't need to know the maximum index of your list and you automatically get the next element in your itemlist in each iteration.

Re: Get values of other pawns through simkin command?

Posted: Sat Aug 02, 2008 12:30 pm
by Juutis
Oh, now I get it. Thanks QoD! :)

Re: Get values of other pawns through simkin command?

Posted: Sat Aug 02, 2008 11:19 pm
by Graywolf
There is also a built-in field for trees(arrays), called "numChildren", which gives you just that.

Code: Select all

{

	Array
	{
		thing1 [0]
		thing2 [1]
		thing3 [2]
		thing4 [3]
	}
		
	LoopThroughArray[()
	{
		for index = 0 to Array.numChildren - 1
		{
			debug(Array[index]);
		}
	}

}
In this case numChildren = 4. We have to subtract 1, because the array indices start at zero.