onExtensionLoaded not being called

Discussion in 'Programming' started by austint30, Nov 15, 2024.

  1. austint30

    austint30
    Expand Collapse

    Joined:
    Aug 7, 2013
    Messages:
    240
    Hello,

    I want to implement onExtensionLoaded and onExtensionUnloaded to create and then clean-up scene objects.
    Unfortunately, I just cannot seem to get onExtensionLoaded and onExtensionUnloaded to be called. Pressing Ctrl + L doesn't seem to help in this case either. I've added logging statements into both of them, but I am not seeing them in the console. Oddly enough, the onVehicleSpawned and onVehicleDestroyed are called as expected.

    This is causing TSStatic objects that were created during onVehicleSpawned to stay in the scenewhen they are supposed to be cleaned up by onExtensionUnloaded when I press Ctrl + L to reload GELUA.

    I've also tried onInit, but that doesn't seem to be called either.

    Here is a very cut-down version of my code:

    Code:
    -- lua/ge/extensions/<redacted>/main.lua
    local M = {}
    local mName = "myExtension"
    
    local function onExtensionUnloaded()
        log("I", mName, "Extension unloading")
        -- Clean up
    end
    
    local function onExtensionLoaded()
        log("I", mName, "Extension initializing")
        -- Do stuff
    end
    
    local function onVehicleSpawned(vehicleId)
        log("I", mName, "Vehicle loaded")
        -- This works
    end
    
    local function onVehicleDestroyed(vehicleId)
        log("I", mName, "Vehicle destroyed")
        -- This works
    end
    
    M.onVehicleSpawned = onVehicleSpawned
    M.onVehicleDestroyed = onVehicleDestroyed
    M.onExtensionUnloaded = onExtensionUnloaded
    M.onExtensionLoaded = onExtensionLoaded
    
    return M
    I also have included a modScript.lua file with the following included:

    Code:
    -- scripts/<redacted>/modScript.lua
    
    setExtensionUnloadMode("<redacted>_main", "manual")
    --- Post updated ---
    @stenyak Oh yeah forgot to mention you so you're notified.
     
    #1 austint30, Nov 15, 2024
    Last edited: Nov 15, 2024
  2. stenyak

    stenyak
    Expand Collapse
    Programmer & Global Moderator
    BeamNG Team

    Joined:
    Aug 6, 2012
    Messages:
    1,996
    Hey :)

    I don't see any line of code that would load the extension. Where do you load it?
     
  3. austint30

    austint30
    Expand Collapse

    Joined:
    Aug 7, 2013
    Messages:
    240
    I load it via modScript.lua, which loads main.lua when the mod is enabled.
     
  4. _N_S_

    _N_S_
    Expand Collapse

    Joined:
    Oct 28, 2017
    Messages:
    73
    Checked it, it works.
    I don't know why onExtensionLoaded is not called for you.
    --- Post updated ---
    modScript.lua files are executed when a mod is loaded, the setExtensionUnloadMode function adds the script to the table of extensions that will be loaded after all mods.
     

    Attached Files:

  5. austint30

    austint30
    Expand Collapse

    Joined:
    Aug 7, 2013
    Messages:
    240
    Does it log "Extension initializing" after you press Ctrl + L?
     
  6. _N_S_

    _N_S_
    Expand Collapse

    Joined:
    Oct 28, 2017
    Messages:
    73
    yes
     
  7. Riccarr

    Riccarr
    Expand Collapse

    Joined:
    Jul 28, 2024
    Messages:
    18
    Here's what I figured manually ...


    -- occurs at start of game (main menu) as well as CTRL-L reload - not when map/level loaded
    local function onInit()

    -- occurs when map/level loaded; not after reload CTRL-L - check for state == 2
    local function onWorldReadyState(state)

    -- occurs at start of game (main menu) as well as CTRL-L reload - not when map/level loaded
    local function onExtensionLoaded()

    Also I believe those function events are only called on the main mod lua file, not any loaded via require().
     
  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