Init Advanced Tip

De Wiki Let's Role
Version datée du 1 septembre 2025 à 16:58 par Guile (discussion | contributions) (Page créée avec « As soon as you have a large number of different sheet to initialize (more than 10), we recommend to use more advanced code to make it shorter. It uses a list of initializers associated with the sheet id. This will prevent you to write a large and repetitive list of <code>if else if</code>. The following code shows one way to do it. // A list of initializers, associating the sheet id with an init function const initializers = { main: initMain, monster:... »)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)

As soon as you have a large number of different sheet to initialize (more than 10), we recommend to use more advanced code to make it shorter. It uses a list of initializers associated with the sheet id. This will prevent you to write a large and repetitive list of if else if. The following code shows one way to do it.

// A list of initializers, associating the sheet id with an init function
const initializers = {
    main: initMain,
    monster: initMonster,
    object: initObject,
}

init = function (sheet) {
    const sheetId = sheet.id()
    if (initializers[sheetId]) {
        initializers[sheetId](sheet);  // this line will execute the desired initializer.
    }
}

function initMain(sheet) {
    // write you code here for the sheet with the id "main"
}

function initMonster(sheet) {
    // write you code here for the sheet with the id "monster"
}

function initObject(sheet) {
    // write you code here for the sheet with the id "object"
}

One big pro for this technic is that the init function will not change anymore. If you want to add a new sheet to initialize (e.g., vehicule), you have to create a new function called initVehicule and reference if in initializers (adding a line vehicule: initVehicule).