Help with creating a macro that takes a number and returns a word

+1 vote
asked Jun 15, 2017 by giucorreias (190 points)


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
answered Jun 15, 2017 by Chapel (51,430 points)
edited Jun 19, 2017 by Chapel

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 : [

<<set $strength.value to 1>>

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

<<print $stats.strength>>


commented Jun 15, 2017 by giucorreias (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
commented Jun 15, 2017 by idling (10,670 points)

Wouldn't it have to be

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

in this case?

commented Jun 19, 2017 by Chapel (51,430 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.