Game Loop!

Started by
5 comments, last by BirdGB 10 years, 5 months ago

Hi,

I have to register a function which will loop continously to call the game update function from game loop. say -


void OnUpdate()
{
   // Update functions
}

Could someone kindly explain a little how to register it.

Thanks in advance.

Advertisement

Manual: Registering a function

For your function you would register it like this:

r = engine->RegisterGlobalFunction("void OnUpdate()", asFUNCTION(OnUpdate), asCALL_CDECL); assert( r >= 0 );

Regards,

Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

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.

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?

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

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.

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();
}
 

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Yes, I have 'main' which is called during initialization of the script class.

This will solve the problem. I was not sure about looping the function in the game loop.

Regards,

HeExists.

This topic is closed to new replies.

Advertisement