DaBooda Turbo v1.4 - Tutorial B |
Going Loopy (Counters) |
Andrew "DaBooda" Stickney |
August 08, 2004 |
Welcome to the introduction of a brand new class, the DBCounter class. This class is a full on advanced timer class, for it allows you to create multiple timers that have all kinds of options with them. Originally Ander's first asked me, how about setting up timers for every propertie, well I got to thinking about this, and for one consider this. For every sprite there is um about 100 different properties, now multiply that by 100 sprites.. well you get the picture... slowwwww... even if it was off, it would still have to check it. Well I wanted a timer that could be used for anything, so I programmed this class into the engine.
I will warn you though, these timers are kind of advanced, well maybe not advanced but thouroghly confusing at best. For I wanted a counter that could be used for anything within this engine, and I do hope I accomplished this, for I have tried these things in every situation, and besides from giving me great shortcuts in game programming, they are nice because you don't have to worry about them.
Lets get into this thing, I am not going to show much or any code, for the simple fact that timers are all theory, trust me. Well I will try to explain them the best I can. The code in the tutorial program is really there for showing them in work, but knowing the code isn't going to work, so lets get into this theory and stop making fun of my simple program with its graphics straight out of paintshop...lol
Counters Are What? Two Loop System? What? |
Ok, some explaination goes into 1 and 2 loop counters. What does this mean, well it simply means that the counters can be made up of two counters. A counter can cycle through one counter then onto the next then back to the first. The first and last sprites on the screen are 1 loop and the two middle ones are 2 loop. If this doesn't help take a look:
{1....4} |
{4...1} |
{11....255} |
These are all examples of one loop counters, basically the counter starts at the beginning and cycles to the end by the increment. When these get incremented is determined by the timer. Lets take a look at the first one, for it starts at 1 and goes to 4. Well lets say the incrment is 1 and the timer is 1. On every render pass the value of that timer will increment by 1. As soon as it above 4 it will go back to 1 if it is a looping timer.
{1....4}{3....2} |
{4...1}{2...3} |
{11....255}{254....12} |
These are examples of two loop counters, basically they are considered one counter except the values change direction or the min and max change. A two loop system can be anything for the min and max of both don't even have to relate, it doesn't matter. The only reason I changed the values of the second loop was to keep a value from repeating more than once. These counters will cycle through both and if it is looping it will return to the min of the first loop.
Values Do Seek The Max |
The value of the counter just has one goal and that is to seek the max of the loop it is in. It does not matter if the max is lower or higher than the value, for the value will go there by whatever increment you set it.
With this said there is one thing you should always stay clear of, that is when the value will not reach the max, for example my min is 0 and my max is 7, but my increment is 2. Does anyone see the problem here, for one it will go past 7 and become 8, then it will go back to 6... this is called an infinite loop and is not a good thing, even if it doesn't stop the program from responding it will most likely cause you some problems.
I am sorry but the seek method was the only one worth doing, for then you can set any min or max's you wish. Just be careful.
Max Loops and Looping? |
Ok, some may be wondering, what is the difference between max loops and looping. Well sometimes you want a counter to repeat a few times then stop, this is where max loops comes into play... you simply set up a number of repeats for the counter and it will quit for you. Now looping set to true, does just that it contiunously loops the counter. All the counters in this example are looping.
The Difference Between Turn On and ResetCounter? |
By default when a counter is added, it is off and its value is 0. This is so the programmer can turn them on when they are needing to use it. But the problem lies in the value for not all counters will start there. So this is where knowing the difference between these two commands is important.
Turn On, does simply just that, it turns it on, leaving the value where it is at, hence the problem. Turn on and Turn Off where designed really to suspend the timer or pause it. And leave it ready to be resumed later.
Reset Counter on the other hand, will turn it on, reset the max loops, and make the value equal to min 1 of the first loop. Therefore the first initial turn on of the counter you should use Reset Counter, and it is also handy to restart a counter that has run its maximum loops.
What Is Up With All These #$%^ing Returns? |
A counter is pretty much useless without returning some kind of value to use, well I feel that I gave you enough return types to most likely handle anything. I will list them all and explain them to the best of my ability.
.ReturnCounterDone |
This return will only return true at one time, and that is when the max loops have been reached and the value is at max2 of a two loop or max1 of a one loop. This is very handy for timing events to happen, and using the counters as a delay. One thing to keep in mind even if the counter is looping it will still return true at this one instance.
.ReturnCurrentLoop |
This and the next one are kind of confusing, what this one does is returns the current loop out of the max loops the counter is on. Not to be confused with the next one. This is also helpful for timing events, but you wish the counter to keep going.
.ReturnCurrentMethod |
This returns the current loop of the counter, as in loop 1 or loop 2. See what I mean by confusing. This just lets you know which part of the counter the value may be in.
.ReturnPositiveInteger .ReturnNegativeInteger .ReturnPositiveSingle .ReturnNegativeSingle |
As you may know this engine, requires two different data types for properties, well the counters all work in integer values. So therefore here you go, this will return whatever data type you wish. The Negative returns are there because I strongly discourage the use of negative mins or maxes for the simple fact they can make counters even more confusing and even confuse the seeking value some.
.ReturnDivision .ReturnMultiplicative |
If you take a look at the example program, you might have noticed that the third sprite actually uses the same timer to do alpha and scale. Well you know that these require different values, this is where these two returns come into play. This way you can get two different values from one counter... just takes a little math on your part, but hey we are programmers aren't we, we are not scared of a little math!
QM's Make Life Alot Easier. |
Well easier on your typing fingers anyways, notice I use QM's to quickly set up the counters, I would suggest this to anyone. Also there are two QM's not shown in this program, but will be later in another one. But they are the FlipMinMax QM's. What these do is interchange the two or swap them if you will. The use for this will become apparent in the next tutorial. Also there is one that will transfer the value of on counter to another, handy, well maybe, I have found no use for it yet, but it is there.
Well that is it for the Counter Tutorial, and I do know that these can and are thoughorly confusing, they even confuse me. But with a little work, they will make game programming a sinch, and if you do not feel the same as me, let me know, tell me what we can do to improve them. And for any other questions please email me at masterbooda@yahoo.com . Please experiment and change values, this will help alot.