Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
Rastamutti
Beginner


Joined: 27 Jun 2012
Posts: 17

PostPosted: Fri Aug 10, 2012 3:13 am   

Looping causing random freezeup
 
As far as I can tell this shouldn't cause this loop to randomly freeze so apparently I'm doing something wrong. It has now froze up
Cmud twice.

Code:

$numlimbs = %numitems(@jakariattacks)
#if ($numlimbs >= 2) {
  limb1 = %item(@jakariattacks,%random(1,$numlimbs))
  limb2 = %item(@jakariattacks,%random(1,$numlimbs))
  #until (@limb2 != @limb1) {limb2 = %item(@jakariattacks,%random(1,$numlimbs))}
}
Reply with quote
Daern
Sorcerer


Joined: 15 Apr 2011
Posts: 809

PostPosted: Fri Aug 10, 2012 4:03 am   
 
If it's freezing, it sounds like it's getting stuck in an infinite loop. I can't see why that would happen with this loop, but if all you're trying to do is get two different items from the list, I would do it like this:
Code:
$jakariattacks = @jakariattacks
$numlimbs = %numitems($jakariattacks)
#if ($numlimbs >= 2) {
  limb1 = %item($jakariattacks, %random(1, $numlimbs))
  #delitem $jakariattacks @limb1
  limb2 = %item($jakariattacks, %random(1, ($numlimbs - 1)))
  }

After the first random limb is chosen, it removes that one from the list, and selects another random limb from what's left.
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4692
Location: Pensacola, FL, USA

PostPosted: Fri Aug 10, 2012 10:55 am   
 
if you have a script with a big loop in it, its a good idea to stick '#WAIT 0' in there somewhere (preferably the start).
This will shunt it into a separate thread and perhaps not hang your system.
_________________
Discord: Shalimarwildcat
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Fri Aug 10, 2012 1:10 pm   
 
Is there ever a situation where @jakariattacks has only one item in it? That would produce an infinite loop because limb1 could never be different from limb2.
Reply with quote
Rastamutti
Beginner


Joined: 27 Jun 2012
Posts: 17

PostPosted: Fri Aug 10, 2012 2:32 pm   
 
Rahab wrote:
Is there ever a situation where @jakariattacks has only one item in it? That would produce an infinite loop because limb1 could never be different from limb2.


Yes, that's why it has the numlimbs check to make sure there are two or more.

Thanks, Daern. I will try this solution. Also taught me I can use local variables as string lists.. Didn't know that.
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net