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

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.

×