I kinda wish nwnscript had a real implimentation of a foreach statement.
I think it might be achieveable with nwnx - but it would be very hacky.
Eg:
Create a list in memory
add object references to it
and each time you call a method like NWNX_GetNext("ContainerName") - it increments the index of the current selection - until it reaches the end of the list.
for(NWNX_GetNext("ContainerName") != FALSE){
object o = NWNX_GetCurrentObject("ContainerName");
//Do something
}
Of course - this could in theory be done in nwscript too- with use of Set/GetLocalObject
Just do a wrapper method around them, and make it return TRUE if the next item in the list is valid, FALSE if not - and then on a TRUE condition, set another LocalVar to be the actual object reference.
Its alot of work to get a simple - ForEach statement working.
It might be easier to use
for(i=0;i<=MAX_SIZE;i++)
{
}
The obvious benefits of a foreach is that it doesnt require you to know the amount of items in the list
With the SetLocal/GetLocal approach - you are going to sorta need to know that information :
If you are just checking to see if an object is not OBJECT_INVALID - then you will run into issues when object 5 out of 15 (a creature), dies and gets cleaned up by his body disappearing.
Object 5 becomes OBJECT_INVALID, and it would break the loop - preventing progression to objects 6-15 etc