+1 vote
by (190 points)

Hello,

So, I'm trying to work with an rpg system that instead of using numeric attributes, uses words. Like Fudge or Storyteller, except that this is trickier to work with when it comes to leveling up. So basically I wanted a way to write "your strength is $att.strength", where $att.strength is a number,  but display "your strength is poor". I've been trying to create a macro for that, but I have very little experience with javascript and couldn't find a tutorial. So I've tried my hand using the sugarcube api macro reference, and this is what i've come up with:

Macro.add('stats', {
	handler : function () {
		try {
			var attribute = this.args[0]; 
			if (attribute == 1) { 
				return this.output("poor");
			}
		} catch (err) {
			return this.error;
			}
		}
});

Except this doesn't write anything if I use <<stats '1'>>. I'm not sure what to try next.

1 Answer

+1 vote
by (62.7k points)
edited by

There's some stuff definitely wrong with the macro, and we can talk about that, but first, all you really need is a data structure. Using a macro here is a bit of overkill, in my opinion. Try something like: 

<<set $strength to {
    value : 0,
    print : [
        'poor', 
        'average', 
        'good', 
        'great'
   ]
}>>

<<set $strength.value to 1>>

<<set $stats to {}>>
<<set $stats.strength to $strength.print[$strength.value.clamp(0, 3)]>>

<<print $stats.strength>>

 

by (190 points)
I hadn't even thought about this, ahahaha. I had tried a widget, but couldn't make it work. Thank you so much for your help <3
by (23.6k points)

Wouldn't it have to be

<<set $stats.strength to $strength.print[$strength.value.clamp(0, 3)]>>

in this case?

by (62.7k points)
You're right. Thanks for the catch. I'll edit the answer.
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.
...