Init Advanced Tip
De Wiki Let's Role
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).