Game Development Community

I have found how to get animation frames count

by Milan Rancic · in Torque Game Builder · 11/11/2009 (7:27 pm) · 4 replies

I have found how to get animation frames count. This can be very useful, since this thing can be used not just for animations.

I was looking for way to set my animation to the last frame, or at least to get somehow how many frames my animations have? (since i was using universal function that can be applied to many different animated objects) After a little experimenting, i found that all of the animations properties are exposed in Datablocks.cs file.

So, here is the function that returns last frame index in the desired animation that you have defined
[as the parameter pass the animation name from Datablocks.cs file: new t2dAnimationDatablock(animationName){} NOT the object name]
function getLastFrameIndex(%animName) {
	return getWordCount( %animName.animationFrames ) - 1;
}
I simply return number of t2dVector elements from animation definition in Datablocks.cs file.

Enjoy!

#1
11/11/2009 (7:51 pm)
That's quite useful!

If you do want to just set the last frame of an animated sprite, something to try would be to extend the t2dAnimatedSprite class in script. I've not tried it, but something like the following should work:
function t2dAnimatedSprite::setToLastFrame( %this )
{
  %animation = %this.getAnimationName();
  %lastFrameIndex = getLastFrameIndex( %animation );
  %this.setAnimationFrame( %lastFrameIndex );
}

Then you can do the following:
%myAnimatedSprite = new t2dAnimatedSprite() {...blah...};
%myAnimatedSprite.setToLastFrame();
#2
11/12/2009 (8:12 am)
That's right.
And this way you can reverse animation or do whatever you want.

I tried to setAnimationFrame(1000) hoping that it will automatically set it to the last frame, but instead TGB throws an error :( so you MUST use this method.

Hope this will be useful to someone.
Enjoy your coding ;)
#3
11/13/2009 (6:03 am)
This way it is possible to retrieve any information from any defined resource in managed/datablocks.cs file.

Now i wonder if i can change those values (or redefine them) and see if i can get the update in runtime ;)

For eg. my fire animation:
new t2dAnimationDatablock(Fire) {
   imageMap = "FireBitmap_256x4";
   animationFrames = "0 1 2 3";
   animationTime = "0.25";
   animationCycle = "0";
   randomStart = "0";
   startFrame = "0";
};
I'll try if i can dynamically add new frames to the animation, or change animation duration ;)
#4
11/13/2009 (6:19 am)
I'll be curious to hear the results. Especially for changing the animationTime.

I did notice that you'll need to call the (undocumented) function "calculateAnimation()" on the datablock after you change the frames or time. It looks like the animated sprite will pick up the changes after that, but I'd definitely like to know what you find.