This wiki is archived from 2021-09-05

New game user interface scene

From Planetary Annihilation: TITANS and Classic PA Wiki
Jump to navigation Jump to search

Planetary Annihilation New Game User Interface Scene

Path: /ui/main/game/new_game/

Scene name: New Game

Server script state: /server-script/states/lobby.js

The new_game user interface is loaded when:

  • host creates a new game
  • player or spectator joins a game in the lobby state

Server mods are now managed in the connect to game scene like players and spectators.

Preventing the need for a host refresh

When a host creates a new game the user interface is displayed before any server mods are loaded requiring a host refresh to load any updated user interface provided by a server mod.

A refresh can be triggered by the host using any of the following:

  • manual refresh (default binding of F5)
  • navigating to another user interface scene eg load_planet when selecting a system

The current approach to avoid the need for a host refresh is:

  • include the new game modifications in both the server mod and a dependent client mod
  • check if the client mod has already been loaded
  • check if the server mod has actually loaded
  • load any assets manually using loadCss, loadHtml, etc (to prevent double loading if specified in modinfo scenes.new_game)

Note: This may change when side loading of companion client mods is implemented.

server modinfo.json snippet

{
    "identifier": "com.pa.my-unique-mod-name",
    "context": "server",
...
    "scenes":
    {
        "new_game":
        [
            "coui://ui/mods/com.pa.my-unique-mod-name/new_game.js"
        ]
...
    "dependencies":
    [
        "com.pa.my-unique-mod-name-client"
...

client modinfo.json snippet

{
    "identifier": "com.pa.my-unique-mod-name-client",
    "context": "client",
...
    "scenes":
    {
        "new_game":
        [
            "coui://ui/mods/com.pa.my-unique-mod-name/new_game.js"
        ]
...
...
    "dependencies":
    [
        "com.pa.my-unique-mod-name"
...

shared new_game.js snippet


var myUniqueModNameLoaded;

function myUniqueModName()
{
    if ( myUniqueModNameLoaded )
    {
        return;
    }

    myUniqueModNameLoaded = true;

    model.myUniqueModNameServerModIsLoaded = ko.observable( false );
    
    model.myUniqueModNameIsHost = ko.computed( function()
    {
        return model.isGameCreator() && model.myUniqueModNameServerModIsLoaded();
    });

    model.myUniqueModNameServerModCheckLoaded = function()
    {
        api.mods.getMountedMods( 'server', function ( mods )
        {

// check to see if server mod (and optionally a dev version) are loaded

            var loaded = _.intersection( _.pluck( mods, 'identifier' ), [ 'com.pa.my-unique-mod-name', 'com.pa.my-unique-mod-name-dev' ] ).length > 0;
            
            model.myUniqueModNameServerModIsLoaded( loaded );
        });
    }

// once mod data is sent check if server mod is actually loaded
    
    if ( window.scene_server_mod_list && window.scene_server_mod_list.new_game )
    {
        model.myUniqueModNameServerModCheckLoaded(); 
    }
    else
    {
        var server_mod_info_updated_handler = handlers.server_mod_info_updated;
   
        handlers.server_mod_info_updated = function( payload )
        {
            server_mod_info_updated_handler( payload );
            
            model.myUniqueModNameServerModCheckLoaded();
        }
    }
    
    model.myUniqueModNameServerModIsLoaded.subscribe( function( myUniqueModNameServerModIsLoaded )
    {
        if ( myUniqueModNameServerModIsLoaded )
        {
 // do mod stuff
        }
    });

    model.myUniqueModNameServerModIsHost.subscribe( function( myUniqueModNameServerModIsHost )
    {
        if ( myUniqueModNameServerModIsHost )
        {
 // do mod stuff if host or became host if host left
        }
    });
...
}

try
{
    myUniqueModName();
}
catch (e)
{
    console.error(e);
}


My personal preference is a named function in preference to an immediately-invoked function expression (IIFE).

This approach will create the following:

  • window.myUniqueModNameLoaded - can be used by other mods to check for conflicts with client mod
  • model.myUniqueModNameServerModIsLoaded() - can be used by other mods to check if server mod is loaded