0 votes
by (2.3k 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
by (44.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!  :-)

by (2.3k points)

Thanks for the fast moves on that one.

I just tried your suggestion

 

str: "Strength " + 71

It worked but it displays

1.   Strength71

Instead of:

1.   Strength 71

I've tried adding &nbsp; but no dice alas.

EDIT. Wait, I've got it. I added a space in like so....

"Strength "

I'll get this Sugar Cube mostly figured out yet! :)

by (23.6k points)
Keep in mind that with this you solution your str property will be a string - meaning you can't use this for math operations or the like.
by (2.3k points)

Really? So I'd be unable to use it for a dice-roll:

 

Roll a <<print "$dexterity">> or under<br>
<span id='dice-outcome'></span>
<span id='dice-button'>
<<button 'Open Fire!'>>
	<<set $roll to random(0, 100)>>	
	<<if $roll gte $strength>>
	<<replace '#dice-outcome'>>
			You rolled $roll. The enemy is quicker and thrusts in a blade, you are injured! Lose 10HP.<br>
			<<set $CurHP -= 10>>
	<<if $CurHP lt 1>>
	<<goto [[Death1]]>><</if>>
		<</replace>>
		<<else>>
		<<if $roll lte $strength>>
			<<set $enemyHP -= 17>>
				<<replace '#dice-outcome'>>
				You rolled $roll. The machineman loses 17 HP.<br>
		<</replace>>
 	<<if $enemyHP lte 0>>
		<<replace '#dice-outcome'>>
			You rolled a $roll. The machineman collapses.<br>[[Continue]]
		<</replace>>
		<<replace '#dice-button'>><</replace>> /% remove button %/
		<</if>>
		<</if>>
		<</if>>
<</button>>
</span>

I guess I'd need a separate set of code that is identical but not a string or something then?

by (23.6k points)

At that point you'd have to consider whether it might be easier to just create the text manually instead of forcing yourself into using a <<for>> loop.

Strength: $pcStats.str
Agility: $pcStats.agi
Intelligence: $pcStats.int

Is much shorter and easier to write than what you are trying to do.

by (2.3k points)
edited by

Hey there! @idling

Using this method where would the numbers data go? Like this?

Strength: $pcStats.str + 71,

 

by (23.6k points)

No - you would just have your initial setup:

<<set $pcStats to {

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

}>>

And in your storycaption you'd put:

Strength: $pcStats.str
Agility: $pcStats.agi
Intelligence: $pcStats.int
by (2.3k points)

Oh I see, thanks.

Ok I just did that and it displayed:

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

Hmmm.

by (23.6k 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".
by (2.3k points)
Sorry, the numbers are fine, it's the issue of it displaying the same word twice.
by (23.6k 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.

by (2.3k 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.
...