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 ➜ VBscript ➜ Variable problem

Variable problem

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


Posted by Microphone   (8 posts)  Bio
Date Sun 14 Sep 2003 01:08 AM (UTC)
Message
Okay, I'm making a script that when a character whispers me 'join' it stores their name, sets stats for them, and all that fun stuff. I also want it to check to make sure they don't already have an account, but since the game I play read's spaces in names as | Joe Slob would be Joe|Slob, therefore making it an illegal variable name.

sub join (name, output, wildcards)
name = wildcards(1)
if (len(world.getvariable(name))<1) then
world.setvariable name, "HP: 50 MP: 25 Strength: 10 Defense: 5 Speed 7: Vitality: 5 Energy: 5"
world.send "wh " + name + " Your account has been created!"
else
world.send "wh " + name + " You already have an account!"
end if
end sub

that's what it looks like so far, any ideas?
Top

Posted by Magnum   Canada  (580 posts)  Bio
Date Reply #1 on Tue 23 Sep 2003 09:31 AM (UTC)

Amended on Tue 23 Sep 2003 09:48 AM (UTC) by Magnum

Message
You need to follow C++ conventions, as stated on this page:
http://cplus.about.com/library/weekly/aa021202a.htm

I had the same problem with script I use that sets timer names based on trigger results. I was working around the problem with individual 'Replace' lines, but I just did a lot of tinkering and wrote these two nifty functions for us:

Function FormalName (NameString)
  Dim regEx
  Set regEx = New RegExp
  regEx.Pattern = "\W"
   Set Matches = regEx.Execute(NameString)
   For Each Match in Matches
      NameString = Replace(NameString, Match.Value, "ANSICHR" & Asc(Match.Value))
   Next
   FormalName = NameString
End Function

Function InformalName (NameString)
  Dim regEx
  Set regEx = New RegExp
  regEx.Pattern = "(ANSICHR)(\d+)"
   Set Matches = regEx.Execute(NameString)
   For Each Match in Matches
      NameString = Replace(NameString, Match.Value, Chr(Match.SubMatches(1)))
   Next
   InformalName = NameString
End Function

The first function takes any illegal character and replaces it with a string that starts with "ANSICHR" followed by the ANSI value for that character. For example, the string "*Star*" becomes "ANSICHR42StarANSICHR42".

The second function reverses the effects of the first. For example, "ANSICHR42StarANSICHR42" gets turned back into "*Star*".

Ok, take that script above, together with the following script, and it should work with your project:

Function ValidMember(MemberName)
  Dim MemberList
  ValidMember = False
  MemberName = "Members_" & FormalName(MemberName)
  MemberList = World.GetVariableList
  For Each Member in MemberList
    If MemberName = Member Then ValidMember = True
  Next
End Function

Sub Join (Name, Output, Wildcards)
  Dim VarName
  Name = Wildcards(1)
  If ValidMember(Name) Then
    VarName = "Members_" & FormalName(Name)
    World.SetVariable VarName, "HP: 50 MP: 25 Strength: 10 Defense: 5 Speed 7: Vitality: 5 Energy: 5"
    World.Send "wh " & name & " Your account has been created!"
  Else
    World.Send "wh " & name & " You already have an account!"
  End If
End Sub

I took the liberty of putting "Members_" at the front of the variable names which represent the people in your list. This is something I often do in my own script so that I can find them easier when using the MUSHclient interface to view variables.

Suppose you wanted to print a chart for yourself of everyone on your list. Here is a sample subroutine which shows how it might be done, and demonstrates how to reverse the 'encoded' variable names above:

Sub PrintMembers (Name, Output, Wildcards)
  Dim MemberList
  Dim MemberName
  Dim MemberData
  Dim StrPos
  MemberList = World.GetVariableList
  For Each Member in MemberList
    StrPos = InStr(1, Member, "Members_", 1)
    If StrPos = 1 Then
      MemberData = World.GetVariable("Member")
      MemberName = Replace(Member, "Members_", "", 1, 1, 1)
      MemberName = Informal(MemberName)
      World.Note MemberName & " --> " & MemberData
    End If
  Next
End Sub

You can actually see that putting "Members_" on the front of the variable names is crutial. Without it, you would have a tougher time figuring out which of your variables are people in your list, and which are just other variables that your script uses.

Thanks to Vaejor and Nick for the thread on Regular Expressions within VBS.

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.


11,355 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.