AddTimer function in AngelScript

Started by
1 comment, last by jacmoe 9 years, 2 months ago

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?

Game Engine's WIP Videos - http://www.youtube.com/sicgames88
SIC Games @ GitHub - https://github.com/SICGames?tab=repositories
Simple D2D1 Font Wrapper for D3D11 - https://github.com/SICGames/D2DFontX
Advertisement

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.

Game Engine's WIP Videos - http://www.youtube.com/sicgames88
SIC Games @ GitHub - https://github.com/SICGames?tab=repositories
Simple D2D1 Font Wrapper for D3D11 - https://github.com/SICGames/D2DFontX

You could probably have posted in the Angelscript forum ;)

<edit>

Only 20 days late, but better than never, eh ?

</edit>

Too many projects; too much time

This topic is closed to new replies.

Advertisement