0 votes
by (8.8k points)

In my game, I've created a set of NPCs with randomly generated traits.

<<set _npcNameList to [
  'Minsc',
  'Jaheira',
  'Dynaheir',
  'Imoen',
  'Dorn',
  'Viconia']>>

<<set $npc1 to {},
      $npc1.name to _npcNameList.pluck(),
      $npc1.strength to random(3,18),
      $npc1.intelligence to random(3,18),
      $npc1.dexterity to random(3,18)>>

<<set $npc2 to {},
      $npc2.name to _npcNameList.pluck(),
      $npc2.strength to random(3,18),
      $npc2.intelligence to random(3,18),
      $npc2.dexterity to random(3,18)>>

<<set $npc3 to {},
      $npc3.name to _npcNameList.pluck(),
      $npc3.strength to random(3,18),
      $npc3.intelligence to random(3,18),
      $npc3.dexterity to random(3,18)>>

This creates three NPCs with a random name and random ability scores.

What I'd really like is for the game to display a comparative analysis of the new NPCs.  Something like:

Dorn is the strongest.  Dynaheir and Imoen are the most intelligent.  Imoen is the most dextrous.

I could do this with a million <<if>> statements, but is there a more intelligent way to go about this?

1 Answer

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

Unless you want to delve into Javascript, the easiest way is probably with a widget. The following widget will set the array $chosen to contain those npcs that have the highest of the requested value:

<<widget compare>><<nobr>>

<<switch $args[0]>>

<<case "strength">>
<<set _ability to [$npc1.strength, $npc2.strength, $npc3.strength]>>

<<case "intelligence">>
<<set _ability to [$npc1.intelligence, $npc2.intelligence, $npc3.intelligence]>>

<<case "dexterity">>
<<set _ability to [$npc1.dexterity, $npc2.dexterity, $npc3.dexterity]>>

<</switch>>

<<set _select to _ability[0]>>
<<set _hero to [$npc1, $npc2, $npc3]>>
<<set $chosen to [$npc1]>>

<<for _i to 1; _i lt _hero.length; _i++>>
	<<if _ability[_i] > _select>>
		<<set _select to _ability[_i]>>
		<<set $chosen to [_hero[_i]]>>
	<<elseif _ability[_i] == _select>>
		<<set $chosen.push(_hero[_i])>>
	<</if>>
<</for>>

<</nobr>><</widget>>

Now you can do something like this:

<<compare "strength">><<print $chosen[0].name>>

How exactly you then translate the array into text depends on how exactly you want that text to look like.

by (8.8k points)

Very clever, thanks idling.  smiley

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.
...