0 votes
asked by (130 points)

I'd like to make a generic shop page in Harlowe. I want to initialise it with a list of stock, then print information about the stock.

(set: $_gun to(dm: "name", "gun", "price", 150, "stock", 1))
(set: $_catapult to(dm: "name", "catapult", "price", 30, "stock", 1))
(set: $_armour to(dm: "name", "armour", "price", 50, "stock", 3))

I feel like datamaps are a good way to do this, but I'm a novice in the world of data structures.

Please browse our wares.
(set: $shoplist to (ds: "$_gun", "$_catapult", "$_armour"))
(for: each _shopitem, ...$shoplist)[(print: _shopitem's name) : £(print: _shopitem's price)]

When I use (print: $_gun's name) it works. When I try to do the same thing by looping through an array or dataset, it doesn't. :(

Thanks for any help!

1 Answer

+2 votes
answered by (139k points)

notes:
1. I strongly suggest not naming your variables like so $_gun because the underscore has special meaning (it is used to indicate a temporary variable) and you're relying on the story format's TwineScript parser to correctly guess that in this particular case the underscore is actually part of the story variable's name.
2. Personally I would use an Array over a Data-Set because as I understand things they are generally lighter weight and perform better during looping due to how they are stored in consecutive memory.

The main issue with your code is how you are passing the items to the Data-Set (ds:) macro, try the following instead.

(set: $gun to (dm: "name", "gun", "price", 150, "stock", 1))
(set: $catapult to (dm: "name", "catapult", "price", 30, "stock", 1))
(set: $armour to (dm: "name", "armour", "price", 50, "stock", 3))

Please browse our wares.
(set: $shoplist to (ds: $gun, $catapult, $armour))
(for: each _shopitem, ...$shoplist)[\
	(print: _shopitem's name) : £(print: _shopitem's price)
]

 

commented by (130 points)
Ugh. I really thought I'd tried that. Thank you very much!
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...