0 votes
by (790 points)

Maybe I missed it in the documentation for SugarCube (v.2.21.0) but I can’t seem to find how to add the content of an array.

Specifically, I have a list of perks and want to track how many the player can take.

<<set $perk to {
adaptimmune: 0,
cortex: 0,
miasma: 0,
}>>

(There’s more to it, but I’m asking this from my bloody phone)

after a player chooses one , the number is set to 1. Basically Boolean with 0/1 instead of false/true.

can I then somehow set, say, “$temp” to the sum of the array without having to go through and test each sub-object within the array and add them to $temp one at a time?

2 Answers

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

What you've described above is not an array, it's a generic object.  Thus, to get what you want, you'd do this:

<<set $temp = 0>>
<<for _val range $perk>>
	<<set $temp += _val>>
<</for>>

That will loop through all of the properties of the $perk object, and add each of the properties' values to the $temp variable.  See the "range form" of the <<for>> macro for details.

Hope that helps!  :-)

by (790 points)
edited by
Not an array...? As you've probably guessed, I'm a fairly novice programmer, so I was going by what other people had previously called it. Thank you. =^^=

NOW the universe shall TREMBLE before the player's hungry, hungry spacecraft...creature...thing...

I'll try this out once I get to work (can you tell my job is a bit boring?) but it looks like what I need.

EDIT: nnnope. No matter what the contents of object, it returns 0.

Edit 2: Got it! Had to set $temp to $temp + _val rather than setting it to += _val
by (44.1k points)
edited by

The original works fine for me:

<<set $perk to {
	adaptimmune: 0,
	cortex: 1,
	miasma: 3
}>>
<<set $temp = 0>>
<<for _val range $perk>>
	<<set $temp += _val>>
<</for>>
Temp = $temp

outputs "Temp = 4".  Doing <<set $temp += _val>> is the same thing as doing <<set $temp = $temp + _val>>.

Anyways, if you want to understand the difference between arrays (which are a specific kind of object) and generic objects, I have a page which explains them and their use in Twine/SugarCube: Arrays vs Generic Objects

(Note: Currently that link may not work because it uses DriveToWeb to access files on my Google Drive, and DriveToWeb is apparently down at the moment.  If it's still not working when you try it, try again later and hopefully it will be back up by then.)

by (790 points)
Thank you!

And thank you for your patience. =o.o=
–1 vote
by (250 points)

I've done it like this (where $ra is your array of values):

<<set $total = 0>>\
<<for _i to 0; _i lt $ra.length; _i++>><<set $total=$total+$ra[_i]>><</for>>\

Not sure if this is what you were asking?

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