@metronome: Couldn't find anything built in to help unfortunately, but this is how I'd do it the plain JavaScript way:
var sum = $gameVariables._data.filter(function(val, i) { return i >= 1 && i <= 100;}).reduce(function(currentSum, val) { return currentSum + val;}, 0);$gameVariables.setValue(101, sum);Nowhere near as nice as it would be in Ruby unfortunately. You could squish it all into one line if you really had to:
$gameVariables.setValue(101, $gameVariables._data.filter(function(val,i){return i>=1 && i<=100}).reduce(function(sum,val){return sum+val},0))Edit:
A shorter-looking alternative (no difference, except maybe performance?):
var sum = $gameVariables._data.reduce(function(currentSum, val, i) { return i >= 1 && i <= 100 ? currentSum + val : currentSum;}, 0);$gameVariables.setValue(101, sum);
Hi,
thanks for the reply.
Using the js function you provided, I did script call to -let's say- sum all the item from item 1 to item 100 in party possession using codes below:
$gameVariables.setValue(101, $gameParty.numItems(function(val,i){return i>=1 && i<=100}).reduce(function(sum,val){return sum+val},0))And it returns with Syntax Error: undefined is not a function hmmmm.
may be I should set all the variables to 0 first right when the game starts?
EDIT:
I think the code I provided is wrong, I forgot that after $gameParty.numItems it should be (dataSomething), not a (function).
And btw,
the codes you provided me comes with error: unexpected tokens.
You are putting too many semicolons perhaps?
And this one:
$gameVariables.setValue(101, $gameVariables._data.reduce(function(sum,val,i){return i>=1 && i<=100 ? sum+val : sum}, 0))Always returns me 0 no matter what.......
Thanks~