Skip to content

Instantly share code, notes, and snippets.

@DemmyDemon
Created August 7, 2023 10:36
Show Gist options
  • Save DemmyDemon/582254f6fe2af651a8a9b8f47142f75b to your computer and use it in GitHub Desktop.
Save DemmyDemon/582254f6fe2af651a8a9b8f47142f75b to your computer and use it in GitHub Desktop.
Discussing loading of data files in FiveM
DefineNightCallback('editor-save-json', function(source, jsonString)
if IsPlayerAceAllowed(source, 'admin') then
local data = json.decode(jsonString)
if data and data.name then
local name = string.lower(data.name)
name = name:gsub("[^a-z.]", "_")
name = name .. '.json'
local saved = SaveResourceFile(GetCurrentResourceName(), 'areas/'..name, jsonString, -1)
if saved then
tellplayer(source,'Saved!', 'Area saved as', name)
else
tellplayer(source,'Not saved!', 'Something error happen!')
end
else
tellplayer(source,'Malformed data', 'The data you were trying to save was corrupted!')
end
else
NOPE(source)
end
end)
DefineNightCallback('editor-load-json', function(source, name)
if IsPlayerAceAllowed(source, 'admin') then
logplayer(source, name)
local fileName = name or 'unnamed'
fileName = fileName:lower()
fileName = fileName:gsub("[^a-z.]", "_")
if not fileName:match(".json$") then
fileName = fileName .. '.json'
end
local raw = LoadResourceFile(GetCurrentResourceName(), 'areas/'..fileName)
if raw then
local data = json.decode(raw)
if data then
tellplayer(source,'Loaded area', 'Editor access granted to edit saved area ~y~', data.name)
return data.name, raw
else
tellplayer(source,'Corruption!', 'For some unknown reason, the area',name,'has been corrupted!')
end
else
tellplayer(source,'No such area', 'No file named',fileName,'was found.')
end
else
NOPE(source)
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment