Can't be the only one with this problem...?
by Mads Laumann · 01/24/2007 (8:51 pm) · 7 comments
It's been a while since my last post, but it has been christmas and I have been on vecation to the Domanican Republic for 14 days :)
I have had this problem for some time now, but haven't been able to fix it my self. I have created and posted in different threads, but I still haven't been able to fix the problem... :(
Threads:
- www.garagegames.com/mg/forums/result.thread.php?qt=54975
- garagegames.com/mg/forums/result.thread.php?qt=52682
(I have posted in this thread)
To make a long story short, the problem is that my game runs at different speed, depending on the CPU of the users PC. I have been looking at the TankBuster source code, to get inspired to how to design the game, codewise. Can't find the link to the source code anymore, so i can't give it to you :(
But my current code/scripts is in the first forum link. It's the private forum.
To sum it all up
What I have found out so far is that the my problem started because I was updating all my entities in the 'onUpdateScene' function. Like they do on the TankBuster game.
I was then told that I shouldn't update my game here, because this function is called as many times at the users computer can handle. Ok, that was my guess too. Next problem, where do I update my game then? Because I really wanna do it this way by have some kind of "GameLoop", because I am use to do it like this when I make games in C#.
In C# i typically have a "StopWatch" and all my updating logic in a 'if'. If X miliseconds haven't passed since the last time it just skips the if, and then don't update the game.
But to get back to Torque. I couldn't update it on the 'onUpdateScene', so I was told to try doing it with a timer. I thought that sounded like a great idea. I should beable to called a %this.setTimerOn(25); method and a OnTick(%this) (or a name simular to this ;) ) function/event should be fired.
When I tryed this out, I found out the you can't set a timer on a t2dSceneObject.
So what to do now? Im stock here, and need some adwise, hope someone can help me out here. I can't be the only one who wants my game to run at the same speed at different PC's :P
PS: I haven't been working on this game lately, because I been working on a little C# project i wanna use at my work :) blog.laumania.net - Project Timer it is called.
I have had this problem for some time now, but haven't been able to fix it my self. I have created and posted in different threads, but I still haven't been able to fix the problem... :(
Threads:
- www.garagegames.com/mg/forums/result.thread.php?qt=54975
- garagegames.com/mg/forums/result.thread.php?qt=52682
(I have posted in this thread)
To make a long story short, the problem is that my game runs at different speed, depending on the CPU of the users PC. I have been looking at the TankBuster source code, to get inspired to how to design the game, codewise. Can't find the link to the source code anymore, so i can't give it to you :(
But my current code/scripts is in the first forum link. It's the private forum.
To sum it all up
What I have found out so far is that the my problem started because I was updating all my entities in the 'onUpdateScene' function. Like they do on the TankBuster game.
Code removed
I was then told that I shouldn't update my game here, because this function is called as many times at the users computer can handle. Ok, that was my guess too. Next problem, where do I update my game then? Because I really wanna do it this way by have some kind of "GameLoop", because I am use to do it like this when I make games in C#.
In C# i typically have a "StopWatch" and all my updating logic in a 'if'. If X miliseconds haven't passed since the last time it just skips the if, and then don't update the game.
But to get back to Torque. I couldn't update it on the 'onUpdateScene', so I was told to try doing it with a timer. I thought that sounded like a great idea. I should beable to called a %this.setTimerOn(25); method and a OnTick(%this) (or a name simular to this ;) ) function/event should be fired.
When I tryed this out, I found out the you can't set a timer on a t2dSceneObject.
So what to do now? Im stock here, and need some adwise, hope someone can help me out here. I can't be the only one who wants my game to run at the same speed at different PC's :P
PS: I haven't been working on this game lately, because I been working on a little C# project i wanna use at my work :) blog.laumania.net - Project Timer it is called.
About the author
Recent Blogs
• AstroMania (alpha)• Web Monitor
• Ninja Nick - Part 1
• MCTS, Torque and C++
• Parachute - Part 1
#2
01/24/2007 (9:44 pm)
Thanks for your quick answer Phil. That was the idea i had my self, my problem is to get this time in code :)
#3
EDIT: Also should you be posting TGB code into a public blog post? I think that's against the EULA
01/25/2007 (6:17 pm)
I update my game logic a bit differently. I set up a function called performGameLogic. The function schedules itself every 100 MS. In the function all of my game objects (stored in an array) check to see if they have any actions to perform. Each object knows how much time needs to elapse before it needs to do something, and it knows when the last time it had a state change was... Doing it this way makes sure everything is kept in perfect sequence. I used to do this differntly. Each object that needed state changes would schedule itself to change states after the state time had elapsed. This lead to things slowly moving out of sequence with each other.EDIT: Also should you be posting TGB code into a public blog post? I think that's against the EULA
#4
You can perform your time logic with that in your onUpdateScene method, or you can schedule an event with a timer using the below block;
01/25/2007 (6:51 pm)
@Mads, there is a console method called "GetRealTime()" which gets the current computers time in milliseconds, also for a smaller value to work with, there is a GetSimTime() (might be called something similiar) which gets the time in milliseconds since the engine initialized, so the values will be much smaller then GetRealTime)You can perform your time logic with that in your onUpdateScene method, or you can schedule an event with a timer using the below block;
new t2dSceneObject(myObject)
{
field1 = 0;
}
myObject.setTimerOn(300);
function myObject::onTimer(%this)
{
%this.field1 = %this.field1 + 1;
echo("myObject.field1 = " @ %this.field1);
}
#5
About the code, didn't thought of that one. I have removed the link for the .zip file with code, so the code is only download able from the private forum. But isn't the EULA only about that you cant show off the source code of the engine?
@David
Thanks, using the code in the code block would force me to set timers for all my object individually... I don't like that :)
But the GetSimTim() looks great, Ill try that out.
01/25/2007 (9:34 pm)
@Chris. Ok that a way of doing it too, but as Im used to the other one, im trying to aim for that and see if it's doable in Torque.About the code, didn't thought of that one. I have removed the link for the .zip file with code, so the code is only download able from the private forum. But isn't the EULA only about that you cant show off the source code of the engine?
@David
Thanks, using the code in the code block would force me to set timers for all my object individually... I don't like that :)
But the GetSimTim() looks great, Ill try that out.
#6
David's method works, but it wont properly sequence events between multiple objects.
01/25/2007 (9:45 pm)
Mads - The EULA for TGE says that, but TGB you cant show your script source either. My method and the updateScene method are the exact same... Just use Sim::Time to determine if you need to trigger an even in your objects, and use the elpased tiem since the last call to perform time sensitive activities like physics calculations.David's method works, but it wont properly sequence events between multiple objects.
#7
I tryed out the getSimTime() yesterday and it actually seemed to work as it should :) Ofcause it gave me some other problems, but yeah, that's coding! :)
Thanks guys!
01/26/2007 (12:16 pm)
@Chris - The code has been removed. I tryed out the getSimTime() yesterday and it actually seemed to work as it should :) Ofcause it gave me some other problems, but yeah, that's coding! :)
Thanks guys!
Torque Owner Phil Carlisle
What I'd recommend, is calling that and storing the value, then every update loop, you check if new time > last time + time step.. (timestep being a value you choose to lock your update rate at), if > then you update all game objects, otherwise you simply return, next time through the loop you check UNTIL you get a value > and it then calls the update.
If your game is running too slow, what you can do is see how much time has elapsed and if > update frequency, you simply update and loop until it isnt.. Basically setting a fixed time step.
One alternative, there are others (like multiplying positions and velocities * time elapsed).