My code breaks Chrome. Why?

Post » Fri Jul 21, 2017 3:03 am

So I have this code in my game:
Image

What I think it should do: While the bool IsArmageddon is true, it should repeat those actions. In other words, it should wait 0.5 seconds, apply the impulse, subtract one from the variable, and repeat. Wait 0.5 seconds again, apply impulse again, etc until the variable hits 0.

What it does: Brutally murders Chrome. Chrome completely locks up as soon as that variable goes over 100.

So, I get that I'm doing something wrong. Probably. I just want to know WHY it's wrong. Is this like an endless loop kind of thing? Or is there a small chance that this is actually a bug?
B
10
S
3
Posts: 23
Reputation: 688

Post » Fri Jul 21, 2017 3:46 am

while loop won't be paused by action wait, it only pend a task for each action wait.
Thus, "it should wait 0.5 seconds, apply the impulse, subtract one from the variable, and repeat..." is not correct.
B
108
S
26
G
258
Posts: 4,430
Reputation: 145,179

Post » Fri Jul 21, 2017 3:49 am

Several things:
Wait does not block! This is important to understand.
While is very dangerous - that event will kill your game if that condition is not cleared quickly, which it is not.
Don't put your Armageddon toggles at the top level, place them right after you change the countdown, otherwise they constantly run, when they only need to run when the value actually changes.

Use the Timer behaviour to manage your wait cycle. When IsArmageddon is set to true, start your timer. In On timer, do your physics and countdown code, and check your flag. If the flag is still true, trigger the same timer.
ImageImageImage
B
71
S
21
G
220
Posts: 3,648
Reputation: 123,725

Post » Fri Jul 21, 2017 6:57 am

Thanks guys! I learn something new every day. Still a noob when it comes to loops.
B
10
S
3
Posts: 23
Reputation: 688


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest