0 votes
by (750 points)

I know this sounds confusing, but I am making a game with a lot of character names( $name, $name1, $name2, ect)

 

Instead of always having to put

<Font color="red">$name</font>

 can I make a variable to always display $name in red or other colors?

2 Answers

+1 vote
by (61.3k points)
edited by

First thing: don't use the <font> element. It's been deprecated for like twenty years now. SugarCube has custom style markup for just this kind of situation. 

TME answered a very similar question not very long ago, so I recommend looking at that. The cliff notes version is that you can either add the markup you need to the variable itself or use a widget. Examples: 

—> variable method
<<set $name to "@@color:red;" + $name + "@@">>

—> widget method (this code goes in a widget-tagged passage)
<<widget "name">>\
    @@color:red;$name@@\
<</widget>>

All things being equal, I would go with the widget. 

It's also possible to use classes instead of individual style rules, if you're doing more than just changing the color. 

by (750 points)

Using the Variable method, do i just type $Name and it displays as always red or do I need to type @@$Name or just $Name

 

I get this error on both:

 

  Error: <<set>>: bad evaluation: Invalid or unexpected token
 

by (61.3k points)

Just type in the variable.

If you're getting an error, you've probably made a typo somewhere.  Try copying and pasting this over to try it out:

<<set $name to 'Bob'>>\
<<set $name to '@@color:red;' + $name + '@@'>>\
My name is $name.

 

by (750 points)

Thanks. My mistake for using $Name instead of $name!

 

Thank you Chapel! wink

+1 vote
by (2.2k points)

You can make a widget. Add a passage named like charname or something (it doesn't really matter), containing code like this:

<<widget "charname">><font color="red">$args[0]</font><</widget>>

Add widget tag (this is important) to this passage, and use it in your game like so:

<<charname $name>>

 

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.
...