Jump to content

xml vs json


ds1-e

Recommended Posts

  • Scripting Moderators
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 by majqq
Link to comment
  • Scripting Moderators
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 by majqq
Link to comment
  • Scripting Moderators
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. :P

  • Like 1
Link to comment
  • Scripting Moderators
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 by majqq
Link to comment
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" }	)
  • Like 1
Link to comment
  • Moderators
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.

 

  • Like 1
  • Thanks 1
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...