Ladies and gentlemen: I proudly present:

for each(<varpointer> in <List>) grin

I know, using for each in the following example is useless, but i is required to draw everything properly, and its a testrun anyway(so i is NOT requird for a normal for each loop, just in this dump testcase :P)
input:
Code:
i = 0;
        for each(LMessage in LList)
        {
            draw_text(LMessage, 10, 10*i, vector(100, 100, 100));
            draw_text("huhu", 200, 10*i, vector(100, 100, 100));
            i++;
        }



output:
Code:
i = 0;
        LList.FEInit(LList); while(LList.FEHasMoreElements(LList) > 0)
       { LMessage = LList.FENextElement(LList);

            draw_text(LMessage, 10, 10*i, vector(100, 100, 100));
            draw_text("huhu", 200, 10*i, vector(100, 100, 100));
            i++;
        }




Okay, how does it work? There is a new Class called CForEachObject. This class implements 3 methods.

FEInit();
FEHasMoreElements();
FENextElement();

If you want your object to work with the for each loop, you have to derive it from CForEachObject and implement those 3 methods.
Here is an example implementation from my CList:

Code:
void* CList::FENextElement()
{
    FFECount ++;
    return((FItems)[FFECount-1]);
}


int CList::FEHasMoreElements()
{
    return(FFECount < FCount);
}

void CList::FEInit()
{
    FFECount = 0;
}



As you can see, my CList has a private var called FFECount. This var is used to count up. In FEInit i initialize it to 0. In HasMoreElements, i check if the FFECount is BELOW my FCount(which is the number of items in my list). And incrementing FFECount in NextElement to go on with the next one later.

If you use the same list in a nested for each (for what reason). It wont break the mechanic but keep in mind that only the inner loop runs properly and the outer one quit directly after the first run.

PS: oh and it seems that deriving classes from each others went a bit buggy in 0.4. i fixed it.

Greetings
Rackscha


Last edited by Rackscha; 02/01/12 19:31.

MY Website with news of my projects:
(for example my current
Muliplayer Bomberman,
GenesisPrecompiler for LiteC
and TileMaster, an easy to use Tile editor)
Sparetime-Development