SugarCube - Adding multiple 'booster' values etc to Variables with one variable value?

Twine Version: 2.3.5
Story Format: 2.30

This is a bit complex but hopefully not too complex for SC.

Assuming I had the following values for my variables at a screen like this:

<<set $pcStats to {
fly: 57,
evade: 62,
pursue: 64,
morale: 50,
}>>

With the morale of a crew or the poison reducing skills etc. Is there a handy-dandy way to modify multiple stats depending on the value of a single variable.

If so, would the ‘modifier’ code look something like:

<<nobr>>

<<if $pcStats.morale gte to 50>> Ship morale is ok! Your crew are going about their business routinely.>> <<set $pcStats.fly to +=2>> <<set $pcStats.evade to +=3>> <<set $pcStats.pursue to +=1>>

<<elseif $pcStats.morale gte to 30>> Ship morale is low! Your men will not fight or fly like they normally do!<<set  $pcStats.fly to -=10>> <<set $pcStats.evade to -=10>> set $pcStats.pursue to -=10>>

<<else>> Ship morale is terrible! Your men will not fight as well now!<<set  $pcStats.fly to -=18>> <<set $pcStats.evade to -=18>> set $pcStats.pursue to -=18>><</if>>

<</nobr>>

Yes, but doing it directly is not much better than what you already have.

<<set _val = -10, Object.assign($pcStats, { fly: $pcStats.fly + _val, evade: $pcStats.evade + _val, pursue: $pcStats.pursue + _val })>>

That uses the Object.assign() method to assign multiple values at once.

You could improve that by putting something like this in your JavaScript section:

setup.modStats = function (val) {
	var stats = State.variables.pcStats;
	return Object.assign(stats, { fly: stats.fly + val, evade: stats.evade + val, pursue: stats.pursue + val });
};

then all you’d need to do is this:

<<set setup.modStats(-18)>>

and that will subtract 18 from the .fly, .evade, and .pursue properties.

Enjoy! :grinning:

Thanks man, helpful stuff.
But would it be better to label something in that code to tell me what it is when I have loads of different stuff that does multi-modifiers?

Like calling the ModStatsMorale or something?

That all depends on what your code needs. If you only need it for one thing, then you only need one name. If you need it for different things, then you either want to have different versions with different names, or you want to add another parameter to make that one function do multiple things.

You need to decide that for yourself.

Well I’ll probably have to get 3 or 4 of this type of thing. So will need to label 3 or 4 of them differently I guess?

What I mean is which part of the Java code can be relabeled within the code itself? Or should I just make a little reference bit to tell me that way?

There isn’t one answer here. There are dozens, if not hundreds, of ways you could do this. You need to decide which method makes the most sense based on how you’re going to use it.

The JavaScript to modify depends on what you want to do.

What I gave you should be enough to figure out how to do what you want. Try things out, and if you’re still stuck, then let us know what you’re trying to do.

Have fun! :grinning:

1 Like

I’m just trying to fathom how to get all that to talk to the ‘init’ file so it loads up in the background as the game starts for the first time?

Is that possible?

I’m not sure of how to get the Java code to do that as a morale @ 30 do this, morale @ 60 do that etc.

Would I have to use Sugarcube language variables in the init to ‘talk’ to the Javascript coding?

Huh? I think you’re trying to make things more complicated than they actually are.

Like I said, you just put the code in your JavaScript section. After that you can access the function just like I showed in the example which followed that code.

First of all, it’s not “Java”, it’s “JavaScript”. Much like “car” and “carpet”, the two words don’t mean the same thing.

If you want the code to do different things based upon various values in your JavaScript, then you’d just use a JavaScript if” statement.

I don’t even know what that means or why you keep referring to “the init”. You’d access the function pretty much the same way, regardless of where you did it (unless you called it from outside of the SugarCube scope, in which case you’d have to add “SugarCube.” to the front).

If you take a look at the example code I gave you, you can see how the JavaScript code uses the SugarCube State.variables object to access the SugarCube story variables from JavaScript, and in the example which followed you can see how you can then simply refer to that function by name within any SugarCube macros, such as <<set>>, in order to use that function.

It’s much simpler than you apparently think it is.

Hopefully that clears things up! :grinning: