0 votes
asked by (2.1k points)

I'm trying to get both a name and the numbers of my list of data to display in the inventory.

Using the above method I can get the numbers to list, but not the actual sub-variable name.

As an example my variable is like this:

 

<<set $pcStats to {

str: 71,
agi: 60,
int: 59,

}>>

and my StoryCaption code is like this:

<<set _txt = "Attributes:">><<for _skillValue range $pcStats>>\
	<<set _txt += "\n# " + _skillValue>>
<</for>>_txt

 Yet it displayed:

1. 71

2. 60.

3. 59.

So I tried this:

<<set $pcStats to {

str: "Strength" 71,
agi: "Agility" 60,
int: "Intelligence" 59,

}>>

Hoping the inventory would display:

Strength  71
Agility  60
Intelligence  59

But got variable errors. :(

1 Answer

+1 vote
answered by (26.7k points)

You just need to display the key for each value:

<<set _txt = "Attributes:">><<for _skill, _skillValue range $pcStats>>\
	<<set _txt += "\n# " + _skill + ": " +_skillValue>>
<</for>>_txt

That will set _skill equal to the key for each key-value pair in the $pcStats object.  Note that objects are unordered lists, so the order you get in one browser may be different than the order you get in a different browser.

Also, this:

str: "Strength" 71

didn't work because the value you're trying to set on the key property isn't valid.  However, you could have done this instead:

str: "Strength " + 71

That would turn the value into a single string, instead of being a string value and a number value as you had previously.

Enjoy!  :-)

commented by (2.1k points)

Oh I see, thanks.

Ok I just did that and it displayed:

Strength: Strength = 51
Agility: Agility = 62
Intelligence: Intelligence = 64

Hmmm.

commented by (22.3k points)
It works perfectly fine on my end and I have no idea what you could have possibly done to get the result you're getting unless you for some reason set $pcStats.str to "Strength = 71".
commented by (2.1k points)
Sorry, the numbers are fine, it's the issue of it displaying the same word twice.
commented by (22.3k points)

That's what I'm talking about. The only way I can think of how the "Strength ="  etc. would get mixed in there is if you incorrectly set your properties to strings. Try a few math operations:

<<set $pcStats.str += 2>>$pcStats.str

And see if your math works out fine.

commented by (2.1k points)
edited by
Alright, I got it working after some more trial and error. Thanks for the suggestions, very helpful and got me to where I want it to be I think.
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.
...