Please login or register.

Login with username, password and session length
Advanced search  

News:

IRC channel - server: waelisch.de  channel: #wme (read more)

Author Topic: Loop speed  (Read 1233 times)

0 Members and 1 Guest are viewing this topic.

Dan Peach

  • Regular poster
  • ***
  • Karma: 4
  • Offline Offline
  • Posts: 100
    • View Profile
    • Viperante - Game Development
Loop speed
« on: May 12, 2015, 03:30:16 PM »

Hi,

Just a quick question so I can understand something better. I have a loop:

Code: WME Script
  1. method ScrollIn()
  2. {
  3.  RcDisable = true;
  4.  for(var i=1; i<=64; i=i+1)
  5.   {
  6.    this.Y = this.Y - 1;
  7.    Sleep(1);
  8.   }
  9.  InvScrolled = true;
  10.  RcDisable = false;
  11. }
  12.  

Looking at this, it seems my inventory should scroll 64 pixels in 64 milliseconds. But, it takes well over a full second to scroll. Am I not understanding it? Is this based on computer performance? I thought it was based on the engine's internal counter so it should always be the same speed on any computer...?

Thanks. :)

Mnemonic

  • WME developer
  • Administrator
  • Addicted to WME forum
  • *
  • Karma: 41
  • Offline Offline
  • Gender: Male
  • Posts: 5679
    • View Profile
    • Dead:Code Site
Re: Loop speed
« Reply #1 on: May 13, 2015, 08:19:32 AM »

Ok, let me explain how game rendering typically works - it runs in an endless loop that looks like this:
 
 - update the game state
 - render the result to screen
 - update the game state
 - render the result to screen
 ...
 
The frequency of those updates depends on hardware. If the game runs in fullscreen with vertical synchronization (vsync) enabled, it usually renders 60 frames per second (because the refresh rate of most LCD displays is 60Hz).
That means the game state (animations, scripts etc.) is updated 60 times per second (that's 16.67 milliseconds per update).

Soooo, if your script says "Sleep(1)", i.e. "wait for one millisecond", it has to wait for the next update, that is at least 16.67 milliseconds (if your game runs at 60 frames per second). In other words, using these tiny fractions of time while calling Sleep() is not a good idea. Change your logic to use larger time values (it's really not necessary to update the iventory position every millisecond anyway - even if the hardware was be able to display it, your eye wouldn't be able to see it :)).
Logged
Yes, I do have a twitter account
Please don't send me technical questions in private messages, use the forum. ::wave

Dan Peach

  • Regular poster
  • ***
  • Karma: 4
  • Offline Offline
  • Posts: 100
    • View Profile
    • Viperante - Game Development
Re: Loop speed
« Reply #2 on: May 13, 2015, 01:33:35 PM »

Hehe. Well, I knew you wouldn't be able to see it move so quickly. I was just using an extreme example. I forget why exactly. :P

Anyway - thanks man - this explanation is exactly what I was looking for. I understand it now. I didn't even think about the refresh rate of the screen. Learning is fun. :D
 

Page created in 0.144 seconds with 23 queries.