Sorting by object key and then printing an array

If by “later on in the story” you mean during the Reader/Player’s progress through your project, then I should note the following…

The special setup variable is designed to store Stateless values (and static methods), which is a fancy way of saying values that don’t change after they have been initialised. The content of the setup variable is not part of Progress History, which means it isn’t persisted in Saves.

So if you intend to change the price of “the hammer” during the Reader/Player’s progress through your project, then (at least) that price needs to be somehow stored in a Story Variable so its value will be tracked by Progress History and persisted in Saves.

One way to do this is to have two Objects:

  • a Stateless object for storing the parts of an item definition that don’t change.
  • a Stateful object for storing the parts of an item definition that do change.
setup.items = {
	"Claw Hammer" : {
		"Name": "Claw Hammer",
		"itemType" : "Tool",
		"Price" : 2,
		"Weight": 5
	},
	/* other item definitions...*.
};

State.variables.prices = {
	"Claw Hammer" : 2,
	"Hand Saw": 5,
	"Nail": 1,
	"Bucket": 3
};

When you want to access one of the Stateless properties of the “Claw Hammer” (like its Weight) in a Passage you would reference the setup.items object…

Hammer Weight: <<= setup.items["Claw Hammer"].Weight >>

…and when you want to access that item’s Price you you would reference the $prices Story variable defined earlier…

Hammer Weight: <<= $prices["Claw Hammer"] >>

…and when the price of the hammer needs to be changed at some point in the story, you would use the $prices variable like so…

/* increase hammer's price by 10  */
<<set $prices["Claw Hammer"] += 10>>
1 Like