Jump to content
Assaultimon

Trying to determine at mission start if save was loaded

Recommended Posts

Hi,

 

I converted a simple SP mission to MP. I'm using the Ravage mod, but for this example i guess it doesn't really matter.

I just want to know during execution of my init.sqf if the mission was loaded from a save game or is in fact a new mission.

 

This is my setup:

 

description.ext

author = "Haleks";
OnLoadName = "Ravage";
overviewPicture = "rvg_missions\images\altis.jpg";
overviewText = "Linger and survive in the wastelands of Altis.";
loadScreen = "rvg_missions\images\altis.jpg";
briefing = 0;

class CfgFunctions
{
	class tim
	{
		class mis
		{
			class myInit
			{
				file = "preInit.sqf";
				preInit= 1;
			};
		};
	};
};

 

/%MISSION_ROOT%/preInit.sqf

tim_fnc_myInit= {

	wasLoadedFromSave= false;
	addMissionEventHandler ["Loaded",{wasLoadedFromSave= true;}];

};

//[] call tim_fnc_myInit;

 

init.sqf

[] call tim_fnc_myInit;

if(wasLoadedFromSave) then
{
	systemchat "Load savegame..";
	diag_log format ["%1, %2 : Load save ------------ timon ------", player, time];
}
else
{
	systemchat "Init new game..";
	diag_log format ["%1, %2 : No save found ------------ timon ------", player, time];
};

 

I'm not sure from where to call tim_fnc_myInit or if it is automatically execute pre-initialization. 

 

Result of this approach:

While loading mission error "Undefined variable 'wasLoadedFromSave' ".

 

My conclusion tim_fnc_myInit was never called in the first place or the global variable isn't recognized by the init script.

 

I hope you can point me to what i'm doing wrong here.

Share this post


Link to post
Share on other sites

Mission eventHandler Loaded?

Share this post


Link to post
Share on other sites
16 minutes ago, Larrow said:

Mission eventHandler Loaded?

 

yep, that's what i'm doing. So... that's good right?

Share this post


Link to post
Share on other sites

Currently your CfgFunctions is compiling tim_fnc_myInit and then running it( preInit ), but your function( the contents of preInit.sqf ) also includes a function definition called tim_fnc_myInit which will be ignored as CfgFunctions has already made a function called exactly the same, which is compiled final so cannot be overwritten.

Then init.sqf calls tim_fnc_myInit, which again tries to define the same function, which again is ignored as it has already been compiled final by CfgFunctions.

So wasLoadedFromSave will never be defined( always nil ) hence the undefined error.

 

Maybe something like...

Spoiler

//description.ext

class CfgFunctions
{
	class TIM	//TAG
	{
		class mission
		{
			file = "functions";	//PATH
			class loadedFromSave { preInit = 1; };	//CLASSNAME file name minus proceeding fn_ and .sqf
		};
	};
};

//Will compile the contents of /%MISSION_ROOT%/PATH/CLASSNAME( loadedFromSave )
//So
// /%MISSION_ROOT%/functions/fn_loadedFromSave.sqf
//into
// TAG_fnc_CLASSNAME
//So
// TIM_fnc_loadedFromSave
//and then call the function( preInit )

//%MISSION_ROOT%/functions/fn_loadedFromSave.sqf

//This only happens if a saved game is loaded
addMissionEventHandler[ "Loaded", {
	params[ "_saveType" ];

	//RPT save type
	diag_log format[ "Mission loaded from %1", _saveType ];

	//Set var as load type STRING
	wasLoadedFromSave = _saveType;

	//You can do something here when loaded from save

}];

//initPlayerLocal.sqf, initServer.sqf OR init.sqf
//Where ever you need to know about mission loading

_nul = [] spawn {

	//Need a persistent loop that never ends
	//So that it is still running from a loaded save game
	while { true } do {

		//Wait for mission to start
		//For testing so as to make sure we see systemChat message
		waitUntil{
			(
				( isMultiplayer && { getClientStateNumber == 10 } ) ||
				( !isMultiplayer )
			) && { !isNull findDisplay 46 && { BIS_fnc_startLoadingScreen_ids isEqualTo [] } }
		};

		//Check if the global var has been created by Loaded EH
		_msg = if ( isNil "wasLoadedFromSave" ) then {
			wasLoadedFromSave = false;

			//Do something here when mission is started from new

			"Mission started from new";
		}else{
			wasLoadedFromSave = true;

			//Do something here when a saved mission is loaded

			"Mission loaded from save";
		};

		//Show some debug output
		systemChat _msg;
		diag_log _msg;

		//Wait here until the next time the mission is loaded from a save
		//If the mission is loaded then the Loaded EH will set wasLoadedFromSave as STRING save type
		//If the mission is restarted from new then so will this script so none of this matters
		waitUntil { sleep 1; wasLoadedFromSave isEqualType "" };
	};
};

TEST_MISSION

 

  • Like 4

Share this post


Link to post
Share on other sites

Wow! Thanks so much for the comprehensive answer. I'd never have figured that out by myself. I totally missed that the whole sqf-file was actually compiled INTO a function.

 

Also, very powerful persistent "main" loop. Still have a lot to learn, but this is a great template to start from.

 

And thanks for adding a mission example, too. Really great help, appreciate you took the time.

 

  • Like 2

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×