+1 vote
by (8.9k points)

My game has an Inventory screen, which the player can access from the sidebar at any time.  So far, so good.

If they access it from a page that sets variables, then return, the game (of course) sets all the variables again.  What's the best way to prevent this?

My first impulse was to create an IF statement along the following lines:

<<if not lastVisited("Bar") is 1>>
  Set the variables

However, that doesn't solve the problem for _temporary variables.

I experimented with a <<return>> link, but that set the variables again.

I'm now thinking about opening the Inventory in a new tab with no back button.  But is there a better way?

3 Answers

+1 vote
by (8.6k points)
selected by
Best answer

Does your game allow you to set up the inventory using the Dialog system, kinda like this (JavaScript code)?


... with the <<Inventory>> widget providing all the inventory UI (alternatively, <<include ...>> an inventory passage).

If you can use this method, you can open up inventory all day long and the passage won't change once you went back.

+1 vote
by (63.1k points)
If you're incrementing or decrementing something, that is, not setting it to a discreet value, then in my opinion the variables should be set using the link, not just in the passage where they can be rerun. So basically, user interaction is the safest place when you want to, say, give the player money or upgrade their stats.
+1 vote
by (159k points)

By default the <<return>> macro moves the story History forward one moment to the last passage shown before the current one, which is why the <<set>> macros within that last passage are being applied twice.

On the other hand the <<back>> macro moves the story History backward one moment to the just before the last passage was shown and then shows that last passage, this results in the <<set>> macros within that last passage only being applied once.

The following TWEE notation example demonstrates both of the above behaviours.

:: Home
<<set $counter += 1>>counter: $counter

:: StoryInit
<<set $counter to 0>>

:: StoryMenu

:: Inventory
counter: $counter



  ... if you visit the Inventory passage and use the Return link then the counter will increase, if you use the Back link then the counter will stay the same value.

You need to use the <<back>> macro instead of the <<return>> macro.

by (8.6k points)
Caveat: If you use anything that's non-deterministic - random() without a pre-seeded PRNG, the current system time and so on - <<back>> won't help you either. The <<set ...>> macros will still run only once, but they will very likely yield different values.
by (8.9k points)
Thanks, that was a helpful comment.
by (8.9k points)
Thanks greyelf, that taught me the difference between <<return>> and <<back>>.  Chapel's suggestion of doing this sort of thing in <<links>> is very clever!  Akjosch's answer looks like the simplest bet.  Everybody here is great!