0 votes
by (280 points)

I want to use two macros in same time.

In case of Korean language, postposition paricle changes with last spell of the word

(similar with An apple, A car)

so I made an Macro that check last word and return appopriate postposition word like,

<<script>>
Macro.add('PPAttach', {
	handler: function() {
		var len = this.args[0].length-1
		var str = (this.args[0].charAt(len) - 0xAC00)%28;
		if(str>0)
			{str = this.args + "을"}
		else{str = this.args + "를"}
		$(this.output).wiki(str);
	}
});
<</script>>

 

second, I made random word chage widget like,

<<nobr>>
<<widget "Rword">>
<<set $R1 to either("Apple","Banana","Orange")>>/*sample text insted of Korean Language*/
$R1
<</widget>>
<</nobr>>

 

and when I use macros in passage like,

<<PPAttach <<Rword>>>>

Script processes like <<PPAttach "<<Rword" >> +">>"

when use like this,

<<PPAttach "<<Rword>>" >>

I suppose that the script processses that ">"is the last word

How can I use random word system, very many type of, with attaching custom macro?

I tried to create random word script and get PPAttach macro in it, but I can't find realistic example but API

1 Answer

0 votes
by (159k points)

Widget's don't return a value, they instead inject the output they generate into the HTML fragment buffer associated with the processing of the current Passage's contents. This buffer then gets injected the HTML structure of the current page.

This is why you can't pass the output of a widget as the argument of a macro.

You could create a JavaScript function you generate the random String value and then use that function's return value as the argument of your macro. You can define this function on the special setup object within your project's Story Javascript area.

setup.Rword = function () {
	return either("Apple","Banana","Orange");
};

... and use this new function as a macro argument (using back-quote markup) within your Passage like so.

<<PPAttach `setup.Rword()`>>

 

note: You used a <<script>> macro to define your custom macro, normally such custom macros are defined within a project's Story Javascript area.

by (280 points)
thank you for rapid answer!

then, where can I see more examples about setup. object for more complecated usage?

linked document has too little informations..
by (63.1k points)
The setup object is quite literally just an empty namespace (or object) for user code, nothing more to say than that.
by (280 points)
ok, thank you.

I'll try someway how
by (44.7k points)

Basically, you just need to do something like this if you're writing in JavaScript:

setup.propertyName = "value";

or something like this if you're writing in Twine code:

<<set setup.propertyName = "value">>

That creates a property on the setup object called propertyName which is assigned the string value of "value".

If it helps, I have a primer on data types and how to use them here.  (Note that it's part of the documentation for an inventory system for Twine/SugarCube, so the "UInv." parts won't work without UInv being installed.)

...