0 votes
by (170 points)

I have a datamap set up like this to give each name a number value:

(set: $people to (dm:
"Alice", 0,
"Bob", 0,
"Celia", 0,
"David", 0,
"Emma", 0)

Except that it has 32 names, not 5. The numbers increase over the course of the game.What I'm trying to make is a result screen at the end of the game, where the name with the highest total is displayed along with the associated value.

I want it to find the maximum value of $people, put it into $result, then print each name of $people whose value matches $result.

So if Alice has a value of 12, and that's the highest, it should print "Alice: 12". Or, if Bob and Celia are tied with 14, if should print "Bob, Celia: 14".

I'm not sure how to do this. If anyone could help me out, it would be greatly appreciated.

I'm using Harlowe 2.1.0 in Twine 2.2.0.

1 Answer

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

Try the following

(set: $people to (dm: "Alice", 12, "Bob", 14, "Celia", 14, "David", 0, "Emma", 0))

(set: $result to 0)
(set: $whom to (a:))
(for: each _name, ...(datanames: $people))[
	(set: _value to $people's (_name))
	(if: _value > $result)[
		(set: $result to $people's (_name))
		(set: $whom to (a: _name))
	(else-if: _value is $result)[
		(set: $whom to it + (a: _name))

result: $result
whom: $whom

It uses the (datanames:) macro to get the names of all the individuals, and it uses a (for:) macro to loop through each of those names.