Skip to content

Game Scripts

Robosturm edited this page May 10, 2019 · 11 revisions

Avaiable since Beta-Release 3

What are Game Scripts?

Game Scripts are the method of adding additional effects to a game. Like dialogues, spawning units or adding additionl victory conditions or some fency animations and a lot more stuff.

A template for game scripts can be found in the template folder of a release

Note The name of the javascript name must be "gameScript"

var Constructor = function()
{
    this.immediateStart = function()
    {
        // called to check if the game should start immediatly
        return false;
     };

    this.victory = function()
    {
        // called when a player wins
    };
    this.gameStart = function()
    {
        // called before a game starts
    };
    this.actionDone = function()
    {
        // function called after all animations are finished
    };
    this.turnStart = function(turn, player)
    {
        // called at the start of each players turn
    };
}

Constructor.prototype = BASEGAMESCRIPT;
var gameScript = new Constructor();

Overview of the most important functions

This section shows you a list of examples on how to use the game script feature. On top you may check out the map folder or co-scripts or game action scripts for other game script examples. This section won't cover everything possible with the gamescript feature. Since all functions of units, map buildings etc. that are in the public slots: section of the heades can be used to get information about the current game state or to modify it. This is a powerfull tool to create your own map with additional features.

Start of Game functions

You could change the GameRules at any time but it's recommended to do it in the gameStart callback function.

this.gameStart = function()
{
    // called before a game starts
    //we're going to set the game rules here.
    map.getGameRules().setNoPower(true); // no co power
    map.getGameRules().setRandomWeather(false); // no random weather
    map.getGameRules().setFogMode(GameEnums.Fog_Off); // no fog of war or GameEnums.Fog_OfWar -> for on
    // map.getGameRules().changeWeatherChance("WEATHER_1SUN", 90); // sets the weather chance of sun to 90. The actual chance is the the value divided through the sum of all chances
    // here we decide how you can win the game
    map.getGameRules().addVictoryRule("VICTORYRULE_NOUNITS"); // win by destroying all units
    map.getGameRules().addVictoryRule("VICTORYRULE_NOHQ"); // win by capturing all hq's of a player
};

Checking for a certain turn:

// check if it's day 1
if (map.getCurrentDay() === 1)
{
}

Most times you wanna create some dialogs. This is how you can queue and create Dialog's

// moods are GameEnums.COMood_Normal, GameEnums.COMood_Happy, GameEnums.COMood_Sad
var dialog1 = GameAnimationFactory.createGameAnimationDialog(
                    qsTr("They're... They're stronger than we had anticipated..."),
                    "co_officier_bh", GameEnums.COMood_Normal, PLAYER.getDefaultColor(4));
var dialog2 = GameAnimationFactory.createGameAnimationDialog(
                    qsTr(" I don't want excuses! I want victory!"),
                    "co_random", GameEnums.COMood_Normal, PLAYER.getDefaultColor(4));
dialog1.queueAnimation(dialog2);

Debbuging and testing

You can press F1 to take a look at the ingame console. It will show you any failures that occured during the execution of your script including line at which an error occured.

You can print information about the execution of your script printing information to the console. Example:

GameConsole.print("This shouldn't happen", 1);

The printed information can be viewn in the game by pressing F1.

Another option is to open the console and call a function directly to test it leading the command with game: for example:

game:map.getPlayer(0).getCO(0).setPowerFilled(10)

This would set the power bar of the first co of the first player to 10 stars or the maximum if it's smaller.

Clone this wiki locally