Sprite spawning at wrong time

Discussion and feedback on Construct 2

Post » Sat Dec 22, 2012 9:19 pm

I'm not sure what I'm doing wrong here, but I can't seem to get the sprite to spawn at the correct interval.

I have some events that add 0.1 to a global variable every 0.1 seconds. This lets me reset the timespan when a level restarts. At 0.3 seconds I spawn a new sprite, which works fine. But as soon as I pass 0.7 seconds mark, sprites start to spawn with a 0.1 second delay.


Sample Capx

Even stranger, while the 0.3 above appears to work if using >=, this does not (the sprite never spawns):


Sample Capx

Can anyone spot what I'm doing wrong? It seems this should be working
B
24
S
4
G
1
Posts: 244
Reputation: 3,462

Post » Sat Dec 22, 2012 9:51 pm

Computers cannot represent 0.1 exactly in binary, so a very close approximation a little less than 0.1 is used. That difference increases every time 0.1 is added.

One solution would be to replace the TimeSpan=0.8 condition with:
System Compare abs(TimeSpan-0.8) < 0.0001

Another would be to only add integers to TimeSpan and set the text to TimeSpan/10.

A final idea would be to instead of adding to TimeSpan every 0.1 sec, add dt to TimeSpan every tick and set the text to int(TimeSpan*10)/10.
B
79
S
24
G
54
Posts: 4,747
Reputation: 40,757

Post » Sat Dec 22, 2012 10:15 pm

Setting text to a number rounds the number (to 5 decimal places) which is why it looks like it should be working. Set your text to str(TimeSpan) and you'll see what's happening.
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Post » Sat Dec 22, 2012 10:37 pm

[QUOTE=ramones] Setting text to a number rounds the number (to 5 decimal places) which is why it looks like it should be working. Set your text to str(TimeSpan) and you'll see what's happening.[/QUOTE]

OH, that makes sense. I wasn't aware that it rounds it. That's a little annoying =/

@R0J0hound: I'll give the dt trick a shot. I can't remember exactly why I didn't do that, I remember running into some weird timing problems but I can't for the life of me remember what they were. I'll give it another try and see how it goes.
B
24
S
4
G
1
Posts: 244
Reputation: 3,462

Post » Sun Dec 23, 2012 3:36 am

My question is why are you changing the system time scale? and do you need to judge your time differnce as you are?

I suggest using something like this.
https://dl.dropbox.com/u/14087254/wierdtimebug.capx

also keep in mind when you have a constantly incrementing value try to compare with <= or >= not just =.
B
87
S
18
G
9
Posts: 2,455
Reputation: 14,834

Post » Sun Dec 23, 2012 4:43 am

@jayderyu: Changing the timescale was just so I could demonstrate the oddity I was seeing, without the numbers flying by in milliseconds. As for why I'm recording it to a variable, it's so that when I restart the level (when the player dies) I can reset it back to 0.

@R0J0hound: I remember the issue I had originally. It was that when I was using dt I was finding it hard to say "every 0.3" because it was rare for the dt to match - it would jump from "0.29897" to "0.31102" and my statement would miss. I thought that by just saying "every 0.1 seconds add 0.1 to my local (and reset-able) timespan" I got around that. I didn't realize it was having the exact same affect, but it was just rounding for me!

I think I've learnt a little bit more about how time works in games. I tells ya, I think understanding time and framerates is one of the trickier subjects to get your head around as an amateur game developer boolean2012-12-23 04:44:47
B
24
S
4
G
1
Posts: 244
Reputation: 3,462

Post » Sun Dec 23, 2012 10:32 am

About your dt issue, you can do an "every x second" this way:[code]+Every ticks
   -> add dt to timer
+timer >= x
   -> substract x from timer
   -> do things[/code]
Yann2012-12-23 10:32:32
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346


Return to Construct 2 General

Who is online

Users browsing this forum: neverk and 16 guests