FANDOM


This page contains outdated information

As of April 2013[1], mygame.js is no longer used to list scene files or create variables, and most authors will never need to touch this file. Listing scene files and variable creation is now done in Startup.txt.

This article is aimed at beginners to ChoiceScript. However, if you have not already done so then the best place to start would be with the very first article in this Introduction To ChoiceScript Game Development series of linked articles: A Basic Tutorial.

mygame.js is the most important ChoiceScript file, at least from the point of view of a game author. If this file isn't laid out correctly, your game won't even run: errors in mygame.js are the main cause of any loading issues.

mygame.js can be found in the "mygame" (..\web\mygame) folder of your ChoiceScript directory.

What is mygame.js?

mygame.js is a JavaScript file (hence the .js bit on the end!), which is the 'language' ChoiceScript uses to turn your ordinary text files (such as choicescript_stats.txt and ordinary scene .txt files) into a working story-game. For this reason mygame.js follows a prescribed format that is slightly different to anything else you will use in your own actual scripting, but it is a format that must be strictly adhered to in order to avoid unwanted errors. In short, be very careful when editing this particular file, and be sure to follow these instructions precisely.


What is mygame.js used for?

mygame.js serves three main purposes, but for most authors only the first two of these will be of any importance initially. The third, "debug stats", is not something you need to worry about for quite a while to come -- if at all.

The first important purpose of mygame.js is the SceneNavigator. This is where you list the new scene files as you add them to your game, which tells ChoiceScript in what sequence your scenes are intended to appear in the game. Think of them as Chapters in an ordinary book: ChoiceScript needs to know that "Chapter Two" (or whatever you choose to call your scene) follows "Chapter One", in sequence. Then, when ChoiceScript hits a end-of-scene *finish command in one scene, it will know -- from the SceneNavigator list in mygame.js -- exactly which scene file it should load next.

The second important purpose of mygame.js is the default starting Stats list. This is where you "declare" the Permanent Variables that your game will use to store necessary information -- game data, in essence -- for use in your story scripting and on the game's Stats Screen. This essential information includes the unique name of each variable, its Data Type (either numeric, string or boolean) and the default starting value each will contain.

When opened for the first time, mygame.js will look something like this. Note that the three sections -- SceneNavigator, Stats and Debug Stats -- are each readily identifiable from the text already included in the file.

// Specify the list of scenes here, separated by commas, with no final comma

nav = new SceneNavigator([

    "startup"

    ,"animal"

    ,"variables"

    ,"gosub"

    ,"ending"

    ,"death"

   

]);

// Specify the default starting stats here

stats = {

    leadership: 50

    ,strength: 50

};

// Specify the stats to use in debug mode

debugStats = {

    leadership: 50

    ,strength: 50

};

// or just use defaults

// debugStats = stats

Note that every line starting with // is just a helpful comment for you and is not actually used in the game.


The SceneNavigator Section

The SceneNavigator section at the top should be changed to list your own scene file names, being those located in the "scenes" (..\web\mygame\scenes) folder, replacing the following list in sequential order:

// Specify the list of scenes here, separated by commas, with no final comma

nav = new SceneNavigator([

    "startup"

    ,"animal"

    ,"variables"

    ,"gosub"

    ,"ending"

    ,"death"

   

]);

Your scene file names should not contain any spaces, should always be enclosed in "quotation marks" when added to this list, and should not include here the ".txt" file extension of the actual file in the "scenes" folder. In addition, note that all except the first scene name in the list should be preceded by a comma, as shown above.

To avoid generating game-stopping errors, whenever you add a new scene name to this list, make sure that a .txt file of that name is also placed or created in the "scenes" folder, even if it's still just a completely blank file.

You should not include choicescript_stats.txt in the above list, even though that file exists (and must remain) in your "scenes" folder along with your ordinary scene .txt files.

Note that the following two lines from the section above:

nav = new SceneNavigator([

and;

]);

are absolutely essential to this JavaScript file. Do not tamper with those lines in any way.


The Default Starting Stats Section

The Stats section of the mygame.js file is used for "declaring" your Permanent Variables in the game; defining a name, data type and a default starting value for each.

In the following example, both of these named variables are numeric data type, by virtue of the default starting value (50) of each being a number. In essence, the data type of a Permanent Variable is determined purely by the type of value you assign to it here -- either a number (so it becomes numeric type), "some text" (making it a string type), or either true or false (without any "quotes", making it a boolean type).

// Specify the default starting stats here


stats = {

    leadership: 50

    ,strength: 50

};

Replace these example variables with your own. Examples of each data type follow:

stats = {

     look: "Nice Appearance"

    ,strength: 50

    ,exam_passed: true

    ,princess_met: false
};

The first is a string (note the use of "quote marks" around the default starting value, to indicate string type) the second is a numeric type (being just a number), and the third and fourth are both boolean (which should never use "quote marks" around the default values of true or false).

Variable names should never themselves contain spaces (under_score_is_fine, however) or use Upper Case (capital) letters. You may of course use capital letters and spaces within the "contents" value of a string variable, as shown in the example above. Bear in mind also that the names of Permanent Variables used in your game should always be unique throughout.

As with the scenes listing under the SceneNavigator section, the second and subsequent variables added to this list should always be preceded by a comma, as shown above. There is no real limit to the number of different variables you can add to this list.

Note that the following two lines from the section above:

stats = {

and;

};

are absolutely essential to this JavaScript file. Do not tamper with those lines in any way.

See the articles on Data Types and Variable Types, respectively, for more information on Permanent Variables.


The Debug Stats Section

As mentioned previously, you really don't need to worry too much about debug stats for quite a while, if at all. For now, it is recommended that you read through this section for future reference and then make the change detailed in the final example below, so that "debugStats = stats". That way you don't have to worry about it at all, and you don't have to create a separate list of variables in mygame.js. You can always change it again later, with more experience under your belt.

// Specify the stats to use in debug mode


debugStats = {

    leadership: 50

    ,strength: 50

};


// or just use defaults

// debugStats = stats

The debug stats are the stats you use when you test your game using the testing programs provided for this purpose. You should only define variables here that are already defined in the "stats" part; for example, if you have this in a mygame.js file:

// Specify the default starting stats here


stats = {

    leadership: 50

};


// Specify the stats to use in debug mode


debugStats = {

    leadership: 50

    ,strength: 50

};


// or just use defaults

// debugStats = stats

It will cause an error, because there's no strength variable in your normal "stats".

If, however, you don't feel that you need specific variables for testing it automatically, you can instead just amend the debug section to read as follows:

// Specify the stats to use in debug mode

debugStats = stats;

The game will then always use your ordinary Stats list when running the automated testing programs provided.

Related Articles

Next Tutorial Article: Indentation

Back to the Index.

Related Articles:

References

  1. Dan Fabulich, New ChoiceScript: *create has changed, mygame.js is optional, April 25, 2013.

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.