Macro.Return ain't returning jack

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
ziltmilt
Dragon
Posts: 331
Joined: Sun Apr 29, 2007 9:28 pm
Contact:

Macro.Return ain't returning jack

Post by ziltmilt »

Sorry for all the questions recently. I'm stuck on another problem, a function call that's not getting a return value

Code: Select all

		
[h: macro.return = total]
Total result = {total}, Wild = {WildResult}, Dice rolled:  {sDice}, returning {macro.return}
here's my output:
Total roll: , '' '' Total result = 15, Wild = 4, Dice rolled: ,4,6, returning 15
So, I"m getting a value into macro.return, but that value isn't making it to the calling macro

Code: Select all

[h: mimic = 0]
[h: mimic = RollDice("3D+1",1)]

Total roll:  {mimic}
I've got the function 'RollDice' defined in onCampaignLoad, and it's on a Lib: token. Plus 'Allow Players to Edit' is unchecked. Any idea what I'm missing? I can attach my cmpgn file if that'll help.

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Macro.Return ain't returning jack

Post by aliasmask »

macro.return is usually the last line in a macro which is called by another macro who returns a value and in the line after a call to a macro. For example:

Code: Select all

<!-- macro1 -->
[H: macro.return = 10]

Code: Select all

<!-- macro2 -->
[macro("macro1@lib:token"): ""]
[H: result = macro.return]
[R: result]

ziltmilt
Dragon
Posts: 331
Joined: Sun Apr 29, 2007 9:28 pm
Contact:

Re: Macro.Return ain't returning jack

Post by ziltmilt »

I guess I'm not using a macro. I've defined a UDF, so maybe I'm not clear on how to get a UDF to return its value. I thought it was via macro.return. The point of failure looks like it's in the calling macro

Code: Select all


[h: mimic = 0]
[h: mimic = RollDice("3D+1",1)]
Total roll:  {mimic}

'mimic' just isn't getting a value from RollDice(), but I can see the value being assigned to macro.return in the RollDice code. Is there another way to get a UDF to return a value? I've got another cmpgn file I built a few years ago which uses macro.return for its UDFs and they work great. So, I'm really at a loss as to why this is failing.

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Macro.Return ain't returning jack

Post by aliasmask »

UDF are function and the values are returned in the place of the function call. I'm not familiar with the dice expressions you're using or the arguments, but here an example:

Code: Select all

<!-- rollDice -->
[H: numDice = arg(0)]
[H: dieSides = arg(1)]
[H: result = roll(numDice,dieSides)]
[H: macro.return = result]

Code: Select all

<!-- calling macro -->
[H: roll1 = rollDice(2,6)]
[H: roll2 = rollDice(1,4)]
[R: strformat("2d6 = %{roll1}; 1d4 = %{roll2}")]

ziltmilt
Dragon
Posts: 331
Joined: Sun Apr 29, 2007 9:28 pm
Contact:

Re: Macro.Return ain't returning jack

Post by ziltmilt »

In the code snippets I provided previously, I didn't have any dice expressions. What I'm looking for is a list of things you need to do to get a UDF to work, and from your answer, it sounds like a UDF has to use macro.return in order to return a value.

- onCampaignLoad needs a call to DefineFunction with the UDF name
- the UDF has to have the 'Allow Players to Edit Macro' unchecked
- the UDF leaves 'apply to selected tokens' unchecked
- the UDF has to include a line assigning the returned value to macro.return
- any calling macro can call the UDF by making a variable equal to the function name, just like any built-in function

Could I be missing anything else? IF a UDF outputs to the chat window, does that interfere with its being able to return a value? I just wrote a small test UDF and it returns a value fine. So, I'm confused why 1 UDF will return something and another won't.

ziltmilt
Dragon
Posts: 331
Joined: Sun Apr 29, 2007 9:28 pm
Contact:

Re: Macro.Return ain't returning jack

Post by ziltmilt »

Aliasmask, actually, I think I do see a return value, but this is weird. If I'm passing '5D' (which means for this game to roll 5 dice), I get back a bunch of commas. If I'm passing 1D, I only get a pair of single quotes. macro.return is getting a number, but the calling macro is getting back punctuation!

Very weird. If you don't mind, I'll post the entire UDF here, which I've copied into a new macro named RollDice2:

Code: Select all


[h: skillCode = arg(0)]
[h: bWild = arg(1)]
[h: pips = 0]
[h: numDice = 0]
[h: totRoll = 0]
[h: ended = endsWith(skillCode, "D")]
[h, if (ended > 0 ), CODE: {
		
		[h: numDice =  substring(skillCode,0,length(skillCode)-1)]
		[h: pips = 0]
		
	};
	{
		
		[h: numDice = substring(skillCode,0,length(skillCode)-3)]
		[h: pips = substring(skillCode,length(skillCode)-2), len(skillCode)-1)]
		
	}]

[h: sDice = ""]
[h: totRoll = pips]
[h: newRoll = 0]
[h: highDie = 0]
[h: WildResult = 0]
[h: loops = 0]
[h: loops = numDice]
[h, if(bWild>0), CODE: {		
		[h: loops = numDice - 1]
		[h: WildResult=d6e]		
	}]
[WHILE(loops > 0), CODE: {

	[h: loops = loops - 1]
	[h: newRoll = d6]
	[h: sDice = sDice + "," + newRoll]
	[h: highDie = if(newRoll > highDie, newRoll, highDie)]
	[h: totRoll = totRoll + newRoll]	
	}]
[h: totRoll = totRoll + WildResult]
[if (getLibProperty("WildDieOne","Lib:Utility") == 2 && WildResult == 1), CODE: {
		[h: totRoll = totRoll - 1 - highDie]
		Wild Die was a 1; Your roll has been reduced by {1 + highDie}
	}]

[if (getLibProperty("WildDieOne","Lib:Utility") == 3 && WildResult == 1), CODE: {
		[g: "Wild Die was 1, but was included in total result. Introduce a complication!"]
		
	}]

[h: macro.return = totRoll]


User avatar
Full Bleed
Demigod
Posts: 4736
Joined: Sun Feb 25, 2007 11:53 am
Location: FL

Re: Macro.Return ain't returning jack

Post by Full Bleed »

ziltmilt wrote:Aliasmask, actually, I think I do see a return value, but this is weird. If I'm passing '5D' (which means for this game to roll 5 dice), I get back a bunch of commas. If I'm passing 1D, I only get a pair of single quotes. macro.return is getting a number, but the calling macro is getting back punctuation!
*snip*

Code: Select all

[WHILE(loops > 0), CODE: {

	[h: loops = loops - 1]
	[h: newRoll = d6]
	[h: sDice = sDice + "," + newRoll]
	[h: highDie = if(newRoll > highDie, newRoll, highDie)]
	[h: totRoll = totRoll + newRoll]	
	}]
The WHILE loop above is generating the 5 commas in the 5D example (i.e. because of the five loops).

And:

Code: Select all

[if (getLibProperty("WildDieOne","Lib:Utility") == 2 && WildResult == 1), CODE: {
		[h: totRoll = totRoll - 1 - highDie]
		Wild Die was a 1; Your roll has been reduced by {1 + highDie}
	}]

[if (getLibProperty("WildDieOne","Lib:Utility") == 3 && WildResult == 1), CODE: {
		[g: "Wild Die was 1, but was included in total result. Introduce a complication!"]
		
	}]
Is generating 2 commas.

Hide the WHILE and IF lines.

Try using Wiki: broadcast() for your chat output so it will work in the hidden IF statements.
Maptool is the Millennium Falcon of VTT's -- "She may not look like much, but she's got it where it counts."

ziltmilt
Dragon
Posts: 331
Joined: Sun Apr 29, 2007 9:28 pm
Contact:

Re: Macro.Return ain't returning jack

Post by ziltmilt »

OK, I see where the punctuation is coming from but why on earth would the UDF return commas? I'm telling it to return a number.

ziltmilt
Dragon
Posts: 331
Joined: Sun Apr 29, 2007 9:28 pm
Contact:

Re: Macro.Return ain't returning jack

Post by ziltmilt »

This is freakin' weird. When I take out this line in the UDF:

Code: Select all


Total result = {totRoll}, Wild = {WildResult}, Dice rolled:  {sDice}

Then the UDF returns totRoll via macro.return. And the calling function can display the value. So, I'm guessing that having a UDF output your return value via the chat window prevents the UDF from returning a value somehow, but I don't see why.

At any rate ... the function now returns a value. So again, many thanks for your help. But, if you could shed some light on this behavior for me, that'd be cool, too.

ziltmilt
Dragon
Posts: 331
Joined: Sun Apr 29, 2007 9:28 pm
Contact:

Re: Macro.Return ain't returning jack

Post by ziltmilt »

From further playing around, I believe outputting anything to the chat window inside a UDF messes up its ability to return a value.

Hope I'm not sounding like a broken record, but thank you thank you thank you !!! Because of your help, I hope to have a useful framework to share with this fantastic community.

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Macro.Return ain't returning jack

Post by aliasmask »

This is how I would write your function. I would make sure to define it with no output.
||| RollDice2 |||

Code: Select all

[H: skillCode = arg(0)]
[H: bWild = arg(1)]

[H: wildDieOne = getLibProperty("WildDieOne","Lib:Utility")]

<!-- parse skillCode -->
[H: numDice = listGet(skillCode,0,"D")]
[H: pips = listGet(skillCode,1,"D")]
[H, if(json.isEmpty(pips)): pips = 0]

<!-- roll Wild Die -->
[H, if(bWild), code: {
   [H: WildResult = d6e]
   [H: loop = numDice -1]
   [H, if(wildDieOne == 2 && WildResult == 1): fumble = 1; fumble = 0]
   [H, if(wildDieOne == 3 && WildResult == 1): complication = 1; complication = 0]
};{
   [H: loop = numDice]
   [H: fumble = 0]
   [H: complication = 0]
   [H: WildResult = 0]
}]

<!-- roll dice and save rolls -->
[H: sDice = "[]"]
[H, for(index,0,loop): sDice = listAppend(sDice,1d6)]
[H: sDice = listSort(sDice,"d")]

<!-- check for fumble and get total roll -->
[H, if(fumble), code: {
   [H: highDie = listGet(sDice,0)]
   [H: wDice = listDelete(sDice,0)]
   <!-- trick to sum a list -->
   [H, if(! json.isEmpty(sDice)): totRoll = eval(strformat("sum(%{wDice})")) - 1 + pips; totRoll = pips -1]
   [H: broadcast(strformat("Wild Die fumble; High die (%{highDie}) -1 applied."))]
};{
   [H, if(! json.isEmpty(sDice)): totRoll = eval(strformat("sum(%{sDice})")) + pips + WildResult; totRoll = pips + WildResult]
}]

<!-- check for complication -->
[H, if(complication): broadcast("Wild Die was 1. Introduce a complication!","gm")]

[H: macro.return = totRoll] 
I don't like to mix functions with output, so I used broadcast for the purpose of an error message. But I notice you want more than just totRoll from the function. In that case, you should either have that function do all the output or pass that info back to calling macro in a json object.

So, instead of the macro.return line, do this. Also, no need to set ignoreOuput to 1 because the function is handling the output now:

Code: Select all

[R: strformat("Total result = %{totRoll}, Wild = %{WildResult}, Dice rolled:  %{sDice}")]
When you call roll dice all you need is [R: RollDice2(skillCode,bWild)]

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: Macro.Return ain't returning jack

Post by wolph42 »

ziltmilt wrote:From further playing around, I believe outputting anything to the chat window inside a UDF messes up its ability to return a value.

Hope I'm not sounding like a broken record, but thank you thank you thank you !!! Because of your help, I hope to have a useful framework to share with this fantastic community.
I could have told you that, and you're correct. To be exact:

1. when the macro has ANY output, including <!-- comment that does not show up --> then it will return that output and nothing else
2. UNLESS its a UDF AND you have 'ignore output' turned on
3. when you use macro.return in macro B, then you can retrieve that value in macro A that calls macro B, as 'macro.return' still contains that value in macro A. Hence you can have a macro return 2 seperate things: one as 'chat' and one through macro.return
B:

Code: Select all

[h:macro.return = 6]
[r:5]
A:

Code: Select all

[h:output1 = B()]
[h:output2 = macro.return]
[r:output1]<br>[r:outpu2]
will return

Code: Select all

5
6
4. when the macro NO output AND you use macro.return THEN the macro outputs the value assigned to macro.return.

Post Reply

Return to “Macros”