Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

Game Loop!


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 HeExists   Members   -  Reputation: 193

Like
0Likes
Like

Posted 01 November 2013 - 09:21 AM

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.



Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3290

Like
0Likes
Like

Posted 01 November 2013 - 01:25 PM

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

#3 HeExists   Members   -  Reputation: 193

Like
0Likes
Like

Posted 02 November 2013 - 02:37 AM

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.



#4 Andreas Jonsson   Moderators   -  Reputation: 3290

Like
0Likes
Like

Posted 02 November 2013 - 08:06 AM

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

#5 HeExists   Members   -  Reputation: 193

Like
0Likes
Like

Posted 02 November 2013 - 09:49 AM

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.



#6 Andreas Jonsson   Moderators   -  Reputation: 3290

Like
0Likes
Like

Posted 02 November 2013 - 11:02 AM

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

#7 HeExists   Members   -  Reputation: 193

Like
0Likes
Like

Posted 02 November 2013 - 11:32 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS