Moddependent configdata | Any problems with this solution?
Posted: 22 Mar 2007, 22:20
The idlebuilder widget needs moddependent data. Till now this data is hardcoded in the LUA-File, but i wanted to save it. There are the functions SetConfigData and GetConfigData to archieve this. When the game starts, the function SetConfigData, Initialize and GetConfigData(I have no clue why) are called and when the widget is disabled, the function GetConfigData is called
This Funtion only takes the old data and stores it in the global widget variable Data.
This function writes Data back and adds/exchange the part, that matches the current mod with the actual variables.
So far, so good. To load this config i needed to extend the SetConfigData-function further:
It seem to work as far as I tested this till now. It will probably make trouble if the config has a wrong structure, but thats a minor problem and easy solveable. My question are:
- Are their further problems with this?
- Is there allready a much better way to archieve this?
- Why is GetConfigData() called when the game starts, too?
- Is there a way to avoid storing the Data, so that less ram is wasted?
If you wan't to look at this code ingame, you can download this file till 29.03.07 here: http://spring.unknown-files.net/file/25 ... y_version/
This Funtion only takes the old data and stores it in the global widget variable Data.
Code: Select all
function widget:SetConfigData(data)
Data = data
return
end
Code: Select all
function widget:GetConfigData()
local modname = Game.modName
local pos = 1
local tbl = {}
if Data == nil then -- no old config
tbl.moddata = {}
else
if Data.moddata ~= nil then -- there was an old modconfig loaded
pos=table.getn(Data.moddata)+1
for num=1, table.getn(Data.moddata) do -- we will check all stored modconfigs
echo("Dataset " .. num .. " : " .. Data.moddata[num].modname)
if Data.moddata[num].modname ==Game.modName then
pos = num -- this config matches our current mod, so we will overwrite it
end
end
else
pos=1 -- we will write the first modconfig
tbl.moddata = {}
end
tbl = Data -- copy all the old data
end
echo("Saving modconfig at position " .. pos)
tbl.moddata[pos] = {
modname = Game.modName,
FacChoices = FacChoices,
MobileBuilderList = MobileBuilderList,
StationaryBuilderList = StationaryBuilderList
} -- overwriting the old data with our current for the current mod, or adding it to the end of the list
tbl.IdleTime = IdleTime
tbl.spacingFromBottom = spacingFromBottom
tbl.showStationaryUnits = showStationaryUnits
return tbl
end
Code: Select all
function widget:SetConfigData(data)
Data = data
if Data ~= nil then -- load modindependent variables
IdleTime = Data.IdleTime
spacingFromBottom = Data.spacingFromBottom
showStationaryUnits = Data.showStationaryUnits
end
local modname = Game.modName
local pos = -1
if Data.moddata ~= nil then -- there was an old config loaded
echo("Modname: " .. Game.modName)
for num=1, table.getn(Data.moddata) do -- we will check all stored configs
echo("Dataset " .. num .. " : " .. Data.moddata[num].modname)
if Data.moddata[num].modname ==Game.modName then
pos = num -- this config matches our current mod
end
end
if pos ~= -1 then -- we have a match
echo("Loading Dataset " .. pos)
FacChoices = Data.moddata[pos].FacChoices
MobileBuilderList= Data.moddata[pos].MobileBuilderList
StationaryBuilderList= Data.moddata[pos].StationaryBuilderList
end
end
return
end
- Are their further problems with this?
- Is there allready a much better way to archieve this?
- Why is GetConfigData() called when the game starts, too?
- Is there a way to avoid storing the Data, so that less ram is wasted?
If you wan't to look at this code ingame, you can download this file till 29.03.07 here: http://spring.unknown-files.net/file/25 ... y_version/