alias calls 2 subs and sets var

Posted by Guest1 on Thu 13 Jul 2006 03:00 PM — 33 posts, 118,081 views.

USA #0
Hi. Can't remember how to do this or even if I can.. I'm using VB. I have an alias that I want to do three things:
set a variable
call a sub
call another sub

Having it set the var and call one sub is no problem obviously, but calling the second sub is where I'm drawing a blank.

Can the alias call two subs at once, and if so, what is the syntax I use? Usually I just use the 'edit alias' window to set it up and fill in the script textbox, but that only allows one sub to be called.. can I go to the .mcl file and just add a second 'script="subname"' line to it (I'm guessing not)? ..Or is there a different method to call subs just like with setting variables - I can either fill in the variable textbox in the 'edit alias' window and send %1, or I can send
SetVariable "varname", "%1"
instead and achieve the same thing..

Alternatively I can just call one sub from the alias, and get that one sub to call the other sub.. but unsure what the syntax is on the .vbs script to do that.

Thanks in advance for help :)

Amended on Thu 13 Jul 2006 03:01 PM by Guest1
USA #1
You can call one sub, which takes care of doing everything else.

So you could have the alias call "DoMyAlias", the contents of which would be world.SetVar(blablabla), followed by CallMyFirstSub(), and finally, CallMySecondSub().
USA #2
yeah cheers, ideally that's what I want to do, but what is the valid function syntax in VB? ie: I can put
SetVariable "variablename", "value"
inside a sub to set that variable, but what is the function I type to have that sub call another sub in VB (or the syntax I put in the alias itself to call the subs)? I've been looking through the MC script functions list on this site (http://www.gammon.com.au/scripts/function.php) but can't seem to spot the right one - the function that you call 'CallMyFirstSub()' etc., that is the one I want to know the correct syntax of in VB.
Amended on Thu 13 Jul 2006 03:23 PM by Guest1
USA #3
If your sub is named MyFirstSub, then it should be sufficient to just write 'MyFirstSub' on its own line, or perhaps 'MyFirstSub()'. That's all you should have to do.
USA #4
Nope doesn't seem to work in VB.. I tried a couple of ways of doing it.. first I just has the alias just call a new sub (call it subnew for now). In that subnew, I had it scripted to do the following:

sub subnew (thename, theoutput, thewildcards)
   world.SetVariable "variablename", "%1"
   sub1
   sub2
end sub

That sets the variable ok, but errors out calling the other 2 subs. I tried also using syntax sub1() above as you suggested, but it still spat an error.

Next I tried putting all the calls inside the alias itself.. so the alias was sending to script

world.SetVariable "variablename", "%1"
sub1
sub2

that didn't work - error on line 2.. so I tried adding the () as you suggested, same problem.


..finally figured out what the problem was - it was an issue with properties / arguements and one sub needing to use wildcards, the other not.. got it to work in the end, thanks, by having the alias call the two subs in a different way each (removed irrelevant bits):

  <alias
   match="myalias *"
   script="sub2"
   send_to="12"
  >
   <send>
   SetVariable "variablename", "%1"
   sub1
   </send>
  </alias>

..sub1 did not require the wildcard, sub2 did. Knew there would be a way, thanks again :)
USA #5
Oh... if the subs need arguments, you need to call them like this: MySub arg1, arg2, ...
USA #6
excellent that solves a bunch of issues for me :)
cheers!

err.. ok probably messed up syntax.. if my current alias is:


  <alias
   match="myalias *"
   script="sub2"
   send_to="12"
  >
   <send>
   SetVariable "variablename", "%1"
   sub1
   </send>
  </alias>


and sub1 does not need the wildcard but sub2 does, I can change it to something like:


  <alias
   match="myalias *"
   send_to="12"
  >
   <send>
   SetVariable "variablename", "%1"
   sub1
   sub2 arg1
   </send>
  </alias>


except that line 'sub2 arg1' spits the dummy.. tried replacing it with 'sub2 arg1,' and 'sub2 %1' and a couple of others but no joy.. I figure it's just the syntax I'm not getting right.. but if not, the other way above works fine :)
Amended on Thu 13 Jul 2006 05:44 PM by Guest1
USA #7
You need to replace arg1 with the name of the variable you're passing as an argument. So, if it's the wildcards you want to pass, you can try using 'thewildcards' or whatever the name of the wildcards variable is. (Somebody more familiar with how MUSHclient does its VBscripting might have to help out here.)
USA #8
Yeah sorry, I should've said, I tried sending on that line

sub2 %1
sub2 *
sub2 (%1)
sub2 variablename
sub2 "variablename"
sub2 thewildcards
sub2 "thewildcards"
sub2 thewildcards (1)

and a few variations but no luck with any of those.. prob just a matter of syntax :/
USA #9
What error message are you getting when you put in 'Sub2 %1'?
USA #10

Wrong number of arguments or invalid property assignment: 'sub2'
Line in error: 


the sub it's calling is (edited):


sub sub2 (thename, theoutput, thewildcards)
   if thewildcards (1) = world.getvariable ("character") then
      do some stuff
   else
      do some other stuff
   end if
end sub




#11
That function has 3 parameters yet you are only trying to pass one argument. That's why there's an error. Hence "Wrong number of arguments"
USA #12
Forgive my ignorance, but the solve this I'd do...?

Replacing that first line
(thename, theoutput, thewildcards)
with something else, or replace
thewildcards (1)
with something else? Appreciate your patience.
#13
You should call it with something like

Sub2 "" "" "%1" or something
USA #14
Or set the script to call "TheAliasSub" (not using send), and then:


sub TheAliasSub(thename, theoutput, thewildcards)
  SetVariable "variablename", "%1"
  sub1
  sub2 thename, theoutput, thewildcards
end sub

sub sub2 (thename, theoutput, thewildcards)
   if thewildcards (1) = world.getvariable ("character") then
      do some stuff
   else
      do some other stuff
   end if
end sub
USA #15
tried using sub2 "" "" "%1"

Expected end of statement
Line in error: 
sub2 "" "" "value"

tried with 1 and 2 less "" sets before, and even tried an extra "" after, none worked :/
USA #16
You need commas between arguments. But see the post I put up while you wrote yours.
USA #17
ok.. I tried what you said first and created a new sub:

sub TheAliasSub(thename, theoutput, thewildcards)
  SetVariable "variablename", "%1"
  sub1
  sub2 thename, theoutput, thewildcards
end sub

and while it solved the issue I had, it created a new issue as it set the value of the variable physically to %1 rather than the value I entered on the alias.. hehe

so.. I took out the SetVariable "variablename", "%1" from the sub above and put it back into the alias itself, so the alias sets the variable and calls the sub above (less the set.var line).. that worked :) However I'm trying to cut down the size of my 5-year-old script rather than add additional subs, so I'll try with commas as you said..

..and nope, using
sub2 "", "", "%1"
spat an error

Type mismatch: 'thewildcards'
Line in error: 


fun huh. Must be a way this works without writing additional subs.
USA #18
You probably don't want the quotes around %1. Try just %1, without the quotation marks.

The other problem is that %1 is a string, not an array of wildcards, whereas the sub expects the whole wildcard array.

If you only care about the first one, you could adjust sub2 to take only one argument, a single string, and remove the first two arguments (which you don't appear to be using). Then, you could call sub2 in the <send> block passing in %1 as the sole argument.
USA #19
Still get a Type mismatch: 'thewildcards' error without the quotes around the %1 unfortunately.

Yes I only care about the first (and only) wildcard as it's an alias I'm initially using to send it rather than a trigger with a possible bunch of variables. When you say about adjusting the number of arguments and the sub, I take it you mean that the alias should just be sending 'sub2 %1' and then the sub itself should be written instead of:

sub sub2 (thename, theoutput, thewildcards)
   if thewildcards (1) = world.getvariable ("character") then
      do some stuff
   else
      do some other stuff
   end if
end sub

I should amend it to:

sub sub2 (thewildcards)
   if thewildcards = world.getvariable ("character") then
      do some stuff
   else
      do some other stuff
   end if
end sub

..? Now although that doesn't spit an error, it also has stopped matching on that if statement.. (I tried writing wildcard without the 's' in both cases above too - no joy).. Probably my lack of knowledge in syntax is the problem now..
USA #20
Variable names aren't really important. You could call them flibberfloop or var12345; it doesn't matter. They're just names that make it easier for us humans to know what the variable is supposed to refer to.

Anyhow, you could try doing something like this in sub2, before the if check:

World.note %1
World.note world.getvariable ("character")

That could help you see what the two things actually are.
USA #21
well the variable is getting set ok (that is set direct from the alias), but '%1' as an actual value is being sent to the sub, not the wildcard...

It all works fine using this setup as the alias:

  <alias
   match="myalias *"
   script="sub2"
   send_to="12"
  >
   <send>
   SetVariable "variablename", "%1"
   sub1
   </send>
  </alias>

but only because sub1 does not require the wildcard, onlt sub2 does.. but I'm trying to get sub2 out of the script field where it is in the above example, and put it into the send field, because in another alias I will want both sub1 and sub2 to require that wildcard, and as far as I know, you can't put two subs in that script field on the 'edit alias' window.
Amended on Thu 13 Jul 2006 09:10 PM by Guest1
USA #22
So, you're saying that when you World.note %1, you get a literal %1, not the first match? And that happens whether you do it in script, send, or sub2?
USA #23
if I put the world.note "%1" in the alias it shows the value of the match correctly.. if I put the world.note it in the sub2 on my vbs script, I get %1 as the value.. so what is getting sent through to sub2 from the alias when I use
sub2 %1
in the alias send field is not the wildcard, but %1..
USA #24
Oh... in the sub2 subroutine, you have to use the name of the argument, not %1... But, you want to use %1 in the alias field.

Do you know what subroutines and arguments are? It seems like it might be a good time to go back to basics a bit. :)
Australia Forum Administrator #25
You are confusing two methods here:


<alias
   match="myalias *"
   script="sub2"
   send_to="12"
  >
   <send>

   SetVariable "variablename", "%1"
   sub1

</send>
  </alias>


The part between <send> and </send> (ie. the contents of the "send" box) are what has the %1 substitution done. Your call of sub1 cannot know the wildcards, as you didn't pass them down.

Your call of sub2 knows the wildcards, but they are in the arguments that MUSHclient supplies.

i.e.


Sub sub2 (name, line, wildcards)

  Note wildcards (2)

End Sub


Having said that, there is a way for all 3 to get wildcards in a consistent way, and that is to use GetTriggerWildcard.

That returns the named of numbered wildcard of the most recent matching trigger. You can use that in send to script, or the subs called directly or indirectly. For example:


<triggers>
  <trigger
   enabled="y"
   match="The (.*) of"
   name="myname"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>Note (GetTriggerWildcard ("myname", 1))</send>
  </trigger>
</triggers>

USA #26
Heh.. I feel like I'm going in circles here. Clearly I need assistance - that's why I'm posting here :) I'm rewriting a script I wrote 5 years ago, and haven't dealt with it again in any great detail since then ..until now.

Ok to make it easy for me see if you can solve this. I have an alias of 'myalias *' When I type a command of 'myalias Joe', I want that alias to set a variable called 'name' to the name 'Joe', and also call two sub routines, named sub1 and sub2, which are included among many many others sitting on my script.vbs file.

Presently I have been able to set it up myself without issue when only one of the subs requires that wildcard of 'Joe', because the one that needs the wildcard I place in the script field of the alias, like so:

  <alias
   match="myalias *"
   script="sub2"
   send_to="12"
  >
   <send>
   SetVariable "name", "%1"
   sub1
   </send>
  </alias>

along with the subs like so.. first the one that needs the wildcard:

sub sub2 (thename, theoutput, thewildcards)
  if thewildcards (1) = world.getvariable ("character") then
      do some stuff
  else
      do some other stuff
  end if
end sub

and then the sub that doesn't need the wildcard:

sub sub1
  do some stuff
end sub

However, if I needed BOTH subs to use that wildcard, that's where I get stuck. I can't both both sub1 and sub2 in that script box on the alias, so I have to figure how to get the send box to call a subroutine that also requires a wildcard (in this example, Joe), without writing another subroutine to call the existing subroutines sub1 and sub2.

Effectively I would like to move that sub2 out of the 'script' field on the alias and put it in the 'send' field of the alias.. but 'sub1 %1' and many other variations as detailed over the last page and a half don't work - the best result I got was that it all worked without error, but what got sent to sub2 was the value '%1' instead of 'Joe'.. if anyone can actually spell it out for me I would be very grateful, and will be able to learn from it :)

Thanks.
USA #27
Oops just saw your comment Nick, posted while I was writing mine. Thanks.

So really the bottom line is I can't send a call to a sub that includes a wildcard directly from the alias 'send' field the way I was trying to? I can only use the 'script' field of the alias to do that, thus am limited to one sub per alias that requires the wildcard (although multiple subs that don't require a wildcard is not a problem).. which is what I actually thought from the start but was trying to find out one way or the other for sure, hehehe.

The way around you said is to use GetTriggerWildcard which involves an additional trigger. Another method actually worked back here too, which was to use the 'script' field to call a sub that would thus also see the wildcard value, and then have that sub call the other subs that want to use that wildcard value.

Anyway I think I have my answer - I can't really get one alias to directly call multiple subs where more than one of those subs requires a wildcard value.. unless I create an additional sub or trigger to handle it.
Amended on Thu 13 Jul 2006 10:32 PM by Guest1
Australia Forum Administrator #28
Quote:

I can't really get one alias to directly call multiple subs where more than one of those subs requires a wildcard value..


Well, you can because the caller knows the wildcards.

eg.


SetVariable "variablename", "%1"
sub1 "%2", "%3"


Here you are calling sub2 (from the trigger itself) and in the "send to script" are calling sub1, passing down wildcards 2 and 3.

Also check out:

http://www.gammon.com.au/scripts/doc.php?function=GetTriggerInfo

Using that you can get the wildcards for any recent trigger, so the sub could use that. Of course, as well as:

http://www.gammon.com.au/scripts/doc.php?function=GetTriggerWildcard

So, you really have many ways of solving this problem. :)
Amended on Thu 13 Jul 2006 10:29 PM by Nick Gammon
USA #29
Yup thanks, just other ways of doing it than most people here thought :)

I wonder if that could be looked at in some future release though (if it's feasible) to be able to just write an alias (not a trigger) of, for example,

  <alias
   match="my * has * so back off."
   send_to="12"
  >
   <send>
   sub1 %1
   sub2 %1
   sub3 %2
   </send>
  </alias>

If I then type in the command line 'my girlfriend has rabies so back off.', sub1 and sub2 would be called and use the wildcard 'girlfriend', and sub3 would be called using the wildcard 'rabies'.. :)
Australia Forum Administrator #30
Just noticed you are doing an alias and not a trigger, but the technique is the same:

http://www.gammon.com.au/scripts/doc.php?function=GetAliasInfo

http://www.gammon.com.au/scripts/doc.php?function=GetAliasWildcard
Australia Forum Administrator #31
Quote:

I wonder if that could be looked at in some future release though (if it's feasible) to be able to just write an alias (not a trigger) of, for example,


  <alias
   match="my * has * so back off."
   send_to="12"
  >
   <send>
   sub1 %1
   sub2 %1
   sub3 %2
   </send>
  </alias>




Sure, you can do that now but you are missing quotes. Think about how this will be expanded by substituting the words you gave:


   sub1 girlfriend
   sub2 girlfriend
   sub3 rabies


You are calling subs with variable names (which will probably be empty) so that won't work. You want:


  <alias
   match="my * has * so back off."
   send_to="12"
  >
   <send>
   sub1 "%1"
   sub2 "%1"
   sub3 "%2"
   </send>
  </alias>


Now that will expand to:


   sub1 "girlfriend"
   sub2 "girlfriend"
   sub3 "rabies"


... which is what you want.
USA #32
GOT IT :)

sub sub2 (thewildcards)
  if world.GetAliasWildcard ("my_alias", 1) = "girlfriend" then
      World.send "scream"
  else
      World.send "cheer"
  end if
end sub

happy happy joy joy. Thanks heap for your help everyone :)