foreach has had a minor upgrade and works like this:
syntax:
foreach in {object}[,{objects..}] do {codeblock} {filters};
Every object that matches the filter criteria will be used as the 'code block' is run. each objects ID is set into $for_id and that is how you reference the specific object in the code block.
All of these variables are set for each object:
$for_id, $for_link, $for_class, $for_name, $for_extra, $for_info, $for_code, $for_host
(If you need others, just let me know.. there is currently no way of choosing what you retrieve from the objects you find.. currenty.
The basics are covered in the coding manual so I will cover the complexes:
foreach in 0 do {codeblock} {filter}
this will loop through everything in cow as no location was set.
foreach in 0 do {codeblock} {filter} where (sql where cause)
This is just the where bit of the query but you can get quite complex..
it extends the existing filters to:
foreach in $loc do printit not layer not doorway where (worth > 100);
will find all objects in the current location which are not player or objects and are worth moe than 100 pennies.
foreach in 0 do {codeblock} join {sql join clause} where {sql where clause}
This is by far the most complex thing you can do.. as you are linking the objects table back to itself.
In this special case, the first table is aliased as 'o' and the linked table is aliased as 'l' (thats a lower case L if you cant tell)
The links always have to be between an o.{field} and a l.{field} like this:
link (l.host = o.obj_id) // the linked objects host will be the object we find
link (l.owner =o.obj_id) // the linked objects owner is the object we find.
link (l.loc = o.obj_id) // to find all objects contains the ones we are looking for
If your feeling adventerous, you could link the object table in a really bizzar way.. (l.host = 0.link) this would find all doors with hosted objects for you to do some comparison against.
read the anatomy of an object to get an idea of what you can link to what.. imagin finding all objects that have the same colour as each other (o.colour = l.colour)
You can not use the standard 'player' 'not player' filters as they are not aware of the aliases and would not know if you want the object or the linked object to be a player.. so you _have_ to use a where clause when using a link clause.
When using the where clause with a link clause you MUST use the aliases to indicate which obejct you are referring to eg:
where (o.class='player' and l.class='pouch') / will find all player objects who have a pouch object inside them
link (o.host = l.obj_id) where (l.material like '%living%') // find all object hosted by all objects that are material living.
To extend this further.. lts find all objects wilded by another object in the current location:
link (o.host = l.obj_id) where (o.hosthow like '%wield%' and l.loc = $loc);
Maybe we want to go further and find those with health less than half its maxhealth:
link (o.host = l.obj_id) where (o.hosthow like '%wield%' and l.loc = $loc and l.health < l.healthmax/2);
Learn how to do SQL queries and you can do anything you want here. google: [mysql query tutorial select]
Now, plase consider this: you can only process 250 blocks of cosscript in one hit before cow gives up worried that it might be in an infiante loop. this is generally fine.. and doing a DEBUG of any command wil show the final mount of bocks process if you need to know.
There is a little override you can do if you need to process 251 block or more, simply $max_subs = 251; and cow will use this as its block count limit for that specific run.
So a cowscript code that knows its going to exceed the limit, for a small one-off type thing, can overide the limit without effecting everything else.