Heres a little script that looks for a config file when a scene loads it none is found it creates one and if one is found it loads the file from disk and applies the data in the read files.
Skyline does this automatically for the render settings upon run but this shows the basics to get your game data driven, allowing the user of your game to mod files to create the content that they want in your game.
The most basic way of making your game modable is to have your character settings such as health, damage, weapon damage all editable in a file like this even player money can be saved with the player settings and stats.
Well its a starting place for those not used to lua or new to coding.
- Code: Select all
obj = 0;
function onInit(objID)
obj = objID;
File_Folder = sky.getDir_ProjectOrGameFolder() -- project Or Game assets folder
-- Look to see if the settings file exists
local find_settings_file=io.open(File_Folder .. config.xml, "r");
if find_settings_file==nil then
Create_File(config.xml)
else
Read_File(config.xml)
end
end
function Create_File( filename )
if filename=="config.xml" then -- Explained below
currFSAA = render.getFSAA();
fullscr = render.getFullScreen();
currQual = render.getQuality();
currRes = render.getResolution();
rendStats = render.getShowStats();
vsync = render.getVsync();
local ConfigTable = {};
ConfigTable["Fullscreen"] = fullscr;
ConfigTable["FSAA"] = currFSAA;
ConfigTable["Resolution"] = currRes;
ConfigTable["Quality"] = currQual;
ConfigTable["Vsync"] = vsync;
ConfigTable["Show_Stats"] = rendStats;
jsonObj = rapidjson.object(ConfigTable);
rapidjson.dump(jsonObj, File_Folder .. "Config.xml", {pretty=true});
return
end
function Read_File( filename )
if filename=="config.xml" then -- Explained below
renderData = rapidjson.load(File_Folder .. filename);
render.setFSAA(renderData["FSAA"]);
render.setFullScreen(renderData["Fullscreen"]);
render.setQuality(renderData["Quality"]);
render.setResolution(renderData["Resolution"]);
render.setShowStats(renderData["Show_Stats"]);
render.setVsync(renderData["Vsync"]);
end
return
end
end
I used the if filename=="config.xml" then because you may want to load up other files at run time and you can use this if statement to separate the files that you are processing.
When opening other files you pass the file name to the create or read functions and preform your tasks within them functions.