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
Maybe not, but perhaps you could make the visibility dependent on another variable that can be saved. I'm thinking completing a goal etc.
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
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?
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.