FANDOM


    ChoiceScript supports a simple form of subroutines. A subroutine is a section of code able to be accessed (using the *gosub command) where & when required at many different points within the same scene file. This essentially negates the need to copy-paste an identical piece of code over & over to use in different places, and also makes any later changes much quicker to implement.

Note that the *gosub command only works within the same scene file. An extension of this command, *gosub_scene, allows a subroutine to be called from any scene file, further reducing the duplication of identical code accross the game files.

Usage

For example, let's assume that in your game you have four main stats (say, Strength, Dexterity, Intelligence & Wisdom) shown on your Stats page as percentile bars, which means you probably don't want them to ever exceed a value of 100. To make certain of this you can use a very simple subroutine, which you would probably place down at the bottom of your scene file. It might look something like this:

*label cap_stats
*if strength > 100
    *set strength 100
*if dexterity > 100
    *set dexterity 100
*if intelligence > 100
    *set intelligence 100
*if wisdom > 100
    *set wisdom 100
*return

You have now effectively created a subroutine with a label of 'cap_stats', by the simple expedient of adding that vital *return command at the end. The *return command is only ever used to signify a subroutine.

You can now use this subroutine's ready-made scripting anywhere within the same file (e.g. wherever you apply any new stats gains), without having to repeat or copy-paste those lines anywhere else in the scene, simply by using the *gosub command where required. For example:

*choice
   #Train body.
       *set strength +10
       *set dexterity +10
       *gosub cap_stats
       You train your body.
       *finish
   #Train mind.
       *set intelligence +10
       *set wisdom +10
       *gosub cap_stats
       You sharpen your mind.
       *finish

When the game reaches a *gosub cap_stats line it will immediately jump to the *label cap_stats line (as shown in the previous example) and execute all the scripting there, until it hits a *return command, at which point it will immediately jump back to the originating *gosub line and continue on from that point. In essence, where you use that particular *gosub cap_stats, ChoiceScript will effectively do this:

*choice
    #Train body.
        *set strength +10
        *set dexterity +10
        *if strength > 100
            *set strength 100
        *if dexterity > 100
            *set dexterity 100
        *if intelligence > 100
            *set intelligence 100
        *if wisdom > 100
            *set wisdom 100
        You train your body.
        *finish
    #Train mind.
        *set intelligence +10
        *set wisdom +10
        *if strength > 100
            *set strength 100
        *if dexterity > 100
            *set dexterity 100
        *if intelligence > 100
            *set intelligence 100
        *if wisdom > 100
            *set wisdom 100
        You sharpen your mind.
        *finish

This is just a simple example to illustrate how a *gosub--run subroutine script--*return combination works. However, there is no real limit to how long or complex a single subroutine can be, making it one of the most useful ChoiceScript features.

Tip: If you find yourself repeating a particular section of code several times, consider if it might not be best turning it into a subroutine and just using *gosub within your actual story scripting - especially if there's a chance you may later wish to modify any part of that code. It's much easier & quicker to edit it when it's all in a single subroutine near the bottom, rather than when repeated several times throughout the file.

Advanced *gosub

Note that subroutines can be nested, meaning that within one subroutine you can *gosub to another, and then another, and so on, until it hits an actual *return command. By this means it's possible to create quite intricate & complicated programming. However, a typical ChoiceScript game would probably never need to do so. For most games it should suffice to use multiple simple subroutines for entirely different, and completely unrelated, purposes.

Note

Although the example above has been used to illustrate how, and under what sort of circumstances, you should consider using a *gosub subroutine, the easiest way to avoid problems with uncapped percentile values is to use ChoiceScript's "Fairmath" system -- covered in more detail in the article on Arithmetic Operators.


More commands
Choice *choice, *fake_choice, *disable_reuse, *hide_reuse, *allow_reuse, *selectable_if
Variable *create, *temp, *set, Arithmetic operators, *delete, *input_number, *input_text, *print, *rand
Conditional *if, *elseif, *else
Goto *label, *goto, *goto_scene, *goto_random_scene, *gosub, *gosub_scene, *finish
Formatting Bold text, Italic text, *image, *line_break, *page_break, *link, *stat_chart
Miscellaneous *comment, *scene_list, *title, *author, *achieve, *achievement, *check_achievements, *bug, *ending, *more_games, *share_this_game, *show_password, *script

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.