Game Development Community

OnAllStopped event?

by David House · in Torque Game Builder · 12/04/2005 (6:30 am) · 2 replies

Do you think its possible to have an event fire when all the objects in the scene stop moving? Kinda like onPositionTarget, but just called once when everything has stopped. Or, if you can point me in the right direction I'll add it myself. For puzzle games, this kind of thing is very common and I think it would be very usefull. Like for example you are doing tetris and you want to know when the set of blocks has reached the bottom ( assuming that each block in the tetris shape is actually a sprite itself ).

Let me know if you need more clarification, not sure if I made sense there... :)

#1
12/04/2005 (8:34 am)
I would create a new source file t2dSceneGraphExtension.cc that looked like this:
#include "core/tVector.h"
#include "console/console.h"
#include "T2D/t2dSceneGraph.h"

ConsoleMethod(t2dSceneGraph, someoneMoving, bool, 2, 2, "")
{
    Vector<t2dSceneObject*> all_scene_objects;
    
    U32 object_count = object->getSceneObjectList(all_scene_objects);

    bool someone_moving = false;
    for(U32 i=0;i < object_count; ++i)
    if(!all_scene_objects[i]->getAtRest())
    {
      someone_moving = true;
      break;
    }

    return someone_moving;
}

And then add this script function:

function t2dSceneGraph::checkAllStoppedEvent()
{
    if( %this.someoneMoving() )
   {
       %this.mAllStoppedEventFired = false;
   }
   else
   {
      if( ! %this.mAllStoppedEventFired )
      {
          %this.mAllStoppedEventFired = true;
          %this.onAllStopped();
      }       
   }
}

This script function could then be called in t2dSceneGraph::onUpdateScene() and would call t2dSceneGraph::onAllStopped().
This way you have not modified the engine, just extended it. Depending on how accurate you want the event to be fired, you could call checkAllStoppedEvent() only like every 100 ms:

t2dSceneGraph::onUpdateScene(%this)
{
     if( t2dSceneGraph.getSceneTime() > $lastAllStoppedUpdate + 0.1 )
     {
                checkAllStoppedEvent();		
		$lastAllStoppedUpdate = t2dSceneGraph.getSceneTime();
     }

}

This would be accurate enouqh for a puzzle game.

-Michael
#2
12/04/2005 (2:00 pm)
Michael,

Thanks for the pointers. The more I think about it, I already have a list of all the pieces on the puzzle board, so I can just check them myself. The part I was missing is the .getAtRest() method. I think I'll put in a method to move a piece. In that method I'll set a timer that will check the pieces every 100ms or so. Then once they are all stopped, I'll fire the event myself. So no need to modify any other classes.

Thanks a bunch, this has set me on the right path. I'm trying to make my puzzle class just drop in and go, no major changes to any other classes. So far its looking like that will be a cinch!

I'll show some screenshots of this in action soon.

Thanks again!