[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Nested 'if' checks processing incorrectly?
Home  |  Users  |  Search  |  FAQ
Register forum user name
Forgotten password?

Nested 'if' checks processing incorrectly?

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Posted by Azeral777   (6 posts)  [Biography] bio
Date Sun 19 Dec 2004 08:12 AM (UTC)
A friend of mine had the desire to create a timer that, every five minutes, used the 'qmorph <name>' command to randomly change his description to one of fourteen designed choices. He came to me asking how it could be done, and I showed him a sample alias that had a 50% chance to smile and a 50% chance to grin. Based off of that and a little independant research, he came up with the code that follows, which is having problems.

He has a MUSHclient variable named 'number' that's blank by default, which stores the random number generated by the randomizer. When he runs the script (I made it into an alias and it's doing the exact same thing), it almost always chooses 'qmorph prinny', despite the 5% chance he assigned to it. Occasionally, it'll 'qmorph delibird', but the other options seem to be completely ignored. I watched the 'number' variable between commands and it is changing as it should, the script just seems to be getting stuck at prinny/delibird.

Anyway, without further ado, I present the script (yes, it's bad code, but it's one of his first attempts at it):

world.SetVariable "number", Rnd
If world.getvariable ("number") > "0.95" Then
world.Send "qmorph delibird"
Elseif world.getvariable ("number") > ".90" Then
world.Send "qmorph prinny"
Elseif world.getvariable ("number") > ".85" Then
world.Send "qmorph metro-taur"
Elseif world.getvariable ("number") > ".80" Then
world.Send "qmorph gamma"
Elseif world.getvariable ("number") > ".75" Then
world.Send "qmorph tundraswan"
Elseif world.getvariable ("number") > ".65" Then
world.Send "qmorph sinow"
Elseif world.getvariable ("number") > ".60" Then
world.Send "qmorph lerk"
Elseif world.getvariable ("number") > ".55" Then
world.Send "qmorph fade"
Elseif world.getvariable ("number") > "0.50" Then
world.Send "qmorph sawfly"
Elseif world.getvariable ("number") > "0.45" Then
world.Send "qmorph normal"
Elseif world.getvariable ("number") > "0.40" Then
world.Send "qmorph dragon"
Elseif world.getvariable ("number") > "0.35" Then
world.Send "qmorph blackswan"
world.Send "qmorph porpoise"
End If

Any idea what might be causing this to happen?
[Go to top] top

Posted by Flannel   USA  (1,230 posts)  [Biography] bio
Date Reply #1 on Sun 19 Dec 2004 09:04 AM (UTC)
You are using the numbers as strings.

0.75 is different than "0.75"
And another (once you fix that) possible source of error is the variable being considered a string, so it would compare
".50" > .50 and well, while it probably wont happen, VB has been known to do some funny things.

You use Cint to convert that variable to a number and make sure VB knows that it is.

May I also suggest you, instead of using getvariable a bunch of times, make it a script variable (at the beginning, which you can Cint once and be done with it) and then use that variable.
Reading MC variables takes a good amount of time (although its still basically instantaneous) so you should probably refrain from using it 20 times every five minutes.

Also, if you look at the random social plugin (check the plugin section of this site) youll find a plugin that sends a random social every X seconds. You can easily modify that to change his descriptiong (think of the qmorph command as a social).


Messiah of Rose
Eternity's Trials.

Clones are people two.
[Go to top] top

Posted by Azeral777   (6 posts)  [Biography] bio
Date Reply #2 on Sun 19 Dec 2004 09:32 AM (UTC)
Thanks for the help, I corrected the code with the majority of those suggestions and sent it back to him (after some thorough testing), it's working fine now. Being used to C++ and new to MUSHclient's functions and VB scripting myself, the answer just wasn't coming to me.

Again, thank you for your time.
[Go to top] top

Posted by Poromenos   Greece  (1,037 posts)  [Biography] bio
Date Reply #3 on Sun 19 Dec 2004 11:22 AM (UTC)
This is what I do:

Dim fso, s, ts, intLine, intCounter
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile("titles.txt", 1)
s = ts.ReadLine
intLine =Int((Int(s) - 1) * Rnd + 2)
For intCounter = 1 to intLine
    s = ts.ReadLine

Make a file called "titles.txt" and write all the titles you want in it and put the number of lines of the file on the top, like:
When you run this code, it will return a random title in the variable s.

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
[Go to top] top

Posted by Nick Gammon   Australia  (21,607 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Sun 19 Dec 2004 07:36 PM (UTC)

Amended on Sun 19 Dec 2004 07:38 PM (UTC) by Nick Gammon

And just to push the Lua barrel a bit more, you could do it in Lua like this, which allows for adding additional titles much more easily. Your method would require changing all your number breaks if you wanted even one additional one:

titles = {
  "delibird", "prinny", "metro-taur", "gamma",
  "tundraswan", "sinow", "lerk", "fade", "sawfly",
  "normal", "dragon", "blackswan", "porpoise",

which = (math.floor (MtRand () * table.getn (titles))) + 1

Send ("qmorph " .. titles [which])

This code here generates a random number in the correct range by finding how many items there are in the table, and multiplying the value from the MtRand function by that. MtRand returns a number 0 <= n < 1.

Since there are 13 items in the list above, that would be a number in the range 0 to 12, so we add 1 to get an index in the range 1 to 13 as Lua requires.

Then we simply index into the table.

In your VB example, it is unnecessary to set a MUSHclient variable in the first place. That is only overhead, and caused the problem you had to start with. Sticking to your own technique, simply use a VB variable, eg.

r = Rnd
If r > 0.95 Then
  world.Send "qmorph delibird"
... and so on ...

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum

[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]