Howdy, Stranger!

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

How do I print a variable that changes based on what happens later on in the passage? (Sugarcube)

Confusing question, I know. I was having difficulty phrasing it myself. The example below demonstrates my question clearer:

Example:
//This printed variable is at the top of the passage//
<<print $enemy_HP>>

//Then later on in the lower parts of the passage, there are <<if>> statements. e.g.://
<<set $random_event to random(1,10)>>
<<if $random_event is > 5>><<set $enemy_HP to $enemy_HP - 10>><</if>>

I guess what I'm trying to say is, in my story, a lot of random events may happen during the passage, but they wont dynamically update the "$enemy_HP" variable because the printed value is at the TOP of the passage. I want the $enemy_HP variable to display a value AFTER the rest of the passage has been rendered, but still remain at the top for aesthetic purposes. And I don't want to rerender the passage because that would execute all the $random_events again. Is that possible?

Like, I have player_HP values in the StoryCaption UI sidebar, and those values display POST rendering of a passage. I want that to happen with my enemy_HP values as well.

Hope my question was clear.

Comments

  • You can use the <<replace>> macro to dynamically update a section a passage that you have given an ID to, as demonstrated in the macro's example.

    But as explained in the Need help with addclass macro you can't update a HTML element until after it has been rendered (displayed).

    You will need to use some technique to delay the execution of your <<replace>> macro until after the passage has been displayed, like using a modified version of the PassageDone example shown in the other thread.

    1. Place the following within your passage. (I named the passage Combat)
    //This printed variable is at the top of the passage//
    <span id="enemy-HP"><<print $enemy_HP>></span>
    
    <<set $random_event to random(1,10)>>
    <<if $random_event > 5>>
    	<<set $enemy_HP to $enemy_HP - 10>>
    <</if>>
    
    2. Create a PassageDone special passage containing the following:
    <<if passage() === "Combat">>
    	<<replace "#enemy-HP">><<print $enemy_HP>><</replace>>
    <</if>>
    
  • Awesome! Thanks, Greyelf.
Sign In or Register to comment.