variable types and string Template


  • Global variables ($fname, $money, etc.) can be “set” (created) anywhere, and then they exist until they are “unset”. Depending on how many level of undo/rewind you allow for the game, they will be saved that many times in the game, hence for a game with many undo/rewind ability, the number of global variables should be kept as low as possible. Though it probably should be a good practice to not have unnecessary variables hanging around anyway now that we can unset them any time they no longer serve a purpose. In ChoiceScript it’s not something we worry about nor can do anything about, but in SugarCube we can, so should try to be mindful of which variables are ACTUALLY necessary at any given moment. Syntax:
<<set $var5 = "some text">>
$var5
<<unset $var5>> /% removes it from game %/
<<set $var5 = "new text">> /% re-create it again and give it a different value %/

/% alternative ways to output a variable's value, just fyi %/
Some text, now show <<= $var5>> or <<print $var5>>
  • Temporary variables (_next, _done, etc.) can also be set anywhere, but unlike Global variables, they don’t need to be unset to be removed since they exist for the duration of the current passage they were created in. I use it mainly for putting a repeated “next passage name” value for choice links, or when there are portions of flavor text that are the same between different choices.
    Syntax:
<<set _next = "c3_chat">>
<<set _same = `Seeing me settling down with my mess of papers, she shifts back a little to give me more space.`>>
<<choice_shown 'I nod, then move my stuff to the small desk. "Thanks for standing up for me."' _next `hasVisited("c3_interrupt")`>>
    <<set $text = `"Us bookworms have to stick together," her smile is more natural this time. ` + _same>>
<</choice_shown>>
<<choice_shown 'I nod. "Thanks."' _next>>
    <<set $text = _same>>
<</choice_shown>>

After we move to a different passage, the temporary variables will be undefined.

  • In the context of SugarCube 2, Template refers to a kind of function call (or think of it like a subroutine in ChoiceScript) that returns a string. It looks similar to a variable, but acts differently and is set up differently. You cannot compare its value to anything, only to display it. Once they are set up, they don’t normally change their value, and are only stored once in the game. These are best for pronouns variations, or even variation text based on a certain variable value. Syntax:
"...$fname could get into any school ?they <<verb 'wants' 'want'>>. I mean, have you seen ?their grades?"

In ChoiceScript, you can do $!{var} to capitalize the first letter easily, but there is no direct equivalent in SugarCube. Because Template words are case sensitive, however, we could make a capitalized template word to return a capitalized version. In the narrative part of the code, this can be more readable at a glance.
To see full definitions of Templates that are set up in this game, see “src/js/templates.js” file for detail, but don’t change anything there without first understanding what the code is doing.

I learned about Template after using a third-party script (see “vendor/chapel/pronouns.js”) for MC’s pronoun template as well as verb conjugation if it’s plural, but it is only usable for the MC, thus I had to learn how to do the simple version for the other gender-variable characters.

See more documentation of this custom pronoun script: https://github.com/ChapelR/custom-macros-for-sugarcube-2/blob/master/docs/pronoun-templates.md

  • Inspired by Chapel’s script’s <<verb>>, I made one for general case (emulating ChoiceScript’s multireplace): <<pverb>>
    Syntax:
<<pverb $pluralornot "have" "has">>

(I didn’t want to edit Chapel’s <<verb>> macro, and wanted to keep ChoiceScript’s multireplace’s syntax, so unfortunately this results in <<verb>> and <<pverb>> not having a similar parameter order, aka they take in singular and plural verb in different order. Sorry for the confusion.)

I tweaked the code further and now we can define the plural and singular forms in “src/js/pverb.js”, and then in the passage we only need to give it the main form of the verb (ie. “are”, “have”, “say”, etc.). Example syntax:

?They <<pverb $theypronounplural "have">> it.

You can still just explicitly provide the plural and singular forms of the verb, but this method can save you some typing if that verb gets repeated a lot in the story.

To add more verb forms to that list, go to src/js/pverb.js:

setup.pverblist = {
   "are" : "is", "aren't" : "isn't",
   "have" : "has"
};

for example, you can add “hug” : “hugs” to any point after a comma and before another pair, but if it’s between other pairs, add a comma after it. If you add it to the end of the list, add a comma after “have” : “has”, like:

setup.pverblist = {
   "are" : "is", "aren't" : "isn't",
   "have" : "has",
   "hug" : "hugs"
};

The last pair in the list should not have a comma after it, or Javascript will complain when you try to compile this code.

Note: you can put every pair on the same line, or put them on separate lines; I just put some pairs on the same line because they are similar verbs. As long as the commas are in the right place, the list will be understood by the engine.

Get SugarCube code collection to emulate the look of ChoiceScript

Leave a comment

Log in with itch.io to leave a comment.