Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local defaultrundata = {teststring = "", testint = 0, testx = 0, testy = 0}; --self-explanatory
- local rundata = defaultrundata; --this is the actual mod data, different for each run, that you'll then access in your mod.
- --When I say data/data list, I mean the table containing all the rundata (the one right below here), which is only modified in the saveData function; when I say rundata, I mean the table containing all the different strings, numbers, whatever, which is modified thorough the mod's code (not in this case, as it's just an example, but whatever).
- --Return a table with, for keys that exist in both the source and target table, the values of the source table, and for values that only exist in the target, the values of the target table
- --We need to do this as simply doing, for example, table={a=1} and newtable = table, and then editing newtable.a like newtable.a=2, would also edit the variable in the old table.
- --In this mod, this would edit the defaultrundata, instead of simply setting the variables in the rundata to the default.
- --Also, in case the mod is updated and some variables not in the file are in the rundata, this will prevent them from being overriden with a nil when the file is loaded.
- --In our case, source is data.rundata[seed] or defaultrundata, and target is rundata (read the comments in loaddata for more info)
- function copyTable(source,target)
- local output = {}
- if not target then target = source end --This needs to be done, to prevent errors when using a nil as a table
- for k,v in pairs(target) do --For every variable in target (not in source as it might not have variables added in a new version)
- if source[k] ~= nil then --If the source contains the variable
- if type(source[k]) == "table" then --If the value of the variable is also a table, we need to do this again. (Actually, I'm not sure, but to be safe, do this)
- output[k] = copyTable(source[k],target[k]);
- else
- output[k] = source[k]; --If the value of k isn't a table, just copy it over to the output.
- end
- else --If the variable is only in target
- if type(target[k]) == "table" then --If the value of the variable is also a table, we need to do this again. (Actually, I'm not sure, but to be safe, do this)
- output[k] = copyTable(target[k]);
- else
- output[k] = target[k]; --If the value of k isn't a table, just copy it over to the output.
- end
- end
- end
- return output;
- end
- --DATA STRUCTURE:
- --data(saved table) = {
- -- rundata = {table with seeds as keys corresponding to that run's rundata},
- -- seeds = {table with ids as keys corresponding to that id's seed used for overwriting},
- -- lastid = x last id that has been saved
- --basically when the data for more than 10 runs is saved, the oldest one is deleted, and a new one is put at that id
- function saveData()
- local seed = Game():GetPlayer(0).InitSeed --This isn't the run's seed, but it's still different for every run.
- local data;
- if Isaac.HasModData(mod) then
- local loadeddata = Isaac.LoadModData(mod);
- data = load("return "..loadeddata)(); --This is the data list, the one explained above, which contains all the different rundata for each run
- if type(data) == "table" and data.lastid ~= nil then --There is some data saved and it's in the right format (meaning, structured like above).
- if data.seeds[data.lastid] == seed then --If the last data that has been saved is for the current run, then
- data.rundata[seed] = mdata; --Save the current rundata to the data list
- --We don't need to worry about using copyTable here, as the data table is only local
- else --If it's the first time you're saving data for this run
- --Increase the lastid by 1, or set it back to 1 if it's at 10.
- if data.lastid < 10 then
- data.lastid = data.lastid+1;
- else
- data.lastid = 1;
- end
- data.rundata[seed] = rundata; --save the current rundata to the data list
- if #data.rundata > 10 then --If the data list has more than 10 rundata, remove the oldest one (the one whose seed has the same id we're using)
- data.rundata[data.seeds[data.lastid]] = nil;
- end
- data.seeds[data.lastid] = seed; --Add our rundata to the data list.
- end
- else --There already is some data but it's not a table, and it isn't saved the right format. So, I will act like there's no preexisting data and use default data.
- data = {rundata={},seeds={},lastid=1}
- data.seeds[1] = seed;
- data.rundata[seed] = rundata;
- end
- else --First time saving data, it will just use a default data
- data = {rundata={},seeds={},lastid=1}
- data.seeds[1] = seed;
- data.rundata[seed] = rundata;
- end
- Isaac.SaveModData(mod, table_tostring(data)); --Save the data list to file.
- end
- function loadData()
- local seed = Game():GetPlayer(0).InitSeed --This isn't the run's seed, but it's still different for every run.
- if Isaac.HasModData(mod) then --if there is a data file then load it
- local data = load("return "..Isaac.LoadModData(mod))(); --load the string as a function, the extra () are to immediately run it
- if data.rundata[seed] == nil then --if the data for this run has never been saved, then use the default one
- rundata = copyTable(defaultrundata, rundata);
- else
- rundata = copyTable(data.rundata[seed], rundata);
- end
- else --if there isn't, just use the default one
- rundata = copyTable(defaultrundata, rundata);
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement