🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

checking state info

Started by
2 comments, last by frizb 24 years, 5 months ago
My player has data representing his state info which I am using to determine which animation sequence should be played. It is checked each loop. Since my player has about 20 different possible states I was looking for suggestions. Right now I am figuring on using a (switch) statement and breaking if I find the correct state. Is this the fastest way to do something like this or am I overlooking something? Still Learning...
Still Learning...
Advertisement
Switch statement is fine. if / else if structures are also ok, because then you can''t forget the break statements. Depending on your state structure you can do also use arrays of function pointers to do your evil with. That is if your state variable is always an int from 0 to 19 you can do:

typedef void (*function_pointer)(TObject *);

function_pointer animation_functions[20];
TObject * SpaceManSpiff;

int state = get_state(SpaceManSpiff);
if (state >= 0 && state < 20) {
(animation_functions[state])(SpaceManSpiff);
}

This really only saves computation time iff you call a function on your object in every branch of your switch statement.
Similar to the post SiCrane posted...

// function pointer
typedef void (*function_pointer)(TObject *);
function_pointer animation_function;

// functions
void doSomethingB(TObject *);
void doSomethingA(TObject *) {
// process TObject
if (state_change) {
function_pointer = doSomethingB;
}
}
void doSomethingB(TObject *) {
// process TObject
if (state_change) {
function_pointer = doSomethingA;
}
}



TObject * SpaceManSpiff;
animation_function(SpaceManSpiff);



Of course, you run the risk of function indirection overhead. I''m unsure if a switch case is more cpu intensive than a virtual function though...
Ok great. I''m not really calling any functions so I''ll stick with the switch statements for now. I''m just comparing states in different situations and adjusting accordingly. Thanks again guys!

Still Learning...
Still Learning...

This topic is closed to new replies.

Advertisement