Game Development Community

Canceling a schedule after x number of repetitions

by Nmuta Jones · in Torque Game Engine · 11/04/2006 (1:17 pm) · 7 replies

I have a bridge that's rising slowly. After it rises for 20 increments, it needs to stop. Right now, it keeps rising forever.

Here's my code:

%thing= new InteriorInstance() {
      canSaveDynamicFields = "1";
      position = "1.87907 -121.427 98.425";
      rotation = "1 0 0 0";
      scale = "1 1 1";
      interiorFile = "~/data/interiors/bridge/bridge.dif";
      useGLLighting = "0";
      showTerrainInside = "0";
   };
	
%movecount=0; 
function moveshape(%obj,%amt) {

		if (%movecount > 20){
			cancel(%themove);
		} else {
		

	%bowt = %obj.getTransform();
	
	%bowx = getWord(%bowt,0);
	%bowy = getWord(%bowt,1);
	%bowz = getWord(%bowt,2);
	
	%newz = %bowz + %amt;
	%obj.setTransform(%bowx SPC %bowy SPC %newz SPC "0 0 1 0");

	%themove= schedule(10,0,"moveshape",%obj,%amt); //
	%movecount++;
	}

	
}

in game, in the console, I find the number of the bridge (1531 for example) and then I'm typing in:

moveshape(1531, 0.1);

and it moves up forever. The cancel is never called. Somehow I suppose %movecount should be some type of global variable but I don't know how to acheive that. Namespaces? Not sure.

#1
11/04/2006 (2:19 pm)
You are not passing %movecount around the schedule, and since it's local it will be 1 each time.

I would do it this way:
Change your canceling if statement to to:
if (%obj.movecount > 20){
%obj.movecount = 0;
cancel(%themove);

And..

%movecount++;

to

%obj.movecount++;

Edit: You also cannot define a local variable outside of a function. Local variables are in scope of a function only.
#2
11/04/2006 (2:22 pm)
To use a global variable, use the $ sign instead of the %, so a $movecount would be a global, but that would work only for one (!) bridge. You could store the variable inside of the InteriorInstance, but you would need to give it a name, just like this:

%thing= new InteriorInstance([b]TestBridge[/b]) {
      canSaveDynamicFields = "1";
      position = "1.87907 -121.427 98.425";
      rotation = "1 0 0 0";
      scale = "1 1 1";
      interiorFile = "~/data/interiors/bridge/bridge.dif";
      useGLLighting = "0";
      showTerrainInside = "0";
      [b]movecount = 0;[/b]
   };

Now you could access the bridge via TestBridge.movecount. Does this help you?
#3
11/04/2006 (2:34 pm)
Absolutely. That helps tremendously. Thanks.
#4
11/04/2006 (2:43 pm)
FYI, you also have the same scoping issue with your scheduled event id: %theMove.

You will never be able to cancel %themove in your cancel statement, since it's not populated. You should make it a dynamic field on your InteriorInstance as well.
#5
11/04/2006 (3:02 pm)
So essentially when you create a new instance of an "item" or an "interiorinstance" the name of that object becomes a global variable. ( for example: new InteriorInstance(TestBridge) )

So I can use TestBridge to access that object henceforth and all of its custom fields.

Does this mean, then, that new instances of shapes created in the mission (.mis) files can also be accessed this way?
#6
11/04/2006 (3:31 pm)
That's true Nmuta. object names are globals and can be used to access objects anywhere in script just like other globals. only difference is object names don't require a '$' symbol. so you have two choices for object references:

$Bridge = new InteriorInstance(Test Bridge)
{
...
};

$Bridge.accessor();

or

new InteriorInstance(TestBridge)
{
...
};
TestBridge.accessor();

you can use names and IDs interchangeably. I think I read a few times that it's a bit more efficient to use the actual ID rather than the name though.
#7
11/04/2006 (3:42 pm)
I would imagine that the first way:

$Bridge = new InteriorInstance(Test Bridge)

would be essentially creating two globals which would be redundant and unneccessary.

AT any rate, thanks to all, I got it working ingame now. YOu enter a trigger and the bridge raises. I have it stopping stops after 2000 milliseconds.

Thanks to all who help illuminate this. I ended up simply using TestBridge as my "global" with all of the neccessary populated custom fields and it's working great.