Unsolved Automatically saving a level with a LUA trigger

Discussion in 'World Editor' started by MysteryManGER, Jul 14, 2024.

  1. MysteryManGER

    MysteryManGER
    Expand Collapse

    Joined:
    May 25, 2016
    Messages:
    770
    Hey,

    Is it possible to save the level automatically with a LUA trigger?

    For example: By default, a certain object is "hidden". Now I drive through a LUA trigger, which sets the object visibility from "false" to "true".

    Here is an example of my map "Car Jump Arena 2024", which makes the trophy appear on the podium as soon as you put the car on the corresponding level.

    Code:
    local function ShowTrophyGold(data)
     local obj = scenetree.findObject("TrophyGold")
     if obj then
      if data.event == 'enter' then
       obj:setHidden(false)
      elseif data.event == 'exit' then
       obj:setHidden(false)
      end
     end
    end
    return ShowTrophyGold
    Can this state be saved? So that when I reload the map, the object is immediately visible without having to activate the trigger again? And without opening the level editor with F11 and saving the status manually?

    I would be grateful for any help!

    Best,
    MysteryManGer
     
  2. el_ferrito

    el_ferrito
    Expand Collapse

    Joined:
    Sep 30, 2020
    Messages:
    510
    Maybe not, but perhaps you could make the visibility dependent on another variable that can be saved. I'm thinking completing a goal etc.
     
    • Like Like x 1
  3. Barbent Servo GT

    Barbent Servo GT
    Expand Collapse

    Joined:
    Jun 4, 2023
    Messages:
    702
    Hi!
    You can save and load data from within the mainLevel.lua using the following code:
    Code:
    -- write data
    testvalue1 = 15
    testvalue2 = 300
    local f = assert(io.open("filename.txt", "w"))
    f:write(testvalue1 , "\n")
    f:write(testvalue2 , "\n")
    f:close()
    
    -- read data
    local f = assert(io.open("filename.txt", "r"))
    testvalue1 = f:read("*line")
    testvalue2= f:read("*line")
    f:close()
    The file is created in the BeamNG user folder root. I may be usefull to put "/levels/[mapname]/" in front of the filename. You can aquire the mapname by:
    Code:
    mapname = getCurrentLevelIdentifier()
    
    Hope that helps...
    Barbent
     
    #3 Barbent Servo GT, Jul 15, 2024
    Last edited: Jul 15, 2024
    • Like Like x 1
  4. MysteryManGER

    MysteryManGER
    Expand Collapse

    Joined:
    May 25, 2016
    Messages:
    770
    Thanks for your help! I'll take a look into it!
     
  5. Barbent Servo GT

    Barbent Servo GT
    Expand Collapse

    Joined:
    Jun 4, 2023
    Messages:
    702
    I just noticed something else interesting: You can also write and read JSON files with BNG LUA:
    Code:
    -- Write Data
    jsonWriteFile("filename.json", {value1 = 15, value2 =30}, true)
    -- Params: jsonWriteFile(filename, obj, pretty, numberPrecision)
                                    
    -- Read Data
    local jsonData = jsonReadFile("filename.json")
        if jsonData then
            value1 = jsonData.value1
            value2 = jsonData.value2
        end
    
    Both the functions (jsonWriteFile and jsonReadFile) are part of the /lua/common/utils.lua from BeamNG. I didn't tested it yet. If you should do so, could you tell me if it worked?
    :)
     
  6. DaddelZeit

    DaddelZeit
    Expand Collapse

    Joined:
    Jul 17, 2019
    Messages:
    3,463
    These functions are extremely common and work exactly as you'd expect.
    There is also readFile(filepath) and writeFile(filepath, string).

    Back to the thread - I'd follow this solution. Saving the level like you would with the world editor will mess up mod updates and cause unexpected behaviour in general.

    I would simply write a save file in either the temp or the level's directory containing the state of the level. You could do JSON if more than a true/false needs to be saved, but otherwise a string based file will suffice.
     
    • 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