Displaying a variable's contents in the output window. (mostly)

Posted by MushclientNewbie on Sun 06 Jul 2003 03:51 AM — 8 posts, 31,689 views.

USA #0
Here is the script that I am trying to use right now. It doesn't work and I'd like you guys to take a look at it. I've had some experience scripting in Unix, but this has got me boggled. I'll post the code and explain what I need it to do.. The parts that I've left un-commented are part of a rolling script. It checks the 'roll' (the wildcard) against a number (51 in this case) and decides if it supposed to roll or stop rolling. I've got that part worked out. Now, I'll explain the problems I do have with the code.

sub OnRoll (name, line, wildcards)
   dim roll
   dim max 'I'm unsure about this... This is the variable that I'm trying to initalize.
            Do I need to initalize it in some special way?
      roll = CInt (wildcards (1))
      max = 0 'Here is an uncertianty that I brought over from unix. I want the
               variable to be reset to keep other 'max roll' values from carrying
               over. I'm not entirely sure how to do it in vb, though. My first
               problem.
 if roll < 51 then
   world.send "roll"
 else
   world.EnableTrigger "LblRoll", 0
   world.Sound "***some wav sound***"
           end if
 if roll > max then 'Here is where I check the max.
      max = roll 'Here, I want the max to be set equal to the roll if the roll is
                  greater. Keeping a running record of the maximum roll. I don't
                  think that it works like the variable stuff in unix so this is
                  my second problem.
      world.note "Max roll =", max 'Here is the note to the world. I can get the text
                                   into the output all right, I just can't figure
                                   out how to 'print' the variable like in unix. My
                                   third problem.
    else
      world.note "Max roll =", max 'Here it prints if the max isn't beat.
           end if
end sub


I think that my code is sound.. I just don't know what syntax I need. Trust me when I tell you all that I've looked around. All I can find, though, are twenty page examples or every single function I don't need. Go ahead and reply as you like, but try to give me exact solutions to my three problem spots. If you want to send me to some basic vbScript site that has what I'm looking for, that would be great too. Thanks, Matt.
Amended on Sun 06 Jul 2003 04:26 AM by MushclientNewbie
Australia Forum Administrator #1
Quote:

dim max 'I'm unsure about this... This is the variable that I'm trying to initalize.
Do I need to initalize it in some special way?



There are three ways you can maintain variables...

  1. Inside the sub - this will be initialised each time - I think - and won't be retained between calls. You don't need to explicitly declare it unless you have "option explicit" set.
  2. Outside the sub - this will retain its value between calls
  3. In a MUSHclient variable - this will retain its value between MUSHclient invocations (if you save the world file)


I suggest that if you are trying to remember the max roll the method you have chosen is the wrong one, especially if you are setting it to zero each time. I would at least put it outside the sub, and either initialise it statically, or have a trigger do it (eg. when it asks you your name).
eg.


dim max  ' declare outside a sub
max = 0  ' initialise it once

sub OnRoll (name, line, wildcards)
   
...

end sub


Quote:

max = 0 'Here is an uncertianty that I brought over from unix


Yes, that is how you set something to zero.

Quote:

max = roll


This is correct.

Quote:

world.note "Max roll =", max


You concatenate with "&", so do this:


world.note "Max roll = " & max






Amended on Sun 06 Jul 2003 04:38 AM by Nick Gammon
USA #2
Nick...

I love you! But, there is a problem with my code. When it runs, it just put the roll value into the max. Here's what I get:

Rolling stats for halfling ranger.:
Max: Str: 17  Int: 17  Wis: 21  Dex: 25  Con: 20
Cur: Str: 8   Int: 8   Wis: 8   Dex: 8   Con: 8 
[33] Cmds: help, end, roll, add, rem>  
roll
Max roll = 33

Rolling stats for halfling ranger.:
Max: Str: 17  Int: 17  Wis: 21  Dex: 25  Con: 20
Cur: Str: 8   Int: 8   Wis: 8   Dex: 8   Con: 8 
[27] Cmds: help, end, roll, add, rem>  
roll
Max roll = 27

Rolling stats for halfling ranger.:
Max: Str: 17  Int: 17  Wis: 21  Dex: 25  Con: 20
Cur: Str: 8   Int: 8   Wis: 8   Dex: 8   Con: 8 
[32] Cmds: help, end, roll, add, rem>  
roll
Max roll = 32


As far as I can tell, it should check to see if the variable in 'roll' is higher than the variable in 'max.' That's the second if statement. If it is higher, it should set the 'max' equal to the 'roll.' If it is lower, it should just print the max. Ugh, this is getting frustrating. I know this is simple stuff. The code I'm using is basically the same as what I've got in my first post. I just fixed it up a bit and added a subroutine that runs on a trigger when the mud connects, like you suggested. What am I missing?
Amended on Sun 06 Jul 2003 05:46 AM by MushclientNewbie
Canada #3
You set: max = 0 inside the subroutine. IE: Every single time you call the subroutine. Naturally, Max is always going to be lower than your roll.

To the fix the situation, put max = 0 outside the subroutine, probably immediately following the line where you Dim max, which should also be outside the subroutine. (You only want to clear it once per loading of MUSHclient).
Amended on Mon 07 Jul 2003 01:13 AM by Magnum
USA #4
I've taken care of that already. When the mud first connects I get a message about: "Abandon hope all ye who enter here..." I used that to trigger the dim max, max = 0 sub. There's something else wrong that I'm missing. Here's the complete 'script' file that I'm running right now. OnStart activates every time I connect and OnRoll activates every time the mud is commanded to roll.

sub OnStart (name, line, wildcards)
   dim max
   max = 0

world.note "Scripting enabled - script file processed"
End sub

sub OnRoll (name, line, wildcards)
   dim roll
      roll = CInt (wildcards (1))
 if roll < 51 then
   world.Send "roll"
    else
   world.EnableTrigger "LblRoll", 0
   world.Sound "C:\SIERRA\Half-Life\cstrike\sound\ambience\guit1.wav"
           end if
 if roll > max then
      max = roll
      world.note "Max roll = " & max
    else
      world.note "Max roll = " & max
           end if
End sub
Canada #5
You are not grasping the scope of variables. Because you put the Dim statement inside a subroutine, it is only retained in memory while that particular subroutine is being executed.

Instead, you want the Dim statement outside ALL subroutines.

dim max
max = 0
world.note "Scripting enabled - script file processed"

sub OnRoll (name, line, wildcards)
   dim roll
      roll = CInt (wildcards (1))
 if roll < 51 then
   world.Send "roll"
    else
   world.EnableTrigger "LblRoll", 0
   world.Sound "C:\SIERRA\Half-Life\cstrike\sound\ambience\guit1.wav"
           end if
 if roll > max then
      max = roll
      world.note "Max roll = " & max
    else
      world.note "Max roll = " & max
           end if
End sub

Delete the trigger that fires on "Abandon hope all ye who enter here...", you don't need it. Any script outside of a subroutine will be executed immediately when you load the script, which happens when you load the World File, or forcefully reinitialize the script from the MUSHclient menu's.
Amended on Mon 07 Jul 2003 03:07 AM by Magnum
Canada #6
Actually, you could keep that other trigger and do this too:

   dim max
   max = 0

sub OnStart (name, line, wildcards)
   max = 0
   world.note "Scripting enabled - script file processed"
End sub

sub OnRoll (name, line, wildcards)
   dim roll
      roll = CInt (wildcards (1))
 if roll < 51 then
   world.Send "roll"
    else
   world.EnableTrigger "LblRoll", 0
   world.Sound "C:\SIERRA\Half-Life\cstrike\sound\ambience\guit1.wav"
           end if
 if roll > max then
      max = roll
      world.note "Max roll = " & max
    else
      world.note "Max roll = " & max
           end if
End sub

...which would initialize max to 0, both when you open the world, and when you see that line you mentioned. (in case you disconnect then reconnect without closing and re-opening the world).
USA #7
Thanks!