Sign in to follow this  
Paul C Skertich

AddTimer function in AngelScript

Recommended Posts

I'm not sure how to go about this but I do know is that recompiling angelscript over and over again won't be benefictional al all.

 

For instance say inside AngelScript file I have a an function called addTime like so:

void onLevelStart() {
    addTimer("testTimer",5.0f,"funct1");
}
void funct1(string &in asTimer) {
     Message("Timer Worked");
}

in C++, the addTimer would compare the how many elapsed time has went since the game has begain or script has been executed. The Message function in C++ would just pop up a message box or something.

 

So recompiling the script in message loop is horrible and darn right nasty. What are the other ways I would go about recompiling or executing a script function without any worries?

Share this post


Link to post
Share on other sites

I love the feeling when you actually solve something on your own! What I was able to figure out was this:

 

The addTimer function would stuff inside a structure data it's name, time, and function to be called. Here's slight code and if you ever want to use it - go ahead.

AngelScript

void onStart() {
     addTimer("timer",5.0f,"timer_funct");
}

void timer_funct() {
    //--- DO whatever you want to do after timer has reached.
}
C++ code:

struct Timer {
 std::string Name;
 float time;
 std::string FunctionCallback;
 bool complete;

};

std::vector<Timer> timers;

unsigned long startTime;
unsigned long elapsedTime;

void startTimer() {
  startTime  = clock();  
}
float getElapsedTime() {
  return(((float) clock() - startTime) / CLOCKS_PER_SECOND);
}
void addTimer(std::string &name, float time, std::string &functcallback) {
    Timer time = { name, time, functcallback, false }; 
    timers.push_back(time);
}

//-- This is for constantly checking to see when timer is finished.
void checkTimers() {
     if(!Timers[0].complete) {
         
          if(getElapsedTime() >= Timers[0].time) {
               Timers[0].complete = true;
               asIScriptContext *ctx = engine->CreateContext(); //-- as said in the documentation it's better to have the original script context.

               std::string funcdecl = "void " + Timers[0].FunctionCallback + "()";
               asIScriptFunction *func = ctx->GetModule(your module name here")->GetFunctionByDecl(functdecl);
               if(funct != null) {
                   ctx->Prepare(func);
                   int r = ctx->Execute();
                   if(r == asEXECUTE_FINISHED) {

                   }
            }
      }
   }
}

the checkTimers function should be in the render loop and the startTimer() should be in the initialization of the game of course.

 

Some of the functions I tried to memorize may be a bit off on function calls. However, essentially what's it's doing is checking to see if the timer is reached elapse point then going back and calling a function declared inside the script file.

 

There's probably a better way of going about all this of course.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this