"while x do y" crashing the simulator?

This could easily be a programming skill issue, but I'm clueless as to what I'm doing wrong. I want to do a simple "while the crank is being turned, shake the screen and play a funny noise", as a fun little thing, and the code I came up for it was this:

on crank do
  while event.ra!=0 do
    shake 0.05
    sound "beep"
  end
end

Of course it's not perfect, but it seems good enough. However, in practice, the result is touching the crank in any way results in the simulator webpage becoming unresponsive. Needless to say, this was not my intention. Am I doing something wrong? I tried to look for an "advance frame" function or something like it, thinking maybe the issue was it continuously shaking forever on the same frame, though I couldn't find this function. I tried adding a...

wait 0.05 then
 
end

...as an "advance frame"-like thing, but it didn't seem to do anything. Furthermore, I also tried

on draw do
 if event.ra!=0 then
  while event.ra!=0 do
  //shake & sound things again
  end
 end
end

yet again, to no avail.

A few things to note; It also crashes even when it is told to do absolutely nothing, so I find it hard to believe the shaking is making it crash, and also if the "while" condition is NOT fulfilled initially, it does NOT crash, so it's not an immediate crash upon using the "while" command as I suspected.

I was tempted to report it as a Pulp bug, but I find it more believable that I'm just not good at programming yet. Is it a Pulp bug or do I need to get good?

Edit: I have also tried this with conditions other than event.ra!=0. Provided the answer is "true", they always crash, without exception.

I think you need some kind of a timeout variable to prevent from calling the shake and sound events too frequently.

I build this little bit of code into the main game script and it appears to work as expected.

on load do
	playingSFX = 0
	sfxTimeout = 10 // set this value to something thats just longer than the sound fx
	sfxTimer = 0
end

on loop do
	if playingSFX==1 then
		if sfxTimer!=sfxTimeout then
			sfxTimer += 1
		else
			playingSFX = 0
		end
	else
	  if event.ra!=0 then
  		shake 0.25 // 0.05 is unnoticeable, but 0.25 - 0.5 feels right
  		sound "beep" // replace with your sounds name or Id
  		playingSFX = 1
  		sfxTimer = 0
  	end
	end
end

Didn't quite work for me; however, I did something like that that ended up...kind of working? The main problem I had is that whether you turn the crank a little or a lot, it'll shake the screen all the same. I guess that'll just have to wait for updates or something.

Code is very simple, it's just

on draw do
	//other stuff in the game, followed by
	if event.ra!=0 then
  		shake 0.5 
  		sound "beep" 
  	end
end

Thanks for the help anyways though!

This doesn’t seem like a bug.

In both approaches you’re effectively entering an interminable while loop. The crank event is only called when the crank moves so event.ra!=0 is always going to be true in that case. The draw event is called every frame (so 20 times per second). event.ra!=0 won’t always be true there but once it is your code will never exit that while loop.

I think what you want is to use the crank event handler (not draw) and your condition with a simple if instead of a while. I think Ryan is also right that you’ll want to gate that logic behind a manually maintained timer to avoid spamming the shake and sound functions.