Creating a stored output property from a foreach.

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
Xaelvaen
Dragon
Posts: 498
Joined: Wed Aug 31, 2011 9:49 pm
Location: Somewhere between Heaven and Hell

Creating a stored output property from a foreach.

Post by Xaelvaen »

So, as the title indicates, I'm trying to figure out how to take some html created by a [Foreach] and store it in a property to call upon later with broadcast(). I have no idea if this is possible, or how to approach it. I tried to simply wrap the foreach in ' ' so it would 'stack' with the quotations of the html around it, but that creates a table basically saying 'foreach' lol. I've never really worked with strformat or anything like that, so not sure if it could help in this case.

Any points in the right direction, or even if it is possible, would be appreciated.
"An arrogant person considers himself perfect. This is the chief harm of arrogance. It interferes with a person's main task in life - becoming a better person." - Leo Tolstoy

User avatar
jackolas
Cave Troll
Posts: 28
Joined: Tue Apr 16, 2013 8:19 am
Location: Netherlands
Contact:

Re: Creating a stored output property from a foreach.

Post by jackolas »

I'm not sure if the "broadcast()" function is the right way to do this. but can't know that for sure without knowing more about what you try to do.

anyway. for the stringformat i use something like this for my weapon attack (you need to write your own)

Code: Select all

[H: RNA_OutputFormat = '
<table border="1" cellpadding="0">
	<td><table border="0" cellpadding="0" cellspacing="0">
		<tr Align="Center"><b>Source:</b></tr>
		<tr Align="Center"><img src="%{OP_SourceImg}"></img></tr>
		<tr Align="Center">%{OP_SourceName}</tr>
	</table></td><td><table border="0" cellpadding="1">
		<tr bgcolor="green">
			<td width="4"></td><td Align="Center" style="color:white"><b>%s</b></td><td width="4"></td>
		</tr><tr>
			<td width="4"></td><td Align="Center">%s</td><td width="4"></td>
		</tr><tr>
			<td width="4"></td><td Align="Center">%s</td><td width="4"></td>
		</tr><tr>
			<td width="4"></td><td Align="Center">%s</td><td width="4"></td>
		</tr><tr>
			<td width="4"></td><td Align="Center">%s</td><td width="4"></td>
		</tr>
	</table></td><td><table border="0" cellpadding="0" cellspacing="0">
		<tr Align="Center"><b>target:</b></tr>
		<tr Align="Center">%{OP_targetImg}</tr>
		<tr Align="Center">%{OP_targetName}</tr>
	</table></td>
</table>
']
and i call it like:

Code: Select all

	[R: strformat(RNA_OutputFormat,
	OP_Type,
	OP_Weapon,
	OP_Attack,
	OP_Damage,
	OP_Ammo
	)]
Every "%s" is a variable and you can add them when calling the strformat
In theory you can put both in and foreach loop

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

Re: Creating a stored output property from a foreach.

Post by wolph42 »

this is really silly but it shows what you're asking for:

Code: Select all

[h:txt = "<html>"]
[h:nums = "0,1,2,3,4"]
[h,foreach(num,nums): txt = txt + ", " + num]
[h:txt = txt + "</html>"]
[h:broadcast(txt)] 
I use this method a lot to build dynamic forms, tables, etc. obviously you can also use strformat in the mix, but I thought to keep it simple.

User avatar
jackolas
Cave Troll
Posts: 28
Joined: Tue Apr 16, 2013 8:19 am
Location: Netherlands
Contact:

Re: Creating a stored output property from a foreach.

Post by jackolas »

that is actually a very good example. think i was overthinking this :P

User avatar
Xaelvaen
Dragon
Posts: 498
Joined: Wed Aug 31, 2011 9:49 pm
Location: Somewhere between Heaven and Hell

Re: Creating a stored output property from a foreach.

Post by Xaelvaen »

Haha - Wolph to the rescue (of my sanity - still awful at strformat after however many years I've been at this). Thank you both for the input; so happy to not have to suddenly dive into strformat.
"An arrogant person considers himself perfect. This is the chief harm of arrogance. It interferes with a person's main task in life - becoming a better person." - Leo Tolstoy

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

Re: Creating a stored output property from a foreach.

Post by aliasmask »

Depends how the html is created. If the author just dumps it to the chat then there is one trick you can use. Any output from a function can be put in to a variable if called as a function or UDF. For example:

Code: Select all

<!-- my html macro -->
<b>stuff goes here</b>
Running this macro just goes to the chat. But if you create a UDF...

Code: Select all

<!-- onCampaignLoad -->
[H: defineFunction("am.myHTML","myHTML@"+getMacroLocation())]

Code: Select all

<!-- save output and broadcast -->
[H: htmlOutput = am.myHTML()]
[H: broadcast(htmlOutput)]
Then you control when and where you want the output to go.
Attachments
lib_test.rptok
(30.01 KiB) Downloaded 33 times

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

Re: Creating a stored output property from a foreach.

Post by wolph42 »

y're welcome. Note that stringformat can take many forms which is why it can be so painstakingly hard to manage. One form however which is very easy to use and very usefull is the one where you use variables e.g.

Code: Select all

[h:me = "Tim the Enchanter"]
[h:target = "Harmless bunny"]
[r:strformat("%{me} is really scared of %{target}")] 
Last edited by aliasmask on Wed Aug 30, 2017 6:25 pm, edited 2 times in total.
Reason: added {}'s

User avatar
Xaelvaen
Dragon
Posts: 498
Joined: Wed Aug 31, 2011 9:49 pm
Location: Somewhere between Heaven and Hell

Re: Creating a stored output property from a foreach.

Post by Xaelvaen »

Code: Select all

[h: outputList=macro.args]

[h: open="<html><body bgcolor=#cccccc><table border=1 cellpadding=1 cellspacing=0>"]
[h: txt="<tr><td border=1 cellpadding=1 cellspacing=0>"]

[h,foreach(output,outputList) : txt=txt +output +"</td></tr>"]
[h: endTxt=add(open,txt,"</table></html>")]

[h: broadcast(endTxt)]
That's what I ended up doing for it, and it worked smashingly, so thank you again Wolph.

As for strformat, in your example Wolph, I assume 'me' and 'target' could be other called variables, like getName() etc? It doesn't just have to be text, right? If so, that seems a lot easier than all the "Blah Blah " +variable +" Blah."

@Aliasmask
I'm still working on figuring out UDFs. At the moment, I don't use any, but I've been very interested in picking it up. My Anarchy framework is almost finished, so that'll be my next 'target' to learn I reckon.
"An arrogant person considers himself perfect. This is the chief harm of arrogance. It interferes with a person's main task in life - becoming a better person." - Leo Tolstoy

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

Re: Creating a stored output property from a foreach.

Post by wolph42 »

As for strformat, in your example Wolph, I assume 'me' and 'target' could be other called variables, like getName() etc? It doesn't just have to be text, right? If so, that seems a lot easier than all the "Blah Blah " +variable +" Blah."
erm... you're quite good at mixing stuff up...
I assume 'me' and 'target' could be other called variables
YES
like getName() etc?
NO. getName() is a function, NOT a variable. You could use [anotherName = getName()] and then use %anotherName inside the strformat.
If so, that seems a lot easier than all the "Blah Blah " +variable +" Blah."
YES, that was the point of my example. It makes it a LOT easier to create output strings and makes much more eligible code.

---------------
IF you want to use functions like getName() directly you get into the second stage of using strformat and that is the %s:

Code: Select all

<!-- assuming  "Harmless bunny" is selected! -->
[h:switchToken("Tim the Enchanter")]
[r:strformat("%s is really scared of %s", getName(), getSelectedNames())] 
%s is substituted by whatever is after the ", " in order of whatever is provided. In contrast to the earlier example, in this case you *can* use functions (next to variables).

another example

Code: Select all

<!-- assuming  "Harmless bunny" is selected! -->
[h:switchToken("Tim the Enchanter")]
[h:text = "is really scared of"]

[r:strformat("%s %{text} %s", getName(), getSelectedNames())] 
OR
[r:strformat("%s %s %s", getName(), text, getSelectedNames())] 
all the examples give exactly the same output.

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

Re: Creating a stored output property from a foreach.

Post by aliasmask »

Xaelvaen wrote:@Aliasmask
I'm still working on figuring out UDFs. At the moment, I don't use any, but I've been very interested in picking it up. My Anarchy framework is almost finished, so that'll be my next 'target' to learn I reckon.
There's nothing really to them. I even made a starter token so any macro you create is automatically a UDF as well. Just read the comments on the lib token as well as in the macros.
Attachments
starter lib token.rptok
(168.82 KiB) Downloaded 40 times

User avatar
Xaelvaen
Dragon
Posts: 498
Joined: Wed Aug 31, 2011 9:49 pm
Location: Somewhere between Heaven and Hell

Re: Creating a stored output property from a foreach.

Post by Xaelvaen »

wolph42 wrote: erm... you're quite good at mixing stuff up...

Story of my life mate lol. By 'called variable', tokenName=getName() was exactly what I was referring to (function to call, store as variable). I'm just awful-bad at explaining things. Also awful-bad at remembering specific terminology. Thank you for the clarification; already started using strformat replacements. Also, thank you for the second use of strformat. I was thinking I'd have to go away from it for output where I needed to use a function in them, because "Blah Blah " +getName() +"Blah" is a bit faster than making a separate (new) variable just for the sake of using strformat. So your new examples helped as well =)

@Aliasmask
Thank you, I've downloaded it, and now I'll give it a poke and read up on 'em.
"An arrogant person considers himself perfect. This is the chief harm of arrogance. It interferes with a person's main task in life - becoming a better person." - Leo Tolstoy

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

Re: Creating a stored output property from a foreach.

Post by wolph42 »

it appeared I made a typo in my earlier messages, but someone (probly AM) corrected them. Its strformat("%{variable}") not strformat("%variable") in strformat.

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

Re: Creating a stored output property from a foreach.

Post by aliasmask »

I've actually had issues with using %s in strformat. I recall there being two things that can muck things up so I just stick with %{varName} as much as I can. I know I've mentioned them in some really old posts but I can't think of what they are at the moment. I think one has to do with ' being in the variable and the other is when % in the string where you have to use %% to get a literal % when using %s in string. When you don't use %s then you don't need to use %%. All these really boil down to is needing an extra step to format the text which can be a pain depending on how dynamic the text is.

User avatar
Xaelvaen
Dragon
Posts: 498
Joined: Wed Aug 31, 2011 9:49 pm
Location: Somewhere between Heaven and Hell

Re: Creating a stored output property from a foreach.

Post by Xaelvaen »

Then perhaps I should just stick to using strformat when I already have variables place in the code, and 'simple' formatting when I need to call something new. Regardless, thank you both as always =)
"An arrogant person considers himself perfect. This is the chief harm of arrogance. It interferes with a person's main task in life - becoming a better person." - Leo Tolstoy

Post Reply

Return to “Macros”