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

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

Help with passing parameters please

Post by Mckorr » Tue Oct 07, 2003 2:51 pm

I'm trying to localize my variables a bit, just good programming habit. Not sure how this works with parameters however:

Code: Select all

    /if @ObstacleCount>=3 {
      /call CheckObstacle(@ObstacleCount)
      /goto :Movementloop
    }
    /if n $target(distance,nopredict)>10 /goto :MovementLoop
/return

sub CheckObstacle(ObstacleCount)
  /if n @MyXLOC==$char(x) /if n @MyYLOC==$char(y) /call HitObstacle
  /varset MyXLOC $char(x)
  /varset MyYLOC $char(y)
  /varset @ObstacleCount 0
/return @ObstacleCount
What I want to do is pass the value for ObstacleCount to CheckObstacle, reset it to 0 in the called sub, and then pass that value back to the main routine at the point it calls it (/call CheckObstacle(@ObstacleCount).) The declaration for ObstacleCount in the first (partial) sub is

Code: Select all

/declare ObstacleCount local
Is this going to work, or will I be forced to use global variables for ObstacleCount?
MQ2: Think of it as Evolution in action.

MacroFiend
a grimling bloodguard
a grimling bloodguard
Posts: 662
Joined: Mon Jul 28, 2003 2:47 am

Post by MacroFiend » Tue Oct 07, 2003 3:15 pm

That's a question for someone who has been digging through the new .cpp for user variables. If Plazmic coded it to create parameters as locals (and that locals are stored as {sub}-{variable} like the arrays are, then you shouldn't have a problem with a local variable being used and declared in different subs.

If I get some time, I'll take a wander through the new user variable cpp and see if I can figure out how he set it up.

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 3:15 pm

The only line in there that I believe will cause some problems is:

Code: Select all

 /varset @ObstacleCount 0
Under the old MQ that would be the equivalent of

Code: Select all

 /varset $ObstacleCount 0
If I understood Plazmic's explanation correctly...


Code: Select all

 
/if n @ObstacleCount=3 { 
      /call CheckObstacle(@ObstacleCount) 
      /goto :Movementloop 
    } 
    /if n $target(distance,nopredict)>10 /goto :MovementLoop 
/return 

sub CheckObstacle(ObstacleCount) 
  /if n @MyXLOC==$char(x) /if n @MyYLOC==$char(y) /call HitObstacle 
  /varset MyXLOC $char(x) 
  /varset MyYLOC $char(y) 
  /varset ObstacleCount 0  |Might need to be Param0
/return @ObstacleCount    |Might need to be @Param0
Might be wrong though. I haven't experimented with the new version much yet.

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

Post by Mckorr » Tue Oct 07, 2003 3:33 pm

As I understood it you could name a parameter, or if unnamed it defaulted to Param0.

Secondary question: to pass multiple parameters

/call MySub(Param0 Param1 Param2) ???

or do the parameters need to be seperated by a comma?
MQ2: Think of it as Evolution in action.

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

Post by Mckorr » Tue Oct 07, 2003 3:43 pm

Okay, this is what I have so far... keep in mind that MoveToMob is called from someplace else.

Code: Select all

sub MoveToMob

  /declare MyXLOC local
  /declare MyYLOC local
  /declare ObstacleCount local
  
  /varset MyXLOC $char(x)
  /varset MyYLOC $char(y)
  /varset ObstacleCount 0

  /if n $target(distance,nopredict)<=15 {
    /face look
    /return
  }
  /sendkey down up

  :Movementloop
    /varadd ObstacleCount 1
    /if $target()=="FALSE" /return
    /face look
    /if n $target(distance,nopredict)<=10 {
      /face look
      /sendkey up up
      /return
    }
    /if @ObstacleCount>=3 {
      /call CheckObstacle(@ObstacleCount @MyXLOC @MyYLOC)
      /goto :Movementloop
    }
    /if n $target(distance,nopredict)>10 /goto :MovementLoop
/return

sub CheckObstacle(Count XLoc YLoc)
  /if n @MyXLOC==$char(x) /if n @MyYLOC==$char(y) /call HitObstacle
  /varset XLoc $char(x)
  /varset YLoc $char(y)
  /varset Count 0
/return @Count @XLoc @YLoc
Since everything should be a local variable I should be able to use the same variable names in seperate subs without problem, but I've altered them in the second subroutine just in case.

Of course this is all untested. Trying to get a handle on the new syntax before I compile the new changes.
MQ2: Think of it as Evolution in action.

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 3:45 pm

Separated by comma's

Code: Select all

/call MySub(Param0,Param1,Param2)

That macro above is a really good starting point, just separate the passed variable names in the Sub, and I think :

Code: Select all

/[color=cyan]if @ObstacleCount>=3[/color] { 
      /call CheckObstacle(@ObstacleCount @MyXLOC @MyYLOC) 
      /goto :Movementloop 
    } 
should be

Code: Select all

/[color=cyan]if n @ObstacleCount>=3 [/color]{ 
      /call CheckObstacle(@ObstacleCount @MyXLOC @MyYLOC) 
      /goto :Movementloop 
    } 
Last edited by wassup on Tue Oct 07, 2003 3:49 pm, edited 1 time in total.

MacroFiend
a grimling bloodguard
a grimling bloodguard
Posts: 662
Joined: Mon Jul 28, 2003 2:47 am

Post by MacroFiend » Tue Oct 07, 2003 3:49 pm

I didn't thing the syntax for /call had changed. I wasn't wrapping the params in () or comma delimiting them. Just space.

I've never tried to return multiple variables from a sub either, so I may just be a day late and a dollar short.

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

Post by Mckorr » Tue Oct 07, 2003 3:58 pm

Plaz's post in General Development shows this:

Code: Select all

Sub Main(FirstArg)
so I'm pretty sure the parameters need to be enclosed in parentheses.

As for the /if n @ObstacleCount, the original macro didn't need it. ObstacleCount was #define ObstacleCount $v#, and that /if statement just has the @ affixed to the front of the variable.
MQ2: Think of it as Evolution in action.

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

Post by Mckorr » Tue Oct 07, 2003 7:01 pm

Eh, gave up on it for now. Would never function correctly in tests... not sure it could handle multiple parameters. Final version is here:

http://macroquest2.com/phpBB2/viewtopic.php?t=3307
MQ2: Think of it as Evolution in action.

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 7:20 pm

I've been trying to run a short macro to test this and cannot figure it out either. It never gets to the Sub. I keep getting:

Ending macro: Bad variable in /var function.
vartest.mac@20 (TestSub(myvar1,myvar2,myvar3)): /varadd myvar3 @myvar1
vartest.mac@15 (Main): /call TestSub(myvar1,myvar2,myvar3)

Here is the test macro:

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 @myvar3
/return

Sub TestSub(myvar1,myvar2,myvar3)
    /varadd myvar3 @myvar1
    /echo MyVar3 is now @myvar3
    
    /varsub myvar3 @myvar2
    /echo MyVar3 is now @myvar3
/return @myvar3

MacroFiend
a grimling bloodguard
a grimling bloodguard
Posts: 662
Joined: Mon Jul 28, 2003 2:47 am

Post by MacroFiend » Tue Oct 07, 2003 7:33 pm

The parameter definition is enclosed in parens and comma delimited but the actual /call sub param0 param1 param2 etc is the same.

This should work ... but I haven't been in game to check it. The changes are in red.

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 

[color=red]    /call TestSub myvar1 myvar2 myvar3
    /echo Mana after casting Spirit of Wolf is $return[/color]
/return 

Sub TestSub(myvar1,myvar2,myvar3) 
    /varadd myvar3 @myvar1 
    /echo MyVar3 is now @myvar3 
       
    /varsub myvar3 @myvar2 
    /echo MyVar3 is now @myvar3 
/return @myvar3

User avatar
blueninja
a grimling bloodguard
a grimling bloodguard
Posts: 541
Joined: Thu Aug 28, 2003 7:03 am
Location: Göteborg, Sweden

Post by blueninja » Tue Oct 07, 2003 7:37 pm

The question about changing a local parameter from a different sub would depend on wether the arguments are passed as copies of the variable or as a pointer to the variable. If it's passed as a copy you can't change it like that, if it's passed as a pointer you wouldn't have to put it on the return statement.

Compare it to defining a parameter to a C function with or without a * in front of it. If it's got the * you can access the memory location of the variable defined in the calling function directly, so you can change it. If it doesn't have the * you get a local copy of the variable that you could change but it will be destroyed as soon as you return from the subroutine so the calling function won't ever see that you changed it.

Basically, don't put anything after /return. If it doesn't work you have to make the variables global.

Hope I make sense, I just got home from the pub.. :)

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

Post by Mckorr » Tue Oct 07, 2003 8:05 pm

I understood you. Guess we need to wait on a better explanation from Plaz before we find out.

I know /return can return a value. I would thing that if you did

/call somesub(@alpha,@beta,@gamma)

then to return values to alpha, beta, and gamma you would have to

/return @delta @epsilon @zeta

Which would set alpha equal to delta, etc.
MQ2: Think of it as Evolution in action.

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 8:36 pm

OK, this works, all the way up to the /return @myvar3

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 @myvar3
/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
Output:

Code: Select all

[Tue Oct 07 20:31:46 2003] [MacroQuest] Current mana is 6845
[Tue Oct 07 20:31:46 2003] [MacroQuest] Mana for Spirit of Wolf is 40
[Tue Oct 07 20:31:46 2003] [MacroQuest] MyVar3 value is 0
[Tue Oct 07 20:31:46 2003] [MacroQuest] Values passed to TestSub are 6845, 40, 0
[Tue Oct 07 20:31:46 2003] [MacroQuest] MyVar3 is now 6845.00
[Tue Oct 07 20:31:46 2003] [MacroQuest] MyVar3 is now 6805.00
[Tue Oct 07 20:31:46 2003] [MacroQuest] Mana after casting Spirit of Wolf is 0

User avatar
dont_know_at_all
Developer
Developer
Posts: 5450
Joined: Sun Dec 01, 2002 4:15 am
Location: Florida, USA
Contact:

Post by dont_know_at_all » Tue Oct 07, 2003 9:21 pm

Return values are in $return, AFAIK.