0 votes
by (180 points)
I'm trying to figure out whether / how you can reference a data map using variable.

As an example, imagine you have four data maps representing a typical D&D-style adventuring party (e.g., $char1, $char2, $char3, and $char4), with each data map containing info about that character (name, race, class, etc etc).

So, for example, if you wanted to display character one's race (as I understand it), you might use:

(print: $Char1's Race)

My question is, if you wanted to, say, create a passage in which you allow the player to choose a specific character and a data item to display, using variables to record the player's choices, how would that be done?  

So let's say, for example, that you write such a passage, and the player's choice of character and attribute are recorded using the variables $charChoice and $attChoice .

You'd think one could then display the answer using something like

(print: $charChoice's $attChoice)

and that you could get the same result as above (i.e., printing character one's race) by setting $charChoice to "$char1" and $attChoice to "Race" .

However, when I have tried this, I get an error message. It's become a bit frustrating, so I thought I'd reach out to the community to see if there is a way to do this, and (if so) how.  If there's no ready solution, that at least would be a resolution of sorts.  I'm a complete and utter newcomer to Twine specifically and programming generally, so patience is appreciated ...

Thank you in advance!

1 Answer

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

The Array documentation explains you can use brackets ( ) to evaluate an expression to determine the array index/position to use, and a (story or temporary) variable ca be used as an expression.

(set: $array to (a: "one", "two", "three"))
(set: _index to 2)

2nd element of array: (print: $array's (_index))

... and you can use this technique with a datamap.

(set: $map to (dm: "first", "abc", "second", "def", "third", "hij"))
(set: _key to "second")

value of map key "second": (print: $map's (_key))

... and now that you know that the only issue left is how to access the character related story variables.

The simplest solution to that is to replace your character related story variables with a datamap that contains each of those variables as keys/value pairs.

(set: $party to (dm:
	"Char1", (dm: "Name", "value", "Race", "value1"),
	"Char2", (dm: "Name", "value", "Race", "value2"),
	"Char3", (dm: "Name", "value", "Race", "value3")
))
(set: _char to "Char2", _attr to "Race")

2nd character's race: (print: $party's (_char)'s (_attr))

If you still want to keep the individual character related story variables then you could use a slight variation on the above solution by temporary creating the party variable within the passage you want to display the values in.

(set: _party to (dm:
	"Char1", $Char1,
	"Char2", $Char2,
	"Char3", $Char3
))
(set: _char to "Char2", _attr to "Race")

2nd character's race: (print: _party's (_char)'s (_attr))

 

by (180 points)
Many thanks for the quick and comprehensive response!
by (159k points)
If you found my answer satisfactory could you please mark the question as answered, that way it will change for blue to green in the question list and people will know that the question has an acceptable answer.
by (180 points)
Of course!  Apologies, new to the site.  

I checked "best answer" next to your original reply -- hopefully that's what you were looking for.
...