Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

[Sugarcube 2.16.0] Replace text during page render

edited March 2017 in Help! with 2.0
I've been using the standard link replace which I love once I got the hang of it. But I was wondering how I could go about modifying the contents of an element during render.

What I mean is say I'm hoping to accomplish something like the following:
<p>This is just a simplified example of the coding scenario.</p>

<p id="action-text"></p>

Select an option:
<hr style="padding:0px; margin:0px" />
<br />
<ul style="padding-top:0px; margin-top:0px">

	<<if random(1) eq 0>>
		<<replace "#action-text">>
			Hello World!
		<</replace>>
	<<else>>
		<<replace "#action-text">>
			Goodbye World!
		<</replace>>
	<</if>>

	<li>
		<<link "Brush your teeth (0:05)">>
			<<replace "#action-text">>
				scrub scrub scrub
			<</replace>>
			
			<<addmins 5>>
		<</link>>
	</li>
</ul>

However I understand that won't work because the macros will be processed before the html is rendered. I saw in the manual the task objects and a postrender which I'm assuming is something I would need to use in a situation like this. But I'm not sure where to start.

Any suggestions, dumbed down examples, or general hand holding would be appreciated.


Edit:

I just came across twinery.org/forum/discussion/comment/15366/#Comment_15366. I'm not sure how crazy the situation still is, but what I'm hoping to accomplish is to group together like content, in this case content that takes place in a single room for ease of organization and updating. It doesn't feel like it would make sense to use a link replace and I was trying to keep the html generic with replaces to keep the code from bloating or being overly complicated.

In my current layout I have a set place where text is displayed to the player and I'd like to keep re-using it if possible so long as the player is in that passage.

Comments

  • For anyone that might be in this situation themselves. It seems the answer is to make new passages and use a goto macro. It actually seems to be working well enough for the moment. But I can't seem to find a way to ensure that nothing continues to execute on the passage the goto macro is encountered on which is something the documentation warns about.

    For now it's not really an issue. I just hope it doesn't become one down the road.
  • edited March 2017
    You don't need the first replace macro, you can just directly insert the random text into action-text.

    For example:
    This is just a simplified example of the coding scenario.
    
    <span id="action-text"><<= ['Hello World!', 'Goodbye World!'].random()>></span>
    
    Select an option:
    <hr style="padding:0px; margin:0px" />
    
    <ul style="padding-top:0px; margin-top:0px">\
    <li>\
    <<link "Brush your teeth (0:05)">>\
    <<replace "#action-text">>scrub scrub scrub<</replace>>\
    <<addmins 5>>\
    <</link>>\
    </li>\
    </ul>\
    


    Anything option to consider is the <<include>> macro:
    <span id="action-text"><<include "RandomPassage">></span>
    

    This will take the contents of RandomPassage and insert it into the action-text.
Sign In or Register to comment.