Game Development Community

Pulling a random frame and remembering it was used...

by Ecliptic · in Torque Game Builder · 01/03/2009 (3:04 pm) · 4 replies

What would be the best way to approach this? I am trying to get my game to pull a random frame from 0-30 and then store it so it isn't pulled twice. I don't want to create a simSet or anything too overly complicated unless I have to. Is there something set up in torque for this?

Thanks guys.
Dane

#1
01/03/2009 (3:52 pm)
If you're using 31 frames, then you can use BITS, anything more than 32 and you'll have to use a different approach.

function t2dSceneObject::getRandomFrame(%this)
{
    %frameIndex = getRandom(0, 30);
    while (%this.FrameMask & bit(%frameIndex) > 0)
    {
        %frameIndex = getRandom(0, 30);
    }
    
    %this.FrameMask |= bit(%frameIndex);
}

When you generate your random number, you check to see if it is in the bitmask and keep generating a number until it is free. Once you find that number, you add it to the mask.
#2
01/03/2009 (4:19 pm)
Thanks so much for the help, BITS sounds like it would be perfect for what I need. I greatly appreciate it =)

Thanks
Dane
#3
01/03/2009 (4:23 pm)
If you need greater than 5-bit bitwise operations, you can string multiples together into a string and do your comparisons on the string rather than direction into the bit() call.
#4
01/09/2009 (4:39 pm)
Say I wanted to turn frameIndex into a global variable that could then be used in multiple spots. So that way everytime I create an object that has the same frame count I can use frame = $FrameIndex;

Also using the function above, but I still get duplicates of certain frames. Right now I am storing the value in a global variable and incrementing it everytime something in my game happens which works in this scenerio, but was curious for future projects.

Thanks
Dane