GameMonkey Script

GameMonkey Script Forums
It is currently Thu Jun 27, 2019 8:34 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Feb 18, 2004 3:35 pm 
Offline

Joined: Wed Feb 18, 2004 3:26 pm
Posts: 6
To make the sleep() function work correctly I used in my program the same part of the code that is in the gme example:
Code:
     Timer g_timer;
     g_timer.Init();

     float timeAcc = 0.0f;
     gmuint32 idt = 0;
     while(g_machine->Execute(idt))
     {
      //
      // update time
      //
      timeAcc += (g_timer.Tick() * 1000.0f);
      if(timeAcc > 1.0f)
      {
        idt = (gmuint32) floorf(timeAcc);
        timeAcc -= (float) idt;
      }
      else idt = 0;
     }

But I don't fully understand it. Can someone explain me how does it works? Thanks.
Claudio


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Feb 18, 2004 11:12 pm 
Offline

Joined: Mon Dec 15, 2003 1:38 pm
Posts: 708
There's actually two things going on in that bit of GME code. The Timer g_timer is for the implementation of TICK() binding. Because that timer is getting the windows millisecond counter already, the GME code uses it to update the virtual machine time. The timer however stores time in 'seconds' but the VM wants time in milliseconds as an integer, so the code multiplies by 1000 to convert back to milliseconds.

All you really care to do, to make sleep() and other time related machine bound functions work, is pass the delta time (time between two game frames) to the Execute() as an integer. Beta 1.1 has a 'minimal' example which has code that may make more sense as it is only doing one thing, even if it is locally performed around the Execute function. Your game or application may already have a MainLoop(int dt) or GetDeltaTime() function that provides the delta time already.

Code:
    int deltaTime = 0;
    int lastTime = timeGetTime();

    // Keep executing script while threads persist
    while(machine->Execute(deltaTime))
    {
      int curTime = timeGetTime();
      deltaTime = curTime - lastTime;
      lastTime = curTime;
    }


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Feb 19, 2004 9:20 am 
Offline

Joined: Wed Feb 18, 2004 3:26 pm
Posts: 6
Thank you for this reply, I have understood now!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group