LUA trigger scripts

Discussion in 'Programming' started by ericmarciano, Nov 26, 2025 at 10:23 PM.

  1. ericmarciano

    ericmarciano
    Expand Collapse

    Joined:
    Sep 1, 2021
    Messages:
    100
    I have created an LUA trigger in my map, I wrote a specific LUA code for it and it works perfectly. I would like to duplicate this in several places of the map. OK, I know I can easily duplicate an object in the World Editor and move it somewhere else. But the problem is that each time I change the code of my LUA script (to improve it), I have to duplicate the code in each instance of my LUA trigger, it's a pain.
    This is why I would like to know if I can write the code once in a LUA file, then reference it in all LUA triggers. Doing this way, I can change the code once and it is taken into account by all my triggers. Is this possible?

    Thanks.
     
  2. MegaKnight

    MegaKnight
    Expand Collapse

    Joined:
    Sep 12, 2024
    Messages:
    262
    Yes, this is possible.

    First of all, you need a mainLevel.lua file in your level folder (levels/your_level_name/mainLevel.lua).
    Copy the following code into that lua file:
    Code:
    -- code in mainLevel.lua
    local M = {}
    
    local function triggerFunction(data)
      -- insert the code of your trigger here
    end
    
    M.triggerFunction = triggerFunction
    
    return M
    This creates a table, which you can access from the outside using mainLevel.something. You can add functions and variables to that table using M.var = var (as seen in the code) to make them accessible.

    Now, you copy the code from your trigger into the function inside the mainLevel file. Then you can call this function from your triggers using the following code:
    Code:
    -- code inside the triggers
    local function fun(data)
      mainLevel.triggerFunction(data) -- pass the data from the trigger to your mainLevel-function
    end
    
    return fun
    Let me know when it works or if you need any more help with it.
     
  3. ericmarciano

    ericmarciano
    Expand Collapse

    Joined:
    Sep 1, 2021
    Messages:
    100
    It works !!
    It was initially failing and it took some time for me to understand what was wrong. mainLevel was first seen as nil, and I finally understood you must use extensions.mainLevel instead. Now it works.

    Thank you for your help !!
     
    • Like Like x 1
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice