Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ Counting and then sending commands upon a certain number

Counting and then sending commands upon a certain number

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


Posted by Archer   (2 posts)  Bio
Date Tue 11 Mar 2003 02:20 AM (UTC)
Message
I was trying to make a counter that will count how many times X happens and upon reaching 8 times it will send a command to the game and then reset the counter to start it all over again. I'm pretty new to scripting and so far this's what I've come up with but empty handidly. Any help would be much appreciated thanks.

sub oncount (name, line, wildcards)
dim coun
If world.getvariable("coun") < 8 then
world.getvariable("coun")
world.setvariable("coun"), coun + 1
else
world.send "say begin"
world.send "say begin"
world.send "say begin"
world.send "say begin"
world.send "say begin"
world.send "say begin"
world.send "say begin"
world.send "say begin"
world.send "hitall"
world.setvariable "coun", 0
end if
end sub
Top

Posted by Magnum   Canada  (580 posts)  Bio
Date Reply #1 on Tue 11 Mar 2003 12:33 PM (UTC)
Message
Well, to start, you seem to be mixing up the scope of variables. So... a lesson in variables:

All programming languages have methods for placing values into memory. You reserve a place in your computer's memory by declaring your intention and assigning the location a label. In Visual Basic Script, it is done like this:

Dim VariableName

Some programming languages allow you to declare a new variable on the fly, simply by assigning a value. For example:

MyVariable = "TestValue"

Now, programming languages usually also have different scopes for variables. This allowance is done so that software can be more efficient, and also to reduce conflicts. Variables (memory) can be allocated only when needed, then released when it is no longer required.

One example, is the Global or Public variable. This is a variable (memory location) that persists for the entire time that your program is running. In Visual Basic Script, there are two ways to declare a variable as Public:

Dim MyVariable

Sub MySubroutine
  MyVariable = "TestValue"
End Sub

Notice that the Dim statement is declared outside of any subroutine. Declaring a subroutine in the 'mainline' of your script means that any subroutine can interact with that variable.

Sub MySubroutine
  Public MyVariable
  MyVariable = "TestValue"
End Sub

This is the other way, which is to explicitly declare you want the variable to be global to your entire program, by using the Public directive instead of the Dim directive.

Now, the other type of variable is the procedural, local, Private variable. This type of variable is allocated inside a subroutine, and it only persists while the subroutine is running. In Visual Basic Script, this is accomplished like this:

Sub MySubroutine
  Dim MyVariable
  MyVariable = "TestValue"
End Sub

...or...

Sub MySubroutine
  Private MyVariable
  MyVariable = "TestValue"
End Sub

In these cases, the declaration of the variable reserves some memory, but only while your subroutine is executing. Once the 'End Sub' line is processed, any local values are tossed as the memory is released.

Ok... following all that? In terms of using VBS under MUSHclient, you should know that any Global/Public resources will remain in memory while your 'World File' is open. If you close your 'World File', Global/Public resources are released/forgotten.

For some people, this would be catastrophic. Fortunately, Nick, the author of MUSHclient, has provided an interface by which you can create a third level of variables. You (as a programmer) can tell MUSHclient to store variables for you. MUSHclient will save these variables to a file when you close the 'World File', so your values will still persist from session to session. In Visual Basic, this interaction is done like this:

World.SetVariable "MyVariable", "TestValue"
World.Note World.GetVariable("MyVariable")
World.DeleteVariable "MyVariable"

Notice that are not required to initialize (reserve a memory location for) the MUSHclient variable using a specific directive. Your MUSHclient variable is initialized on-the-fly when you assign the value.

For purposes of efficiency, I recommend you not use MUSHclient variables unless you explicitly require that the values be stored from session to session. You should be mindful of allocating resources carelessly. Efficiently written script will run faster.

Get my plugins here: http://www.magnumsworld.com/muds/

Constantly proving I don't know what I am doing...
Magnum.
Top

Posted by Magnum   Canada  (580 posts)  Bio
Date Reply #2 on Tue 11 Mar 2003 12:51 PM (UTC)
Message
Now, to specifically address your problem, I would probably use something like this:

Dim Counter
Counter = 0

Sub OnCount (Name, Line, Wildcards)
  Dim x
  Counter = Counter + 1
  If Counter => 8 Then
    For x = 1 to 8
      World.Send "say begin"
    Next
    World.Send "hitall"
    Counter = 0
  End If
End Sub

First, I declare the variable "Counter" outside of any subroutine, which means it will be a Global\Public variable which will remain in memory while the World is open. If you close the World, the variable will be forgotten, but I'm guessing that should be ok for this project. I also set the value to 0 immediately.

I assume this subroutine will be called by a trigger. When that happens, we increase the Counter right away. Next, we check if the Counter is equal to or greater than 8. If it is, we send your "say begin" line 8 times, then "hitall", then reset the Counter to 0.

Get my plugins here: http://www.magnumsworld.com/muds/

Constantly proving I don't know what I am doing...
Magnum.
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.


13,211 views.

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

Go to topic:           Search the forum


[Go to top] top

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