Timedelta question

For questions about using Classic.

Post » Sun Apr 17, 2011 12:48 pm

I'm still trying to understand where to apply timedelta. Instead of having to apply it to so many things, can I just have an event which is "every millesecond" then have tons of events under that event as sub-events? Since the every [time] conditions are affected by timescale, can't I just put everything under an event like that?
B
134
S
65
G
16
Posts: 1,765
Reputation: 19,188

Post » Sun Apr 17, 2011 3:19 pm

I'm not quite sure what you mean.

The time scale is just one project-wide setting, you can't apply it to a few things but not to others. When you set the time scale to 0.5, everything moves half as fast.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Sun Apr 17, 2011 10:15 pm

I think he means timedelta. What he's proposing (I think), is instead of using timedelta in expressions, of putting the whole game in a every x milliseconds event. That could work in theory.. but then if you have a really fast framerate, the game will be stationary for many frames, instead of moving in smaller, incremental steps. You should do some experimenting to see how reliable the solution is, and how it affects smoothness of the game. However, if you do this, then change the timescale, the game framerate will just slow down. instead of smoothly slowing down while keeping framerate.
B
25
S
3
G
6
Posts: 1,197
Reputation: 5,620

Post » Mon Apr 18, 2011 4:52 am

[quote="alspal":22an2qeu]...can I just have an event which is "every millesecond" then have tons of events under that event as sub-events?[/quote:22an2qeu]
No, you can't!

I can't stress this statement enough. 'every x millisecond' is not a reliable way of programming time based! Its timeslice is dependent on the framerate and that is exactly what you try to avoid when using a delta time.
If your game runs at 60 fps, the shortest timeslice you get for 'every' is 1/60s, ~17ms. You could try to set the condition to 17ms, but if for whatever reason the game runs on another computer with 30 fps the shortest timeslice is 1/30 or ~34 ms. In result, the game will be half as fast as on the first computer, because the 'every'-event is still triggered on every tick...
Btw, the example above also shows, that you can't even set the actual values but an approximation (e.g. 17 instead of 16.666666666666...)

[quote="Ashley":22an2qeu]This is actually an interesting problem because the Every event can only trigger at most once per tick (like Always). So if you're running at 10fps, the Every event will only trigger at most every 100ms. This is kind of awkward because it can make things framerate dependent again, even in well-designed, framerate-independent games.[/quote:22an2qeu][size=85:22an2qeu]Source[/size:22an2qeu]

'Every x milliseconds' also triggers in the next possible tick, not at the exact time. For example, with 60 fps, the 20th tick is reached at 333.33333333...ms, the 19th at 316.6666666...ms. If you'd now have it run 'every 320 milliseconds' it will occur in the 20th tick, not in between tick 19 and 20. This isn't that bad when just, let's say, switching a light on and off, but it is crucial when constantly moving a sprite or fading a color, etc. It will get slowly out of time.

Here is a cap. Run it and watch for a while (half a minute or more). The two sprites are both set to repeatedly move 400 pixels per second. The one above uses timedelta, the one below uses 'every...' I think, seeing this is better than a thousand words.

TimeDeltaVSeveryXms.cap
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Mon Apr 18, 2011 6:57 am

Thanks so much.
B
134
S
65
G
16
Posts: 1,765
Reputation: 19,188

Post » Mon Apr 18, 2011 8:55 am

Good explanation, tulamide. Note Construct 2 uses seconds as its default unit of time, instead of milliseconds like 0.x. I think too many people assume since they type in a quantity of milliseconds it is accurate to the millisecond. It's only accurate to a tick, though, so I guess it is a little misleading... a good article on ticks and timing would be useful...
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Mon May 09, 2011 1:07 pm

So is it ok to do something Every 1 second? Or is it only bad to use small millisecond events?
B
134
S
65
G
16
Posts: 1,765
Reputation: 19,188

Post » Sat May 14, 2011 4:09 pm

It's not bad to use millisecond events. It only gets inaccurate if you're using a very small number of milliseconds, because your timed event might fall between ticks and won't get passed until the next tick.

90% of the time it's such a tiny discrepancy that it won't visibly affect your game at all. But as a general rule I usually don't use anything less than about 50 milliseconds, unless you know without a doubt that it's not going to be an issue.

Edit:
For things like smooth motion and fading sprites and such, as tulamide was saying, it's a good idea to use timeDelta, yes. For things like machine guns spawning bullets, using Every X millisecond is fine, and if you have a small number of milliseconds then it's not going to affect your game too much.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 4 guests