0 votes
asked by (150 points)

I am trying to build an inventory system based on datamaps. Now, if the player drops an item I have to remove it from the datamap. I tried it in a similar way to an array:

 [(set: $inventory to it-(dm: $i, $inventory's $i))]

Where $i is the name string of the item.

Does not work. The Message I get is:

I can't use - on a datamap.►
I tried to perform an operation on some data, but the data's type was incorrect

I can't find anything about removing an item from a datamap in the documentation. How do I do it?

1 Answer

+1 vote
answered by (125k points)
selected by
 
Best answer

You can us the (move:) macro to achieve the result you want.

(set: $inventory to (dm:
	"first key", "first value",
	"second key", "second value",
	"third key", "third value"
))

Inventory before: (print: $inventory)

(set: $key to "second key")
(move: $inventory's ($key) into _buffer)

Inventory after: (print: $inventory)

NOTES:
1. You will need a variable to use as an 'into' target of move, I suggests using a temporary variable for that purpose unless you actually want to store/use that value. 

2. Based on the documentation the correct way you use a value stored within a variable as the 'key' of an Array or a Datamap reference is to wrap that variable reference (an expression) within parenthesises like so:

(set: $key to "second key")

BAD reference to ''second key'' within datamap:
(print: $inventory's $key)

GOOD reference to ''second key'' within datamap:
(print: $inventory's ($key))

3. I also thought that a method similar to that you used in your original example would work, and I was equally surprised that it didn't.

commented by (150 points)
Thank you. It works.

Also thanks for the hint on correct reference. I'm pretty new to Harlowe, just downloaded twine two days ago. So I don't know all recommendations and best practices yet.
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.
...