Rik's Treehouse > Babbling in Binary > My Software > Orphanware > R2DToo > R2DToo Help > For the Programmer > forEachAgent

forEachAgent

Declaration
Parameters
Remarks
Revisions
Example
See Also

Declaration

void __stdcall forEachAgent(
	bool (*func)(pState,int,int,pState*,int,int)	// name of function
);
Top of page

Parameters

func
The name of a function to apply to each agent in turn. (See Remarks.)
Top of page

Remarks

This function is available through the API. It provides an efficient way to process every agent in turn.

func

func is the name of a function with the following declaration:
bool func(
	pState state,  	// array of agent state variables
	int x,        	// X-coordinate of agent
	int y,        	// Y-coordinate of agent
	pState *nbr,   	// array of neighbours
	int nbrCount, 	// size of nbr array, nbr[0..nbrCount-1]
	int feaMode   	// calling mode, one of feaBegin, feaContinue or feaEnd
);
The parameters param, state, x, y, nbr, and nbrCount are passed into the function with the appropriate values to enable processing of each agent. The function is called first with parameter feaMode=feaBegin (and an arbitrary agent), then once for each agent with feaMode=feaContinue and finally, once more with feaMode=feaEnd (and the last agent again). See
forEachAgentVarfor an example of how to use feaMode.

The function must return a boolean value. The return value is ignored when feaMode is not feaContinue. When feaMode=feaContinue a return value of true indicates that the next agent in the sequence should be processed, false tells forEachAgent to prematurely abort processing of the agents. In this case, func will only be called one more time with the same agent and feaMode=feaEnd.

Top of page

Revisions

API v1.5
  • no longer passes param to func
Top of page

Example

bool stage1(pState state, int x, int y, pState *nbr, int nbrCount, int feaMode) // count live neighbours (called in onTick) { if (feaMode!=feaContinue) return true; // store # live neighbours in state[1] state[1] = 0; for (int i=0; i<nbrCount; i++) state[1] += nbr[i][0]; return true; } bool stage2(pState state, int x, int y, pState *nbr, int nbrCount, int feaMode) // update alive/dead state (called in onTick) { if (feaMode!=feaContinue) return true; // Conway's game of Life if (state[1] == 3) state[0] = 1; else if (state[1] != 2) state[0] = 0; // else no change (count==2) return true; } void __stdcall onTick(double &time) { forEachAgent(stage1); // count live neighbours forEachAgent(stage2); // update alive/dead state } Top of page

See Also

forEachAgentVar, onTick.
Top of page
[Rik's Office Hours] [Contact Rik]
Last updated: Fri Apr 30 2004, 1:37pm