0 votes
by (390 points)

So in the game I'm working on, every character has their own style of textboxes defined in CSS. For example:

.tom {
color: green;

.bob {
color: blue;

.lisa {
color: pink;

There's more to the code than that, but I'm at work, so that'll do. Now whenever I have a character speak, it looks like this:

<<pro $tom.profile>>$tom.name:<hr><<img $tom.img>>
Hello blah blah blah...@@

That works great. <<pro>> and <<img>> are macros I set up for handling profile and full size images easier. What I'd like to do is condense everything down to a single macro, however I can't seem to be able to get a variable to be accepted as a style.

<<nm $tom "Hello world">>
<<widget "nm">>
  /* $tom.style = "tom" */
  <<set _x = $args[0].style
  <<pro $args[0].profile>>$args[0].name<hr><<img $args[0].img>>

I've tried writing this several different ways, including <div id=_x> but I cannot get the style to activate. Replacing _x with something like @@.tom works fine.

This is probably stupid user error, but I'd really appreciate some help!

1 Answer

0 votes
by (65.4k points)
selected by
Best answer

The custom styles markup does not currently support the use of variables, so you'll have to use the Stupid Print Trick™ to compose the markup, so that it contains the class before it's rendered.  For example, something like the following should work:

<<widget "nm">>
\<<= '@@.' + $args[0].style + ';\n<<pro $args[0].profile>>$args[0].name<hr><<img $args[0].img>>\n$args[1]\n@@'>>

NOTE: The newline character escape ("\n") takes the place of hard returns within the string.

by (390 points)
I've never had a reason to use the print macro until now, but that works perfectly. I never would have figured that one out on my XD

Thanks so much!!!
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.