[SOLVED] How do I save a state of a vehicle module?

Discussion in 'Programming' started by SineMatic, Oct 18, 2025 at 5:36 PM.

  1. SineMatic

    SineMatic
    Expand Collapse

    Joined:
    Nov 17, 2022
    Messages:
    68
    After resetting the vehicle every module and global variable is lost. Is there any way to store the variables (apart from saving them into a json file)?

    P.S. by storing the variables I meant their values, for example, M.var = "someValue". I know I can create a module for GE Lua which I would write the values into but this approach seems a little clumsy.
     
    #1 SineMatic, Oct 18, 2025 at 5:36 PM
    Last edited: Oct 19, 2025 at 7:32 AM
  2. SineMatic

    SineMatic
    Expand Collapse

    Joined:
    Nov 17, 2022
    Messages:
    68
    Ok, I figured it out. If your module (extension) is in \lua\vehicle\extensions directory, you can use M.onSerialize and M.onDeserialized functions. For example:

    Code:
    M.variable = "morning"
    
    -- some code that changes M.variable to, for example, "afternoon"
    
    M.onSerialize = function()
      local data = {}
      data["variable"] = M.variable
      return data
    end
    
    M.onDeserialized = function(data)
      M.variable = data.variable -- the variable value is reassigned to "afternoon"
    end
    
    
     
  3. Neverless

    Neverless
    Expand Collapse

    Joined:
    Sep 18, 2016
    Messages:
    259
    In the vehicle lua vm lua is never reset with a vehicle reset aslong as you dont listen to the reset event and reset everything yourself. I guess you are rather talking about a vehicle reload `CTRL+R`? Can i ask why you need to keep data between vehicle reloads?
     
  4. SineMatic

    SineMatic
    Expand Collapse

    Joined:
    Nov 17, 2022
    Messages:
    68
    Oh, ctrl+r is called reloading...

    I've made a gravity mod which dynamically shifts a planet object. A user can enter custom values in the Imgui window, which are then sent to the vehicle module. So the problem was that these values were not saving while reloading :)
     
    • 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