Param syntax

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

Zengore
decaying skeleton
decaying skeleton
Posts: 4
Joined: Mon Jan 26, 2004 9:34 pm

Param syntax

Post by Zengore » Tue Jan 27, 2004 3:03 pm

is @Param0 @Param1 @Param2 ... etc the only syntax we can use to get our parameters? was simply wondering if there was something more along the lines of @Param(1) so that i could use a variable in there like @Param(x), thanks ahead of time for any help with this.

I am simply wondering cause i am working on my first macro and that would help a lot, if not oh well, i'll be posting it here after it's done.

Gumby
a ghoul
a ghoul
Posts: 99
Joined: Sat Jan 24, 2004 5:27 pm

Post by Gumby » Tue Jan 27, 2004 4:17 pm

I don't know of anything natively by default that would allow you to do it; not only not sure what the benefit would be really but if you have to pass *that* many parameters to a subroutine it may be better to find them out in the subroutine itself.

If it'd make it cleaner I suppose you could simply pass all of the parameters as an array and get the functionality you're looking for, but you'd have to build the array beforehand and I don't see where it'd save ya that much time nor give much benefit.

Another method, though I've never tried this in a script I just played with it out of curiosity and it works (though people are going to scream don't do it probably and I'd likely agree with them as it's confuzzling as hell and bad practice imo to boot), you could do something like this (skeleton code):

/varset Param0 blah
/varset ParamCounter 0 //loop this for multiple variables

/varset Sneaky Param
/varcat Sneaky @ParamCounter

/varset RealVariable @@Sneaky

/echo @RealVariable

resulted in 'blah'

Again not sure what this saves you but it'd allow you to varset based upon @@Sneaky rather than Param0/1/2/3 directly. Probably your only real implementation on this would be to use a for or goto loop, and have an array of known variable names in the subroutine that you wanted to set whose indices corresponded with ParamCounter outlined above; otherwise it'd really suck with /if's to setup the variables in the sub you were passing to.

G

Gumby
a ghoul
a ghoul
Posts: 99
Joined: Sat Jan 24, 2004 5:27 pm

passing an array to a subroutine?

Post by Gumby » Wed Jan 28, 2004 7:20 pm

Fiddled today with my own code in trying to pass an array and it flat out went to hell in an oxcart. I'm guessing as it's not a simple variable that the Sub routines can't pass it directly? Tried it several ways and couldn't do much cept pass an individual element of the array.

So... open question for anyone,

Any easy way to pass an array (not element by element) to a Subroutine, or do you need to pass a dummy variable which holds the array name and simply copy everything from the real array into the temporary one with a for loop, and then likely write it all back when you're done making modifications in the sub?

Thanks,

G

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 » Thu Jan 29, 2004 2:08 am

Arrays are global if I am not mistaken, so why pass it to a Sub?

As far as @Param0 etc... those are defaults assigned when you aren't explicitly using your own variable names that get passed into a sub:

For example:

Code: Select all

/call MySub @firstVar @secondVar @thirdVar

Sub MySub
   /echo Values passed to this sub are: @Param0 ... @Param1 ... @Param2
/return
and

Code: Select all

/call MySub @firstVar @secondVar @thirdVar

Sub MySub(First,Second,Third)
   /echo Values passed to this sub are: @First ... @Second ... @Third
/return
Do the same thing, but in different ways.

Gumby
a ghoul
a ghoul
Posts: 99
Joined: Sat Jan 24, 2004 5:27 pm

Post by Gumby » Thu Jan 29, 2004 5:56 am

Wassup wrote:Arrays are global if I am not mistaken, so why pass it to a Sub?
Have a subroutine that does anything to multiple arrays at different times over the course of a script; single array, sure just list the name of it and mod it in the sub and have a nice day.

Multiple arrays though, if I can't pass an array in some fashion then I wind up having to write multiple subroutines, one for each specific array. Hadn't worked with arrays much tilll recently when I needed some form of a data structure, may try the gimpy aliasing I goofed off with in the earlier post or something along those lines or pass a name and Sub ArrayCopy it. Was being lazy =/. Hrm, may try to source it anyway, amusing to try if nothing else for me.

Thanks

G

ml2517
a grimling bloodguard
a grimling bloodguard
Posts: 1216
Joined: Wed Nov 12, 2003 1:12 am

Post by ml2517 » Thu Jan 29, 2004 8:59 am

Not sure if this will work but maybe try passing the actual name of the variable of your array. Try something like this: (not at home or I'd try this)



/call Test "Array1"

Sub Test(ArrName,Element)
/echo @@ArrName(@Element)
/return

Gumby
a ghoul
a ghoul
Posts: 99
Joined: Sat Jan 24, 2004 5:27 pm

Post by Gumby » Thu Jan 29, 2004 3:20 pm

ml2517 wrote:Not sure if this will work but maybe try passing the actual name of the variable of your array. Try something like this: (not at home or I'd try this)



/call Test "Array1"

Sub Test(ArrName,Element)
/echo @@ArrName(@Element)
/return
Yeah, that was the aliasing I was referencing in my previous post with @@Sneaky.

Did try it this morning with:

Code: Select all

Sub Main

   /declare testarray array
   /varset testarray(0) blah
   /call ArrayTest testarray

/return

Sub ArrayTest(AliasTest)
   /echo @@AliasTest(0)
/return
echo'd out 'blah' successfully. Going to probably cause headscratching on other's first read through at the code, but that's what comments are for :). Certainly a lot better than duplicating subroutines.

Thanks for the replies,

G

ml2517
a grimling bloodguard
a grimling bloodguard
Posts: 1216
Joined: Wed Nov 12, 2003 1:12 am

Post by ml2517 » Thu Jan 29, 2004 3:30 pm

I guess I should have read the earlier posts before I posted. :) Ah well I'd just woken 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 » Thu Jan 29, 2004 6:36 pm

Code: Select all

Sub Main 

   /declare testarray array 
   /varset testarray(0) blah 
   /call ArrayTest testarray 

/return 

Sub ArrayTest(AliasTest) 
   /echo @@AliasTest(0) 
/return
Should also work with

Code: Select all

Sub Main 

   /declare testarray array 
   /varset testarray(0) blah 
   /call ArrayTest testarray 

/return 

Sub ArrayTest
   /echo @@Param0(0) 
/return
However, the example you used is much more readable and easier to understand, which is why I love the new variables so much.