Updating a In-Game Sidebar Journal with Old Variable Data

Twine Version: 2.7.0

So Microsoft’s Copilot was being handy at first and gave me the idea for a journal in the sidebar, so I implemented it as a passage that only contains $journalData. Then in the special passage I have at top: <<link "Journal">> <<script>> Dialog.setup("Journal", "Journal"); Dialog.wiki(Story.get("Journal").processText()); Dialog.open(); <</script>> <</link>>
To display it. Now for my question:

I can set Journal Data to delete its contents or += to append. It supports links and variables, but I’m having trouble simply putting data in there that stays the same despite the variable being updated later. Say I have this button:

<<button "ADD HP">><<set $journalData += "\nEra: $eraTime.era, Y: $eraTime.year, D: $eraTime.day: On this day you gained HP from the machine! Debug! From $player.hp to ">><<set $player.hp += 1>><<set $journalData += " $player.hp! \nNew Line.">><<replace "#story-caption">><<include "StoryCaption">><</replace>><</button>>

How can I save or print this data without it being the most up-to-date variable data?

Hi there,

Saving new values of variables only happens when you change passages (and create another “state” for your savefile). However, you can still print updated version of a variable in multiple ways:

  • <<replace>> or other DOM macros
  • using custom macros like Cycy’s Liveupdate
  • reloading the passage with passage() (like <<goto passage()>> or <<replace '.passage'>><<include passage()>><</replace>>

If the information is printed on the SideBar or other non-passage element:

  • UIBar.update() (to update the whole the sidebar)
  • <<replace>> for any element on the screen

If the information is going to be displayed in a Popup (with the Dialog API), that will be updated every time the Popup opens.

Since your $journalData seems to be a regular variable, you can just do:

  • $journalData
  • <<print $journalData>>

inside the the passage called: Journal

1 Like

It’s hard to explain so I’ll show the result:

Era: 1, Y: 0, D: 0: On this day you gained HP from the machine! Debug! From 10 to 10!
New Line.
Era: 1, Y: 0, D: 0: On this day you gained HP from the machine! Debug! From 10 to 10!
New Line.
Era: 1, Y: 0, D: 0: On this day you gained HP from the machine! Debug! From 10 to 10!
New Line.
Era: 1, Y: 0, D: 0: On this day you gained HP from the machine! Debug! From 10 to 10!
New Line.
Era: 1, Y: 0, D: 0: On this day you gained HP from the machine! Debug! From 10 to 10!
New Line.

I haven’t included code for updating time once I press the debug button, but I want an alternative to placing $player.hp inside $journalData, as it just shows the most up-to-date result.

Then don’t put what’s essentially a bit of naked variable markup within the string that will get interpolated later. Instead, concatenate the current value to the string. For example:

<<set $journalData += "… From " + $player.hp + " to ">><<set $player.hp += 1>><<set $journalData += $player.hp + "! \nNew Line.">>

Additionally, a suggestion. Keeping the entries as separate members of an array would give you more formatting options when displaying them. You’d also have an easier time manipulating them if necessary. Storing them as one giant string has some issues.

Thank you so much, the solution was simple but avoided me. I don’t like how messy a simple one-line journal entry has become, but it works. I will keep the suggestion about an array in mind, I am still engineering many parts of the story and don’t know if a simple one-string journal would be better for simplicity or to go all out.