Sign in to follow this  
BirdaoGwraBasumatary

Game Loop!

Recommended Posts

Sorry for the short explanation. Actually I have already implemented AS which is working fine. What I want to know is - I want to register a GetFunctionByDecl, which will be used for the game loop. like the "void main()". Where all game update functions will be called. like

 

as:

void Update(float dt)
{
   object.setPosition(x,y,z);

   // Other calls

}

This function should be called continuously from the game loop to update the position of the object. How and where to register it?

 

Thanks for the reply.

Share this post


Link to post
Share on other sites
WitchLord    4677

I'm sorry. I'm still not clear on what you want to do.

 

Is your game loop implemented in C++ and is supposed to call a script function to update the game state? Or is your game loop implemented in script and is supposed to call a C++ function to update the game state?

 

Is the Update() function supposed to be implemented in script or C++? 

 

 

Perhaps you can give a little bit more detail on how you wish to set things up so we can provide more useful help to you?

Share this post


Link to post
Share on other sites

OK, Right now I have only 'void main()' in as script, from where I am calling the registered functions. But it is called only once during the initialization of the game.

 

After loading and initialing the game, I need to call some functions to update the object properties (like positions, rotations, moving objects, events etc) continuously, for which I need to register a script function like 'void main().

 

Example of a script will look something like this -

const Object @hero;
const Object @enemy;

float forward;

// init the game
void main()
{
   @hero = findObject("hero");
   @enemy = findObject("enemy");

   // some other functions

}

// update the game loop
void update(float dt)
{
   forward = forward + 1 * dt;
   hero.setPosition(0, 0, forward);

   // other functions

}

Anything inside 'void update()' should loop continuously to move the hero object. I am just a little bit confused, how to register the 'void update'.

 

Thanks.

Share this post


Link to post
Share on other sites
WitchLord    4677

So, I gather that the game loop will be managed by the application itself. 

 

You already have the application calling the 'main' function from the script, don't you? The application would call the 'update' function the exact same way from the game loop. 

 

 
// Application pseudo-code
void gameloop(asIScriptEngine *engine)
{
   asIScriptModule *scriptMod = engine->GetModule("game script");
   asIScriptFunction *updateFunc = scriptMod->GetFunctionByDecl("void update(float)");
   asIScriptContext *ctx = engine->CreateContext();
 
   for(;;)
   {
      // Call the script's 'update' function each loop
      ctx->Prepare(updateFunc);
      ctx->SetArgFloat(delta);
      int r = ctx->Execute();
      if( r != asEXECUTION_FINISHED )
      {
         // Something went wrong, check what it is, then exit
         break;
      }
   }
 
   ctx->Release();
}
 

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