Help with passing parameters please

Need some help with that macro you're working on or aren't quite sure how to get your macro to do something? Ask here!

Moderator: MacroQuest Developers

wassup
Official Guardian and Writer of TFM
Official Guardian and Writer of TFM
Posts: 1487
Joined: Sat Oct 26, 2002 5:15 pm

Post by wassup » Tue Oct 07, 2003 10:58 pm

Yep, that did it:

Code: Select all

Sub Main
    /declare myvar1 local
    /declare myvar2 local
    /declare myvar3 local

    /varset myvar1 $char(mana,cur)
    /echo Current mana is @myvar1
    
    /varset myvar2 $spell("Spirit of Wolf",mana)
    /echo Mana for Spirit of Wolf is @myvar2
    
    /varset myvar3 0
    /echo MyVar3 value is @myvar3

    /call TestSub @myvar1 @myvar2 @myvar3
    
    /echo Mana after casting Spirit of Wolf is $return
/return

Sub TestSub(myvar1,myvar2,myvar3)
    /echo Values passed to TestSub are @myvar1, @myvar2, @myvar3
    
    /varadd myvar3 @myvar1
    /echo MyVar3 is now @myvar3
    
    /varsub myvar3 @myvar2
    /echo MyVar3 is now @myvar3
    
/return @myvar3
[Tue Oct 07 22:55:46 2003] [MacroQuest] Current mana is 6529
[Tue Oct 07 22:55:46 2003] [MacroQuest] Mana for Spirit of Wolf is 40
[Tue Oct 07 22:55:46 2003] [MacroQuest] MyVar3 value is 0
[Tue Oct 07 22:55:46 2003] [MacroQuest] Values passed to TestSub are 6529, 40, 0
[Tue Oct 07 22:55:46 2003] [MacroQuest] MyVar3 is now 6529.00
[Tue Oct 07 22:55:47 2003] [MacroQuest] MyVar3 is now 6489.00
[Tue Oct 07 22:55:47 2003] [MacroQuest] Mana after casting Spirit of Wolf is 6489.00

Plazmic
The One
The One
Posts: 800
Joined: Fri Jun 14, 2002 12:31 am
Contact:

Post by Plazmic » Tue Oct 07, 2003 11:08 pm

Hmm... there are no true pointers, so you can't set the value of a variable local to a subroutine that called this one ;(

Best I can offer currently would be:

Code: Select all

| This adds the second parameter to the first, then zeros the second
/call SubAdd @MyLocal @AnotherLocal
/varset MyLocal $arg(1,$return)
/varset AnotherLocal $arg(2,$return)
....
Sub SubAdd(FirstArg,SecondArg)
   /varadd FirstArg @SecondArg
/return @FirstArg,0
About Subroutines and parameters
"Sub ThisSub" will accept the first parameter as "Param0"
"Sub AnotherSub(MyArg)" will accept the first paramter as "MyArg"
If you name parameters, they need to be in ()'s
Last edited by Plazmic on Thu Oct 09, 2003 3:13 am, edited 1 time in total.
- Plazmic

wassup
Official Guardian and Writer of TFM
Official Guardian and Writer of TFM
Posts: 1487
Joined: Sat Oct 26, 2002 5:15 pm

Post by wassup » Tue Oct 07, 2003 11:22 pm

Heya Plazmic...

Using

Code: Select all

/call SubAdd(@MyLocal,@AnotherLocal)
doesn not work, it will only work if you use

Code: Select all

/call SubAdd @MyLocal @AnotherLocal
At least in my experience so far.

Mckorr
Developer
Developer
Posts: 2326
Joined: Fri Oct 18, 2002 1:16 pm
Location: Texas

Post by Mckorr » Wed Oct 08, 2003 10:12 am

Okay, let me make sure I got the syntax right

Code: Select all

sub Main
  /declare alpha local
  /declare beta local

  /call SomeSub @alpha @beta
  /varset alpha $arg(1,$return)
  /varset beta $arg(2,$return)

  /echo Alpha and Beta are now @alpha and @beta

/endmacro

sub SomeSub(delta,gamma)
  /varset delta 0
  /varset gamma 1
/return @delta,@gamma
That script should pass the value of alpha and beta to SomeSub, where they are stored as delta and gamma. Upon return we have to /varset to the new values passed back from SomeSub.

The /echo should return "Alpha and Beta are now 0 and 1"

Correct?
MQ2: Think of it as Evolution in action.

Jabber
a lesser mummy
a lesser mummy
Posts: 32
Joined: Sat Aug 23, 2003 2:45 pm

passing an Array to a Sub

Post by Jabber » Wed Oct 08, 2003 3:23 pm

Anyone try passing arrays to Subs?
I tried something simple like:

Code: Select all

Sub Main
	/zapvars 
	/declare a array

	/varset a(0) 7
	/varset a(1) 8
	/varset a(2) 9
	/call SubTest @a
/return

Sub SubTest(inArray)

	/echo 0: @inArray(0) 1:@inArray(1) 2:@inArray(2)

/return
the result wasn't really what i wanted.

Result:

Code: Select all

[MacroQuest] 0: @a(0) 1: @a(1) 2: @a(2) 
i expected to see something like this:

Code: Select all

[MacroQuest] 0: 7 1: 8 2: 9 
Any ideas?

Plazmic
The One
The One
Posts: 800
Joined: Fri Jun 14, 2002 12:31 am
Contact:

Post by Plazmic » Wed Oct 08, 2003 9:03 pm

Arrays are global. You don't need to (and can't) pass them into a sub.
You can pass individual elements in though, of course...
- Plazmic

Jabber
a lesser mummy
a lesser mummy
Posts: 32
Joined: Sat Aug 23, 2003 2:45 pm

Post by Jabber » Thu Oct 09, 2003 2:13 am

well i understand they are global, but i wanted to pass in a different array to the sub depending on conditions. I will just send in a variable instead to tell me which part of the 2-dimentional array to use.

Thanks for clearing it up though!

Plazmic
The One
The One
Posts: 800
Joined: Fri Jun 14, 2002 12:31 am
Contact:

Post by Plazmic » Thu Oct 09, 2003 3:13 am

Wassup wrote:

Code: Select all

/call SubAdd(@MyLocal,@AnotherLocal)
doesn not work, it will only work if you use

Code: Select all

/call SubAdd @MyLocal @AnotherLocal
Bad copy/paste ;(
No ()'s on the call line is correct...
- Plazmic