I am trying to create a timer that calls a function when the timer ends
this is the timer class

class CreateTimer{
    float OverallLapsed;
    float EndOfTimer;
    bool Running;
    public:
    CreateTimer::CreateTimer(float);
    void StartTimer();
    void StopTimer();
    void Lapsed(float);
    void SetEndOfTimer(float);
    bool CheckTimer();
    void ResetOverallLapsed();
    float GetOverallLapsed();
};
CreateTimer::CreateTimer(float EndTime){
    EndOfTimer = EndTime;
    OverallLapsed = 0;
    Running = FALSE;
}
void CreateTimer::StartTimer(){Running = TRUE;}
void CreateTimer::StopTimer(){Running = FALSE;}
void CreateTimer::Lapsed(float Lapse){OverallLapsed += Lapse;}
void CreateTimer::SetEndOfTimer(float endtime){EndOfTimer = endtime;}
bool CreateTimer::CheckTimer(){
    if (Running){
        if (OverallLapsed >= EndOfTimer){return TRUE; OverallLapsed = 0.0f;}
        else{return FALSE;}
    }
    else{return FALSE;}
}
float CreateTimer::GetOverallLapsed(){return OverallLapsed;}
void CreateTimer::ResetOverallLapsed(){OverallLapsed = 0.0f;}

I want to be able to give the function in the constructor

CreateTimer::CreateTimer(float,CALLBACK);

and I would like to call the function inside the CheckTimer function.
I know you can do that because I have used a timer that did that before but I cannot figure out how to.

Recommended Answers

All 7 Replies

Just use a pointer to a function for the callback:

#include <iostream>

class foo {
  int _i;
  int _limit;
  void (*_action)();
public:
  foo ( int limit, void (*action)() )
    : _i ( 0 ), _limit ( limit ), _action ( action )
  {}

  bool done();
};

bool foo::done()
{
  if ( _i++ < _limit )
    return false;

  _action();

  return true;
}

void my_action()
{
  std::cout<<"boop!\n";
}

int main()
{
  foo bar ( 10, my_action );

  while ( !bar.done() )
    std::cout<<"Working...\n";
}

I don't completely understand some of the syntax you used
this is what I don't understand

foo ( int limit, void (*action)() )
: _i ( 0 ), _limit ( limit ), _action ( action )
{}

could you or someone explain what this means

It's called an initialization list. The code is roughly equivalent to this:

foo ( int limit, void (*action)() )
{
  _i = 0;
  _limit = limit;
  _action = action;
}

ok everything compiles but it does not call the function it is supposed to.
here is the modified code

class CreateTimer{
    float EndOfTimer;
    float OverallLapsed;
    bool Running;
    void (*CallBack)();

    public:
    CreateTimer::CreateTimer(float _EndOfTimer, void (*_CallBack)());
    void StartTimer();
    void StopTimer();
    void Lapsed(float);
    bool CheckTimer();
    void SetEndOfTimer(float);
};
CreateTimer::CreateTimer(float _EndOfTimer,void (*_CallBack)()){
    CallBack = _CallBack;
    EndOfTimer = _EndOfTimer;
    Running = FALSE;
}
void CreateTimer::StartTimer(){Running = TRUE;}
void CreateTimer::StopTimer(){Running = FALSE;}
void CreateTimer::Lapsed(float Lapse){OverallLapsed += Lapse;}
bool CreateTimer::CheckTimer(){
    if (Running){
        if (OverallLapsed < EndOfTimer){return FALSE;}
        else{CallBack(); OverallLapsed = 0.0f; return TRUE;}
    }
    else{return FALSE;}
}
void CreateTimer::SetEndOfTimer(float _EndOfTimer){EndOfTimer = _EndOfTimer;}

I assume you call CheckTimer in a loop somewhere right?

yes of course its inside the main game loop. if you want me to post my int main() code just tell me

ok I figured it out the time elapsed was a lot smaller value than I thought
thank you for all your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.