OnUpdateScene() is called multiple times in one frame?
by shadowcode · in Torque Game Builder · 07/05/2010 (9:54 am) · 7 replies
Hi! I have defined a onUpdateScene() function for my game scene-graph.
In onUpdateScene() I calculate the time since last frame like this:
%sceneTime = %this.getSceneTime();
$LEVEL::timeSinceLastFrame = %sceneTime - $LEVEL::lastSceneTime;
$LEVEL::lastSceneTime = %sceneTime;
echo("Time since lastframe:" SPC $LEVEL::timeSinceLastFrame ); // print time since last frame
$LEVEL::timeSinceLastFrame and $LEVEL::lastSceneTime are initialized at start with 0.
And now comes that makes me nervous. In the console stands:
Time since lastframe: 0.032001
Time since lastframe: 0
Time since lastframe: 0
Time since lastframe: 0
Time since lastframe: 0
Time since lastframe: 0.031999
Time since lastframe: 0
Time since lastframe: 0
Time since lastframe: 0.032001
Time since lastframe: 0.031999
Time since lastframe: 0
Time since lastframe: 0
Time since lastframe: 0.032001
Time since lastframe: 0
Time since lastframe: 0
Time since lastframe: 0
Time since lastframe: 0
How could that be? I don't call OnUpdateScene. And I thought onUpdateScene() is called after every render frame? Did I oversee something?
regards
In onUpdateScene() I calculate the time since last frame like this:
%sceneTime = %this.getSceneTime();
$LEVEL::timeSinceLastFrame = %sceneTime - $LEVEL::lastSceneTime;
$LEVEL::lastSceneTime = %sceneTime;
echo("Time since lastframe:" SPC $LEVEL::timeSinceLastFrame ); // print time since last frame
$LEVEL::timeSinceLastFrame and $LEVEL::lastSceneTime are initialized at start with 0.
And now comes that makes me nervous. In the console stands:
Time since lastframe: 0.032001
Time since lastframe: 0
Time since lastframe: 0
Time since lastframe: 0
Time since lastframe: 0
Time since lastframe: 0.031999
Time since lastframe: 0
Time since lastframe: 0
Time since lastframe: 0.032001
Time since lastframe: 0.031999
Time since lastframe: 0
Time since lastframe: 0
Time since lastframe: 0.032001
Time since lastframe: 0
Time since lastframe: 0
Time since lastframe: 0
Time since lastframe: 0
How could that be? I don't call OnUpdateScene. And I thought onUpdateScene() is called after every render frame? Did I oversee something?
regards
About the author
#2
What are you using the time since last frame for? It might be that there is a better way to do what you are attempting to do than to use the onUpdateScene callback.
07/05/2010 (10:57 am)
Something else I just thought of:What are you using the time since last frame for? It might be that there is a better way to do what you are attempting to do than to use the onUpdateScene callback.
#3
Hmm.. I use the time since last frame to increase a game timer. The timer could be increased or decreased over the time by an object effect which the player can pick up. (Time-bonus or time-malus items). Hmmm.. If I thing about it again... Maybe it's a bit to much for a little game time counter. :S
07/05/2010 (11:25 am)
Ah, okay that make sense. Now I understand the console output. Hmm.. I use the time since last frame to increase a game timer. The timer could be increased or decreased over the time by an object effect which the player can pick up. (Time-bonus or time-malus items). Hmmm.. If I thing about it again... Maybe it's a bit to much for a little game time counter. :S
#4
For a game timer, I'll sometimes use the same code that you have there, but put it in schedule:
This function will only be called about every tenth of a second (instead of every frame). It's still called too often for the user to notice (and even if it is noticable, just change the 100 milliseconds to 50).
I used "<yourScene>" as the name of your game scene. When the scene ends, the timer will automatically stop. You can also manually stop the timer by using "cancel( $Level::GameTimerSchedule );".
07/05/2010 (7:21 pm)
It's okay for a game timer. You'll just need to realize that sometimes the time returned will be 0. The user will not notice that it goes up by 0.032 seconds.For a game timer, I'll sometimes use the same code that you have there, but put it in schedule:
// Somewhere in onSceneLoaded...
$LEVEL::lastSceneTime = %this.getSceneTime();
gameTimerTick();
function gameTimerTick()
{
%sceneTime = %this.getSceneTime();
$LEVEL::timeSinceLastFrame = %sceneTime - $LEVEL::lastSceneTime;
$LEVEL::lastSceneTime = %sceneTime;
// Do whatever with the scene time here...
$LEVEL::GameTimerSchedule =
schedule( 100, <yourScene>, gameTimerTick );
}This function will only be called about every tenth of a second (instead of every frame). It's still called too often for the user to notice (and even if it is noticable, just change the 100 milliseconds to 50).
I used "<yourScene>" as the name of your game scene. When the scene ends, the timer will automatically stop. You can also manually stop the timer by using "cancel( $Level::GameTimerSchedule );".
#5
First of all. How Do you add formatted source code in a forum Post?
Then I have one more question. :) I want move a GuiControl over the screen, at the moment I do it with a function which starts a schedule, which calls the function again until the GuiControl has reached the target position? Would you say this is a good approach or gives it better one?
07/06/2010 (6:47 am)
Thanks William.First of all. How Do you add formatted source code in a forum Post?
Then I have one more question. :) I want move a GuiControl over the screen, at the moment I do it with a function which starts a schedule, which calls the function again until the GuiControl has reached the target position? Would you say this is a good approach or gives it better one?
#6
I'd say that there are 3 valid approaches:
1) What you've already done.
2) I modify the source code to allow GuiControls to animate. I think there is a resource for this (www.torquepowered.com/community/blogs/view/14865), but it does more than I need it to.
3) Attach the GUI to a scene object and then use "moveTo()" to move the GUI. You can learn about the interface on TDN. (t2dSceneObject::attachGui)
07/06/2010 (10:41 am)
Next to the "Add Your Reply" is a link to show MarkupLite. Specifically for code, you begin with "(code)" and end with "(/code)", but change the "()" to "[]".I'd say that there are 3 valid approaches:
1) What you've already done.
2) I modify the source code to allow GuiControls to animate. I think there is a resource for this (www.torquepowered.com/community/blogs/view/14865), but it does more than I need it to.
3) Attach the GUI to a scene object and then use "moveTo()" to move the GUI. You can learn about the interface on TDN. (t2dSceneObject::attachGui)
#7
07/08/2010 (2:17 am)
Thanks William for your feedback. I will take care of it.
Associate William Lee Sims
Machine Code Games
onUpdateScene() gets called before each frame is rendered.
getSceneTime() returns the current scene time, but the value is only updated each tick (32 milliseconds, or 0.032 seconds).
You can use $Sim::Time (which I remember as returning the computer's time), but this won't account for scene speed-ups or pauses.