« FristScript » : différence entre les versions

De Wiki Let's Role
m add shortcut to format code
Aucun résumé des modifications
 
(4 versions intermédiaires par le même utilisateur non affichées)
Ligne 9 : Ligne 9 :
* make it readable and understandable : use appropriate names, indent your code (format your code automaticaly with <code>SHIFT+ALT+F</code>)
* make it readable and understandable : use appropriate names, indent your code (format your code automaticaly with <code>SHIFT+ALT+F</code>)
* structure, separate your rules
* structure, separate your rules
* don't repeat yourself (DRY) : there are many ways to make your code read
* don't repeat yourself (DRY) : there are many ways to make your code readable and not repetitive. Remind one rule : if you copy / paste some code in your script, it could good to generalise it.
* Don't hesitate to ask the community for help


=== Step 1 : init ===
=== Step 1 : init ===
Ligne 45 : Ligne 46 :
     // write you code here for the sheet with the id "object"
     // write you code here for the sheet with the id "object"
  }
  }
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.
As the number of pages increases, you will quickly see that you are repeating yourself by adding a long list of <code>if else if</code>, to prevent that you can check this [[Init Advanced Tip|advanced script tip for init]].
// A list of initializers, associating the sheet id with an init function
 
const initializers = {
{{CLEFDETRI:FirstScript}}
    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., <code>vehicule</code>), you have to create a new function called <code>initVehicule</code> and reference if in <code>initializers</code> (adding a line <code>vehicule: initVehicule</code>).

Dernière version du 10 septembre 2025 à 23:52

Introduction

Writing a script while you're not a coder is a very intimidating task : it is a new language with a lot of syntax rules.

You can start reading some tutorials on how to start with javascript. Let's Role allows you to code with a version of JavaScript that is close to the standard, but with a few differences.

Step 0 : basics

Here are a list of empiric rules that will help you writing your script :

  • make it readable and understandable : use appropriate names, indent your code (format your code automaticaly with SHIFT+ALT+F)
  • structure, separate your rules
  • don't repeat yourself (DRY) : there are many ways to make your code readable and not repetitive. Remind one rule : if you copy / paste some code in your script, it could good to generalise it.
  • Don't hesitate to ask the community for help

Step 1 : init

Every script must have a init function. This function will be called as soon as a sheet is opened (character sheet, token sheet, craft sheet, prompt sheet). We use the init to add some interactivity and complex rules on the sheet.

As it is a unique function to handle many different sheets, you must use an if to check which init code to execute on which sheet.

Here is a simple example that we recommend to use only if you plan to keep the very simple script.

init = function (sheet) {
    if (sheet.id() === "main") {
        // write you code here for the sheet with the id "main"
    }
}

If you want to make some inits for few other sheets, we recommend to create different function. The following solution is a very clean way to handle the initialisation of different sheets using their id to detect them. Creating different function helps you to separate the rules of your system. The more your script will grow, the more you'll need to organize it.

init = function (sheet) {
    const sheetId = sheet.id()
    if (sheetId === "main") {
        initMain(sheet)
    } else if (sheetId === "monster") {
        initMonster(sheet)
    } else if (sheetId === "object") {
        initObject(sheet)
    }
}

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"
}

As the number of pages increases, you will quickly see that you are repeating yourself by adding a long list of if else if, to prevent that you can check this advanced script tip for init.