Moddependent configdata | Any problems with this solution?

Moddependent configdata | Any problems with this solution?

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
chlue
Posts: 101
Joined: 28 Dec 2005, 20:48

Moddependent configdata | Any problems with this solution?

Post by chlue »

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.

Code: Select all

function widget:SetConfigData(data)
	Data = data
	return
end
This function writes Data back and adds/exchange the part, that matches the current mod with the actual variables.

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
So far, so good. To load this config i needed to extend the SetConfigData-function further:

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
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/
Post Reply

Return to “Lua Scripts”