People keep asking about a wait equivalent in C++

The hard way is to work with threads, but if you didn't know this yet you better just forget it again. For (simple) tasks found in gameplay that's not needed. A better way is to either call your function every frame from lite-c and to decide inside C++ code what to execute when. This decision can be based on a var (Cpp function was called N times?), on the engine timer (2 seconds passed since last execution?) or a condition (is the object in line of sight?). Or a more advanced way, especially if you want to write more code than some snippets in Cpp is to use a 'scheduler'. This scheduler is a singleton class inside your dll, to which tasks are added which then get executed at a requested time.

Code:
class ITask
{
public:
	virtual void onUpdate() = 0;
};

class Scheduler
{
public:
	static void addTask( ITask* task )
	{
		mTasks.push_back( task );
	}

	static void update()
	{
		TaskContainer::iterator itr, end = mTasks.end();

		for( itr = mTasks.begin(); itr != end; ++itr )
			(*itr)->onUpdate();
	}

private:
	typedef std::vector< ITask* > TaskContainer;
	static TaskContainer mTasks;
};

DLLFUNC void scheduler_update()
{
	Scheduler::update();
}


Instead of using a singleton class here I just made the methods static. Also, tasks are executed just every frame

Now, just derivate whatever from ITask and call scheduler_update() every frame.

Code:
class MoveTask : public ITask
{
public:
	MoveTask( ENTITY* entity )
		:	mEntity( entity )
	{
	}

	void onUpdate()
	{
		c_move( mEntity, ... );
	}

protected:
	ENTITY* mEntity;
};

...
Scheduler::addTask( new MoveTask( myEntityInTheWorld ) );



which corresponds to

Code:
while(1) {
	c_move( Entity, ... );
	wait(1);
}



Last edited by TWO; 07/27/10 13:04.