[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Calling a Sub from a sub
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Calling a Sub from a sub

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page


Posted by David B   USA  (80 posts)  [Biography] bio
Date Tue 11 Mar 2003 12:56 PM (UTC)  quote  ]
Message
Is there anyway to call a second Subroutine, from within a Subroutine?

sub whaterver(name, line, wilds)
<insert random code>
if yadda yadda = blah then
callsub (yadda)
else
yadda yadda
end if
endsub

My code(with a LOT of Nicks help) to fame:

sub OnAutoCombo (TriggerName, TriggerLine, arrWildCards)
dim AutoCombo
AutoCombo = split (arrWildCards (1))
Dim i, attack
for i=lbound (AutoCombo ) to ubound (AutoCombo )
Select Case AutoCombo (i)
case "rp" attack = "punch right"
case "lp" attack = "punch left"
case "s" attack = "sweep"
case "r" attack = "roundhouse"
case else attack = "" ' unknown attack type
End Select
if i = ubound (AutoCombo ) then
world.send "throw " + world.getvariable ("attacker") + " down"
else
world.send attack + " " + world.GetVariable ("attacker")
end if
next
end sub
[Go to top] top

Posted by Magnum   Canada  (580 posts)  [Biography] bio
Date Reply #1 on Tue 11 Mar 2003 01:17 PM (UTC)  quote  ]

Amended on Tue 11 Mar 2003 01:28 PM (UTC) by Magnum

Message
Visual Basic Scripting Edition Language Reference
--------------------------------------------------------------------------------

Call Statement

Transfers control to a Sub or Function procedure.

[Call] name [argumentlist]

Arguments

Call

Optional keyword. If specified, you must enclose argumentlist in parentheses. For example:

Call MyProc(0)

name

Required. Name of the procedure to call.

argumentlist

Optional. Comma-delimited list of variables, arrays, or expressions to pass to the procedure.

Remarks

You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist. If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded.

Call MyFunction("Hello World")
Function MyFunction(text)
   MsgBox text
End Function

Requirements

Version 1
--------------------------------------------------------------------------------
©2000 Microsoft Corporation. All rights reserved.

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

Constantly proving I don't know what I am doing...
Magnum.
[Go to top] top

Posted by David B   USA  (80 posts)  [Biography] bio
Date Reply #2 on Tue 11 Mar 2003 02:11 PM (UTC)  quote  ]
Message
So if I get this straight
I can do a subroutine and depending if a variable is X or Y I could call an external subroutine.

My code(with a LOT of Nicks help) to fame:

sub OnAutoCombo (TriggerName, TriggerLine, arrWildCards)
dim AutoCombo
AutoCombo = split (arrWildCards (1))
Dim i, attack
for i=lbound (AutoCombo ) to ubound (AutoCombo )
Select Case AutoCombo (i)
case "rp" attack = "punch right"
case "lp" attack = "punch left"
case "s" attack = "sweep"
case "r" attack = "roundhouse"
case else attack = "" ' unknown attack type
End Select
if i = ubound (AutoCombo ) then
world.send "throw " + world.getvariable ("attacker") + " down"
else
world.send attack + " " + world.GetVariable ("attacker")
end if
next
end sub
[Go to top] top

Posted by Shadowfyr   USA  (1,775 posts)  [Biography] bio
Date Reply #3 on Tue 11 Mar 2003 06:06 PM (UTC)  quote  ]
Message
In short yes. Note a few things however:

1. Using 'call' is always prefered for 'sub' types. Without it both pure Visual Basic and VBScript will do odd things in some cases. This is because a sub called without the 'call' command is treated different that one that is called that way. Better safe than confused and even the description of why and what happens when you do it the wrong way confuses me. ;)

2. There is a difference between 'function' and 'sub'. Technically 'function' is used to return a value after a call, it only uses the 'call' command it you don't care what is returned (which is the case with most Mushclient's own script commands like world.note, where if it doesn't work, it tends to be pretty obvious. Example:
function Test(blah)
  if blah = 1 then
    Test = blah
    world.note blah
  end if
end function

sub Test(blah)
  if blah = 1 then
    world.note blah
  end if
end sub


You could call these like:

Variable = Test(MyValue)

and

call Test(MyValue)

The first version would return nothing if 'blah' was anything except 1, or 1 if it is, and place the result in your variable. This is convenient for things like a color calculator I use that requires the same large batch of code to be called up to 12 times in the same script and returns a HTML color string. The second version just performs a task and returns without any result. Unless you have to return some value or you need to return an error so that you know if something worked, you probably won't need the 'Variable = sub' version.

3. The 'name' of the variable does not need to be the same in the sub declaration as in the call. VBScript only cares that you pass the correct number and real languages that allow you to specifically tell it the type, like 'integer' or 'string', only care that what you send the sub or function is the same type and in the right order. This lets you give the variable a name that makes sense for that subroutine, but may be called something else in the one calling it. Example:

sub List
for count = 0 to 19
call Show(Weapons(count))
next
end sub

sub Show(Weapon)
world.colournote "Purple","Black",Weapon
end sub

This is a simple example, but the idea is that since you are only dealing with one 'weapon' at a time in Show, it makes sense to use that name there, even though the call uses an array called 'Weapons'.

main {
__if (Schrodinger_Cat is Alive or version >= "XP"){
____if version = "Vista" then Performance /= Number_of_Cores;
____call Functional_Code();}
__else
____call Crash_Windows();}
[Go to top] 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.


2,147 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]