Variable problem

Posted by Microphone on Sun 14 Sep 2003 01:08 AM — 2 posts, 13,817 views.

#0
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?
Canada #1
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.