Scripting Moderators ds1-e Posted April 1, 2019 Author Scripting Moderators Share Posted April 1, 2019 (edited) 25 minutes ago, IIYAMA said: Might be the reason for writing incomplete data. The file isn't flushed, so it might handy to do that as well: https://wiki.multitheftauto.com/wiki/FileFlush Oh and empty(recreate) the file each time you start writing data. @majqq The file is incorrect. There has been double written as you can see. (from a few moments before) se ] ] from: false, false ] ] Clean the file and try again. OnClientResourceStart creates a good file, but when file saves onClientResourceStop it have this typo (or i don't know...). I should re-create file in script? Edited April 1, 2019 by majqq Link to comment
Moderators IIYAMA Posted April 1, 2019 Moderators Share Posted April 1, 2019 8 minutes ago, majqq said: I should re-create file in script? yes, every time you are going to write. 1 1 Link to comment
Scripting Moderators ds1-e Posted April 1, 2019 Author Scripting Moderators Share Posted April 1, 2019 (edited) 12 minutes ago, IIYAMA said: yes, every time you are going to write. Ehm, i tried to do it, on ClientResourceStop i saved data from file to variable, then delete a file, and create it again. But this don't wanna work. I hate using file functions. function onClientResourceStart() if not fileExists("settings") then local settingsFile = fileCreate("settings") if settingsFile then local jsonData = toJSON(playerData.playerItems) fileWrite(settingsFile, jsonData) fileFlush(settingsFile) fileClose(settingsFile) end end if fileExists("settings") then local settingsFile = fileOpen("settings") if settingsFile then local loadSettings = fileRead(settingsFile, fileGetSize(settingsFile)) local loadedData = fromJSON(loadSettings) fileFlush(settingsFile) fileClose(settingsFile) end end end addEventHandler("onClientResourceStart", resourceRoot, onClientResourceStart) function onClientResourceStop() if fileExists("settings") then local settingsFile = fileOpen("settings") if settingsFile then local jsonData = toJSON(playerData.playerItems) fileWrite(settingsFile, jsonData) fileClose(settingsFile) outputDebugString(jsonData) end end end addEventHandler("onClientResourceStop", resourceRoot, onClientResourceStop) This is how it looks for now, it's full code. Edited April 1, 2019 by majqq Link to comment
Scripting Moderators ds1-e Posted April 1, 2019 Author Scripting Moderators Share Posted April 1, 2019 55 minutes ago, IIYAMA said: yes, every time you are going to write. Okay nevermind. I got this to work somehow. Rewrited functions again. And now file content is correct. If something will change, or i will need help in something else, i'll let you know, thank you once again. 1 Link to comment
Scripting Moderators ds1-e Posted April 2, 2019 Author Scripting Moderators Share Posted April 2, 2019 (edited) 10 hours ago, IIYAMA said: Might be the reason for writing incomplete data. The file isn't flushed, so it might handy to do that as well: https://wiki.multitheftauto.com/wiki/FileFlush Oh and empty(recreate) the file each time you start writing data. @majqq The file is incorrect. There has been double written as you can see. (from a few moments before) se ] ] from: false, false ] ] Clean the file and try again. I need ask you once again. Last question. I want to store additional data for (each) item. Something like item_1, data_1, item_2, data_2. What will be the best way to do it? + It's possible to add another table to same file, and read this data? Edited April 2, 2019 by majqq Link to comment
Overkillz Posted April 2, 2019 Share Posted April 2, 2019 6 minutes ago, majqq said: I need ask you once again. Last question. I want to store additional data for (each) item. Something like item_1, data_1, item_2, data_2. What will be the best way to do it? I didn't read the previous replies, but you can follow this way. local myTableName = {} table.insert(myTableName, {itemName = "IDK-SomeName", dataValue = "12345", anotherData = "12345" } ) 1 Link to comment
Moderators IIYAMA Posted April 2, 2019 Moderators Share Posted April 2, 2019 2 hours ago, majqq said: I need ask you once again. Last question. I want to store additional data for (each) item. Something like item_1, data_1, item_2, data_2. What will be the best way to do it? + It's possible to add another table to same file, and read this data? As @Overkillz said, you can wrap Lua(tables in tables) and in JSON (objects/arrays in objects/arrays). Or, you can use XML to manage the JSON/tables as I said before. 1 1 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now