Created
August 7, 2023 10:36
-
-
Save DemmyDemon/582254f6fe2af651a8a9b8f47142f75b to your computer and use it in GitHub Desktop.
Discussing loading of data files in FiveM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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