Mysterious output quirks - vanishing plus signs and spaces

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
User avatar
Venatius
Dragon
Posts: 256
Joined: Mon Feb 16, 2009 7:12 pm

Mysterious output quirks - vanishing plus signs and spaces

Post by Venatius »

Hello! I've hit the darnedest quirks in my recent macro coding that I was hoping perhaps someone could account for. Both are called as UDFs.

Case 1: The disappearing space.
Consider this function, whose purpose is to hide output if the variable "private" in the calling macro is set to 1.

Code: Select all

[r, if(private == 0), code: {
	[r: " "+input]
};{
	[s,g,r: " "+input]
}]
It's defined onCampaignLoad with [h: defineFunction("privateOutput", "PrivateOutput@this", 0, 0)]
A space is included so that it can be formatted not to leave a gap if information is hidden. For instance, if you want "Rolling." vs "Rolling 12." to appear, the idea is you would input:
Rolling[r: privateOutput(12)].
and if private == 1, it should output "Rolling." to everyone but the GM(s) and the invoking user, who see "Rolling 12."
Here's the weird part, though:

Code: Select all

[h: private = 0]Rolling[r: privateOutput(12)]
returns: "Rolling12." However, if private is set to equal 1, the space renders correctly, displaying "Rolling 12." for the user and just "Rolling." for everyone else. Is there any reason why R: would eat the space while S,G,R does not?

Case 2: The disappearing plus sign.
I run into a lot of cases where the rules of a game, by convention, prepend things with a plus sign if it's positive. I wanted to make a UDF to automate this. Thus, getSign:

Code: Select all

[h, if(arg(0) >= 0): result = "+ "+arg(0); result = "- "+abs(arg(0))]
[h: macro.return = result]
It's defined onCampaignLoad with [h: defineFunction("getSign", "GetSign@this")]. Here's the problem. This is a workaround. The original code was this:

Code: Select all

[h, if(arg(0) >= 0): result = "+"+arg(0); result = arg(0)]
[h: macro.return = result]
So in theory, if it's 0 or greater, it should return the first argument with a + prefixed to it. The problem is, no matter how I struggle with it, it doesn't. The number simply returns as-is. getSign(5) returns 5, not +5. I found it didn't "eat" it if I included a space (and thus set it up to include one for negatives just for visual consistency). Is there any workaround for this that doesn't mandate the extra space?

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Mysterious output quirks - vanishing plus signs and spaces

Post by aliasmask »

I'm not sure about the space problem, but the + disappears once you assign the result to a value because it's converted. If you want to display a + you either do it during the output or use an html entity. I recommend using strformat method for your output.

For example:

Code: Select all

[H: value = 5]
[R: strformat("%+d",value)]

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Mysterious output quirks - vanishing plus signs and spaces

Post by aliasmask »

As for the space, if there is nothing before it it may get lost with html formatting, which is what chat is. If you want to force a space I recommend and html entity  

User avatar
Venatius
Dragon
Posts: 256
Joined: Mon Feb 16, 2009 7:12 pm

Re: Mysterious output quirks - vanishing plus signs and spaces

Post by Venatius »

aliasmask wrote:
Sun Jul 28, 2019 3:54 pm
As for the space, if there is nothing before it it may get lost with html formatting, which is what chat is. If you want to force a space I recommend and html entity  
Thanks very much! This has the privateOutput() function working just fine. I'm perhaps a little unclear as to how to best utilize your advice on the other one. Here's a quick, improvised use case example:

Code: Select all

[h: modifiers = '{"Flanking":2,"Pants around ankles":-4}']
Your modifiers are:
[r, forEach(current, modifiers), code: {
	[r: current] [r: getSign(json.get(modifiers, current))].
}]
(It's inexact, but it hopefully provides context.) In this case, the ideal output would be "Your modifiers are: Flanking +2, Pants around ankles -4." My hope with creating getSign() was to have a succinct and easy way to just slap down the appropriate + or -. But so far I can't seem to find any way to retrieve it from the function with the leading + intact. I did try modifying the code in a rough attempt to comply with the suggestion:

Code: Select all

[h, if(arg(0) >= 0): result = strformat("%+d",arg(0)); result = arg(0)]
[h: macro.return = result]
But that didn't quite do it, possibly because I misunderstood the intended usage. It could always be done manually without making a UDF. I was just aiming for convenience. I guess it's not really much harder to do [r, if(number >= 0): "+"][r: number] than just [r: getSign(number)].

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Mysterious output quirks - vanishing plus signs and spaces

Post by aliasmask »

Code: Select all

[h: modifiers = '{"Flanking":2,"Pants around ankles":-4}']
[H: output = "Your modifiers are: "]
[H: modList = ""]
[H, forEach(current, modifiers), code: {
   [H: modList = listAppend(modList,strformat("%s %+d",current,json.get(modifiers,current)),", ")]
}]
[R: output = output + modList]
Output wrote:Your modifiers are: Flanking +2, Pants around ankles -4

User avatar
Venatius
Dragon
Posts: 256
Joined: Mon Feb 16, 2009 7:12 pm

Re: Mysterious output quirks - vanishing plus signs and spaces

Post by Venatius »

aliasmask wrote:
Sun Jul 28, 2019 4:52 pm

Code: Select all

[h: modifiers = '{"Flanking":2,"Pants around ankles":-4}']
[H: output = "Your modifiers are: "]
[H: modList = ""]
[H, forEach(current, modifiers), code: {
   [H: modList = listAppend(modList,strformat("%s %+d",current,json.get(modifiers,current)),", ")]
}]
[R: output = output + modList]
Output wrote:Your modifiers are: Flanking +2, Pants around ankles -4
Gotcha. Looks like I'll need to do it in some manner like this without the UDF then. Thanks very much for your assistance!

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Mysterious output quirks - vanishing plus signs and spaces

Post by aliasmask »

As a rule of thumb, I always put my output in to a variable before outputting it. There are several benefits to this like readability, memory storage, data manipulation, portability of output...

User avatar
Venatius
Dragon
Posts: 256
Joined: Mon Feb 16, 2009 7:12 pm

Re: Mysterious output quirks - vanishing plus signs and spaces

Post by Venatius »

That makes sense. I might be on still too amateur a tier to make much productive use of that, but I can see the logic in it.

Post Reply

Return to “Macros”