Time Delta problems

For questions about using Classic.

» Sun Aug 29, 2010 4:10 pm

So I just learned about Time Delta the other day, and good thing too. I'm having some problems with it though D:

Basically, I've got some code that works like this:

"Every 100 milliseconds - Add 1 to Value"

"If value is 50 - Do this"
"If value is 100 - Do this"
"If value is 150 - Do this"

and so on.

How do I apply Time Delta to this code? I've tried, but certain things still aren't working right when I run the game using Unlimited FPS. Also, unless I misunderstood, Time Delta varies. This means the value will never actually be exactly 50,100, or 150, right?
B
254
S
32
G
13
Posts: 1,795
Reputation: 19,270

» Sun Aug 29, 2010 4:42 pm

Er... By nature if an event occurs every X ms it's timesynced to begin with. The problem with unlimited FPS is some computers might run the game at 100fps, some at 20fps, so on and so forth. You never know. So moving something by 50 pixels every step will vary wildly depending on how fast the game is rendering.

If you're doing things in steps of time anyway, you don't need to worry about it.

Maybe you could elaborate exactly what you're talking about, because what you've listed is the same as doing 100ms x 50, x 100, x 150 (5 seconds, 10 seconds, 15 seconds).

Why don't you just "do this every x ms" (every 5, 10, 15 seconds) ? Why bother adding to a counter?

EDIT: Missed this question:

[quote:10frcve3]Also, unless I misunderstood, Time Delta varies. This means the value will never actually be exactly 50,100, or 150, right?[/quote:10frcve3]

Sure it varies a little (or can a lot), but it ultimately depends on how much you're doing each step. If you load resources at random times during the game it'll sky rocket when you do, compared to if it's humming along just doing the same stuff every frame.

TimeDelta is usually a very small number. For example 0.0013. It's the time it takes your computer to perform 1 game loop. Even on a "slow" computer, it's usually a very small number (compared to whole numbers). Most computers can render a basic game at 60fps solidly with vsync on. Mine can run my sample projects upwards of 10,000 at times with it off. That's a really small number

I can't think of a reason you'd need to be using TimeDelta in a cumulative fashion like I think you're attempting (even though it's not in your sample you give). Construct does this for you by offering the "every X ms" event.

But again, unless you elaborate more, I can't even fathom what it is you are trying to accomplish. But to answer your question... if you are adding it cumulatively every gamestep... if you're casting it into an integer, it will eventually land on the whole number you're after. Yes. But if you happen to do something time consuming at that moment, it may skip over the number you are after. I'd encourage you to use greater than/equal to as opposed to just equal to.

But for more accurate time calculations, just use what construct offers.
B
3
S
1
G
2
Posts: 31
Reputation: 887

» Sun Aug 29, 2010 4:49 pm

Also, i think using the timer in such small amounts can be in-accurate..

I believe timedelta is used to make something happen x times per second, for example-

Always : Move sprite1 1 pixel left.
This will make sprite 1 move 100 pixels left per second if the game is running at 100FPS,

However with timedelta,

Always : Move sprite 1 pixel left * timedelta
Will move it 1 pixel left per second, no matter what FPS the game runs at. So to achieve the same effect you'd want to change it to-
Always : Move sprite 1 pixel left *timedelta *100

Not sure if this helps or is relevant, but i believe, if possible you want to use timedelta instead of using the timer.
B
2
S
2
G
5
Posts: 448
Reputation: 2,546

» Sun Aug 29, 2010 5:00 pm

Well basically, when my player dies, a value is constantly added to. When that value reaches certain numbers, things happen..like the screen fading to black or going to a different layout.

It works fine when I use V-Synced Mode, but everything happens waaaay too fast when I use Unlimited Mode. I read that if something like this happens (anything running too fast when you use Unlimited mode) then Time Delta should be applied..otherwise you'll have some problems on certain computers..but I guess this situation is different?
B
254
S
32
G
13
Posts: 1,795
Reputation: 19,270

» Sun Aug 29, 2010 5:06 pm

[quote="Tokinsom":1noi3qt7]Well basically, when my player dies, a value is constantly added to. When that value reaches certain numbers, things happen..like the screen fading to black or going to a different layout.

It works fine when I use V-Synced Mode, but everything happens waaaay too fast when I use Unlimited Mode. I read that if something like this happens (anything running too fast when you use Unlimited mode) then Time Delta should be applied..otherwise you'll have some problems on certain computers..but I guess this situation is different?[/quote:1noi3qt7]

So when your player dies you're trying to delay a little bit of time then fade the screen out or restart the level ?
B
3
S
1
G
2
Posts: 31
Reputation: 887

» Sun Aug 29, 2010 5:27 pm

[quote="catch22":3veh2g1m]So when your player dies you're trying to delay a little bit of time then fade the screen out or restart the level ?[/quote:3veh2g1m]

Yessir. I figured my above method was simple and effective enough, but I guess not D:
B
254
S
32
G
13
Posts: 1,795
Reputation: 19,270

» Sun Aug 29, 2010 5:35 pm

Okay, so what you'd want to do is when your player dies, store the time in a variable for just that.

For example, death_time. Then when death_time has a value to compare, if the difference between it and the current time exceeds your delay (5 seconds, for example), then do your function/action/magic.

I will provide a .cap later if you like, I gotta go right now though. Good luck
B
3
S
1
G
2
Posts: 31
Reputation: 887

» Sun Aug 29, 2010 6:23 pm

You might also want to read these threads:

viewtopic.php?f=3&t=6469

viewtopic.php?f=3&t=6227
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,289

» Sun Aug 29, 2010 8:13 pm

"Every 100 milliseconds - Add 1 to Value"

"If value is 50 - Do this"
"If value is 100 - Do this"
"If value is 150 - Do this"

So you would want to change this to -

Always - add 50 * Timedelta to value

"If value is 50 - Do this" Will take 1 second
"If value is 100 - Do this" Will take 2 seconds
"If value is 150 - Do this" Will take 3 seconds

If you halved that value (50 to 25) then the steps would take twice as long. Also note that timedelta uses decimal places, so it will be hard for it to land precisely on "50". So if you modify those variables to greater than/less than etc.

It seems rediculous at first, but it does get very comfortable quite quickly. Have a look through the second topic Tulamide linked to, it is one where he helped me come to terms with Timedelta.
B
9
S
2
G
4
Posts: 346
Reputation: 2,726

» Sun Aug 29, 2010 10:58 pm

It was my understanding that using "Every x milliseconds" will result in the same speed whatever computer it's run on, and whatever frame-rate it ran at, because it's already factoring in the time delta and will run based on actual time passed.
In which case, your first post should run fine.

It's when you use "Every x ticks" that the speed is affected by frame-rate and computer speed.

Correct me if I'm wrong, because all of my game is based on "Every x milliseconds".

Krush.
B
2
S
2
G
3
Posts: 406
Reputation: 2,062

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 0 guests