Jump to content
StealthIsKey

Is there any way to save a global variable and its value to a SavedGame file and have them loaded later?

Recommended Posts

I was trying to keep track of some status in gameplay and react as soon as certain condition's met, and I've written some code similar to that below. 

-- function to run on a unit of this type created
function KeepTrackAndReactOnCreated(self)
	if unitCnt == nil then
		unitCnt = {}	-- global variable
	end

	local plyr = ObjectTeamName(self)
	if unitCnt[plyr] == nil then
		unitCnt[plyr] = 1
	else
		unitCnt[plyr] = unitCnt[plyr] + 1
	end
	
	if unitCnt[plyr] >= 5 then
		ObjectGrantUpgrade(self, "Upgrade_SomeUpgrade")
	end
end

-- function to run on a unit of this type destroyed
function KeepTrackAndReactOnDestroyed(self)
	local plyr = ObjectTeamName(self)
	unitCnt[plyr] = unitCnt[plyr] - 1

	if unitCnt[plyr] < 5 then
		ObjectRemoveUpgrade(self, "Upgrade_SomeUpgrade")
	end
end

It's working just fine, until I saved the game and then loaded it...It seems that every time I load the SavedGame file, unitCnt doesn't exist, as if the global variable and its value were not saved to the SavedGame file.

Does anyone has any idea of how I can solve this problem? Is there any way to save a global variable and its value to a SavedGame file and have them loaded later? Or is it just impossible to do so?

Share this post


Link to post

To answer your question, yes you can make custom savegames with lua file io commands. If you use a timestamp you can load exactly the state you need. I made custom savegames in many variations in metamod (base templates, auto start config, stats save) . The source code is completely open so have a look and report how that works out. The main function you will have to work with is WriteToFile and LoadFile (from meta mod).

Edited by Mjjstral

Share this post


Link to post
On 20/10/2019 at 2:47 AM, Mjjstral said:

To answer your question, yes you can make custom savegames with lua file io commands. If you use a timestamp you can load exactly the state you need. I made custom savegames in many variations in metamod (base templates, auto start config, stats save) . The source code is completely open so have a look and report how that works out. The main function you will have to work with is WriteToFile and LoadFile (from meta mod).

Thanks for your reply. I'll check it out.

Share this post


Link to post

http://lua-users.org/wiki/PersistentTables
 

Usage as mentioned in the examples:
 

make_persistent(globals(),"C:/Anywhere/")


Also here is a prototype I made myself quickly that creates one lua savefile. You can then recreate the savestate with dofile(savefile).
 

function SaveLuaState(savepath)
	local Save =  ""
	for index,value in globals() do
		if type(value) == "number" then
			Save = Save .. index .. " = " .. value .."\n"
		elseif type(value) == "string" then
			if strfind(value,"\\") or strfind(value,"\n") then
				Save = Save .. index .. " = " .. strrep(strchar(91),2) .. value .. strrep(strchar(93),2) .. "\n"
			else
				Save = Save .. index .. " = " .. "'" .. value .. "'" .. "\n"
			end
		elseif type(value) == "table" and not strfind(index,"ObjID") then
			Save = Save .. index .. " = {"
			for t_index,t_value in value do
				if type(t_value) == "number" then
					if type(t_index) == "number" then
						Save = Save .. "\t[" .. t_index .. "]=" .. t_value .. ","
					elseif type(t_index) == "string" then
						Save = Save .. "\t['" .. t_index .. "']=" .. t_value .. ","
					end
				elseif type(t_value) == "string" then
					if type(t_index) == "number" then
						Save = Save .. "\t[" .. t_index .. "]='" .. t_value .. "',"
					elseif type(t_index) == "string" then
						Save = Save .. "\t['" .. t_index .. "']='" .. t_value .. "',"
					end		
				else
					--Save = Save .. "\n\t['" .. t_index .. "']=" .. t_value .. ","
				end
			end
			Save = Save .. "}\n" 
		end
	end
	local filehandle = openfile(savepath, 'w')
	if filehandle ~= nil then
		write(filehandle, Save)
		flush(filehandle)
		closefile(filehandle)	
	end
end

Next step is a lua savefile to game savefile identifier function that then loads the lua save. It could simply be executed in the ususal scipts.lua environment.
If your patient I will provide all that with the "Meta" dll framework. It hooks the save function and cares for everything automatically so modders doesn't have to care anymore about lost lua vars in saves.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×