+1 vote
by (8.9k points)

Hello, everybody!  So I have this widget, which helps an NPC decide what to do.

<<widget "npcdecision">>
  <<set _d100 to random (1,100)>>
  <<if _d100 gte 66>>
    <<set $npcDecision to "npc_attack">>
    <<elseif _d100 gte 33>>
    <<set $npcDecision to "npc_defend">>
    <<else>>
    <<set $npcDecision to "npc_surrender">>
<</widget>>

I'd now like to run another widget, named either npc_attack, npc_defend, or npc_surrender as appropriate.

I know I could achieve this using <<if>> statements like so:

<<if $npcDecision eq "npc_attack">>
  <<npc_attack>>
  <<elseif $npcDecision eq "npc_defend">>
  <<npc_defend>>
  <<elseif $npcDecision eq "npc_surrender">>
  <<npc_surrender>>
<</if>>

However, this seems inelegant.  It would be really good if I could just make the game run the widget with the same name as $npcDecision.  However, when I tried:

<<$npcDecision>>

It just prints the value of $npcDecision enclosed in << >>s.

Is it possible to do what I want, or should I just stick to the <<if>> statements?

1 Answer

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

Not directly, no.  I'd suggest using either the <<switch>> macro, rather than <<if>>, or the Stupid Print Trick™.

 

Example for <<switch>>:

<<switch $npcDecision>>
<<case "npc_attack">><<npc_attack>>\
<<case "npc_defend">><<npc_defend>>\
<<case "npc_surrender">><<npc_surrender>>\
<</switch>>

 

Example for the Stupid Print Trick™:

<<= '<<' + $npcDecision + '>>'>>

That works because you're concatenating the necessary angle brackets to the value of $npcDecision before it's output by the <<print>> macro (via its <<=>> shortcut), the output of which is also processed as markup which is what allows the trick to work.

by (8.9k points)
That should be called the Awesome Print Trick(TM).  Thanks, Exile.
...