ID:141586
 
Code:
//WORLD
world
hub = "Hi1.Sample"
name = "Sample"
status = "<font color=green><b>SAMPLE</b></font>"
//END WORLD

mob // Define Mob

//BEGIN VARS
var
loop1
loop2
//END VARS

verb
//BASIC LOOP
Start_Basic_Loop()
if(!loop1)
for(loop1=1,loop1)
spawn(50)
usr<<"Basic Loop every 5 seconds..."
else
alert("Basic Loop in progress...")
End_Basic_Loop()
if(loop1) loop1 = 0 ; alert("Basic Loop not in progress...")
//END BASIC LOOP

//ADVANCED LOOP
Start_Advanced_Loop()
if(!loop2)
loop2 = 1
do
if(prob(20))
spawn(50)
usr << "<b>Advanced Loop random seconds. (5 This Time)</b>"
if(prob(20))
spawn(70)
usr << "<b>Advanced Loop random seconds. (7 This Time)</b>"
if(prob(20))
spawn(100)
usr << "<b>Advanced Loop random seconds. (10 This Time)</b>"
if(prob(20))
spawn(120)
usr << "<b>Advanced Loop random seconds. (12 This Time)</b>"
if(prob(20))
spawn(150)
usr << "<b>Advanced Loop random seconds. (15 This Time)</b>"
while(loop2)
else
alert("Advanced Loop in progress...")
End_Advanced_Loop()
if(loop2) loop2 = 0 ; alert("Advanced Loop not in progress...")


Problem description:

The game just crashes when I try to start either loop...please help, thanks!

-Hi1
You don't spawn in a loop like that. All spawn()ing does is say, "OK, keep going guys, I'll wait [arg] 1/10 seconds and execute what is below me". So in other words, you loop is iterating extremely fast, and freezing your game up before you could end it. Use sleep() instead, it will actually stop the loop.
In response to Jeff8500
Okay, thanks!
In addition to what Jeff pointed out, I think there's a logic issue you might not be seeing here. It's subtle so it's easy to miss.

if(prob(20))
spawn(50)
usr << "<b>Advanced Loop random seconds. (5 This Time)</b>"
if(prob(20))
spawn(70)
usr << "<b>Advanced Loop random seconds. (7 This Time)</b>"
if(prob(20))
spawn(100)
usr << "<b>Advanced Loop random seconds. (10 This Time)</b>"
if(prob(20))
spawn(120)
usr << "<b>Advanced Loop random seconds. (12 This Time)</b>"
if(prob(20))
spawn(150)
usr << "<b>Advanced Loop random seconds. (15 This Time)</b>"


I'm guessing you actually meant that to be a chain of if, else if, else if, else if, and else statements, so if one choice was picked the others wouldn't be. As it is if one of the choices is picked, the next if() still runs regardless.

It seems like what you really wanted was to pick at random from 5 choices. For that purpose, pick() probably would be a better choice. prob() is going to skew your results a bit. If this was a chain of if/else statements like so:

if(prob(20))         // 20%
...
else if(prob(20)) // 80% * 20%
...
else if(prob(20)) // 80% * 80% * 20%
...
else if(prob(20)) // 80% * 80% * 80% * 20%
...
else if(prob(20)) // 80% * 80% * 80% * 80% * 20%
...
// None of the above: 80% * 80% * 80% * 80% * 80%


...the chances of each part of the loop being picked would get lower and lower. Each prob() is separate from the others, so if the first prob(20) fails and you move onto the next if(), you effectively multiply the next prob's chance of success by 80%. The second prob(20) is really equivalent to starting out with prob(16), because that part of the loop has a 16% chance of ever running. By the end you end up with a little over 8%. And the chance of none of the choices being picked is about 33% (80% to the 5th power).

If you don't use pick(), the best way to do this is to do your random part once, like var/choice=rand(1,5), and check the value in each if() or use a switch() to choose between them.

Hope that helps you in your project.

Lummox JR