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

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD Beta Forum
oldguy2
Wizard


Joined: 17 Jun 2006
Posts: 1201

PostPosted: Sat Nov 10, 2007 12:49 am   

Question about #while loops and #wait
 
Alright, with my limited knowledge of programming I would think that using a #while loop would be easier than a bunch of #alarms for the purpose I am using them. However, it doesn't work. Basically I want to perform a single-level control break right? So how do I do that because it won't change the variable while the loop is running and using #wait.

For example, in all of my curing for herbs I set an alarm to wait 2 seconds then try eating again as long as the herbbal variable does not equal 0 or 1. When I eat the herb it is changed to 0 and when the message is received I have herb balance it is set to 1.

However, trying to use a #while loop just will not work no matter what I do.

Like for example for an alias called dotest:

Code:
#if (@testvar=1) {
      perform action
      testvar=0.5
      #while (@testvar=0.5) {
          #wait 2000
          testvar=1
          dotest
          }
      }


Then I set two alarms and two triggers to fire off of those alarms. One fired in 3 seconds to #show Test in process and to set testvar=0 and another in 6 seconds to #show Test complete to set testvar=1. However, you can't change the testvar value no matter what. It is just stuck at testvar=0.5 even after the triggers fire. Of course it will change testvar=1 in the #while loop though. It just won't change it testvar=0 when "Test in process" fires like it is supposed to. Sorry I am such an idiot. I can't figure this out. This is probably slower than using alarms but I was just trying to figure it out anyway. [/code]
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Sat Nov 10, 2007 1:05 am   
 
I won't be able to check this until later tonight, but in general, it is a bad idea to perform equality tests on floating point numbers. Binary-based computers cannot represent a floating-point value *exactly*. So your variable probably isn't ever exactly equal to 0.5. It's probably really something like 0.49999999999 or something like that.

Try changing your script so that it is testing an integer value instead of a floating value, or see the %norm function for how to properly compare floating point values.

With the v2.x beta versions, using #WAIT within a #WHILE loop *should* work. It won't work properly in the 1.34 version, nor in zMUD.
Reply with quote
oldguy2
Wizard


Joined: 17 Jun 2006
Posts: 1201

PostPosted: Sat Nov 10, 2007 1:41 am   
 
Well even if you set it to 2 or something else, it still doesn't work and the variable won't change when the trigger fires.
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Sat Nov 10, 2007 1:53 am   
 
I could not replicate this with a few simple tests in 2.10. I will check again with some more complex tests in 2.11. Just probably not tonight.
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
oldguy2
Wizard


Joined: 17 Jun 2006
Posts: 1201

PostPosted: Sat Nov 10, 2007 2:15 am   
 
How did you get it to work?

This is what happens when I do it.

Performing test
Performing test
Performing test
Performing test
Performing test
[+4-> #show Test in process]
Test in process
[Test in process-> testvar=0]
Performing test
Performing test
[+8-> #show Test complete]
Test complete
[Test complete-> testvar=1]
Performing test
Performing test
Performing test
Performing test
Performing test
Threads:
# ID Window Name Status Script
-------------------------------------------------------------------------------------------------
8 [u] untitled wait 1500 ms dotest
11 [u] untitled running #thread
Performing test

Alias dotest:

Code:
#if (@testvar=1) {
  #show Performing test
  testvar=2
  #while (@testvar=2) {
    #wait 2000
    testvar=1
    dotest
    }
  }
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23379
Location: Colorado, USA

PostPosted: Sat Nov 10, 2007 2:53 am   
 
OK, I was going to try and test this, but there is something weird about your example.

You said that the code shown above is the Alias dotest? But you are calling dotest from with the #while loop. That's just going to cause an infinite loop, won't it?

In other words, as soon as you set "testvar=1" you are calling the alias again, which immediately passes the #if statement and then immediately sets "testvar=2" and then waits for 2 seconds. Thus, it never even gets to the end of the #while loop because you are calling "dotest" again at the end of the loop.

Also, I don't see your alarms defined anywhere. I know you mentioned them briefly in the first post, but you didn't show the exact code that you are using for your triggers and alarms. It's always better to show the *exact* code that you are using.

So can you please show the exact scripts that you are using? Go into the Settings editor and select the Untitled window (or whatever window you are using) and then go to the XML tab and copy/paste the XML into the forum post so that we can see your full scripts.
Reply with quote
oldguy2
Wizard


Joined: 17 Jun 2006
Posts: 1201

PostPosted: Sat Nov 10, 2007 3:14 am   
 
Oops. You are right. I took that out and it works fine. I was still thinking of how I had the alarm coded I guess.
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD Beta Forum 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