Jump to content

Recommended Posts

Posted (edited)

I've been trying to get a feel for Lua scripting in DCS, but I think I'm still a ways off. I'm trying to start small, then go from there - in this case what if I want to save the value of a set of flags from one mission and use them in a subsequent mission? Do I need to use Lua tables for that? Or can I just save flags/variables?

 

Basically here's the scenario:

 

MP Mission one - a small set of objectives are either met or not met; lets say 3 flags representing objectives achieved/failed are individually either 1 (true) or 0 (false). I'd like to save the state of those flags in a file (e.g. using io.open(..) for writing, then write/close the file, etc.).

 

MP Mission two - Read the file saved from mission one (e.g. using io.input(..), etc.) - based on the flag values, spawn specific groups of units to make the campaign flow semi-dynamic. At the end of MP mission two, write out another file, etc.

 

What's the best way to script this? At this point, any suggestions are appreciated.

 

Thanks!

Relent

Edited by 609_Relentov
Posted

For your needs, I would use the loadfile shown in MBot's DC Guardians script.

 

Save the flags by exporting a table to a file:

local function TableSerialization(t, i)													--function to turn a table into a string (works to transmutate strings, numbers and sub-tables)
	local text = "{\n"
	local tab = ""
	for n = 1, i + 1 do																	--controls the indent for the current text line
		tab = tab .. "\t"
	end
	for k,v in pairs(t) do
		if type(k) == "string" then
			text = text .. tab .. k .. " = "
			if type(v) == "string" then
				text = text .. "'" .. v .. "',\n"
			elseif type(v) == "number" then
				text = text .. v .. ",\n"
			elseif type(v) == "table" then
				text = text .. TableSerialization(v, i + 1)
			end
		elseif type(k) == "number" then
			text = text .. tab .. "[" .. k .. "] = "
			if type(v) == "string" then
				text = text .. "'" .. v .. "',\n"
			elseif type(v) == "number" then
				text = text .. v .. ",\n"
			elseif type(v) == "table" then
				text = text .. TableSerialization(v, i + 1)
			end	
		end
	end
	tab = ""
	for n = 1, i do																		--indent for closing bracket is one less then previous text line
		tab = tab .. "\t"
	end
	if i == 0 then
		text = text .. tab .. "}\n"														--the last bracket should not be followed by an comma
	else
		text = text .. tab .. "},\n"													--all brackets with indent higher than 0 are followed by a comma
	end
	return text
end


local exportData = "local t = " .. TableSerialization(MDC_data, 0) .. "return t"				--The second argument is the indent for the initial code line (which is zero)
	exportFile = io.open("MDC_campaign_results.lua", "w")
	exportFile:write(exportData)
	exportFile:close()

 

 

 

Load the saved file and run a loop to activate the names as flags:

local LoadCampaignData = loadfile("MDC_campaign_results.lua")

Posted

Thanks for the suggestions Xcom - so it seems tables are used for everything data-wise with DCS/Lua scripting. These look like hash tables - ill try these suggestions and see how far I get..

 

Thanks again!

Relent

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...