It occurs to me that sometimes I'd like a "Save and Quit" button. There are definitely times when you want to save and then exit, so giving a button for this scenario just seems like a nice quality of life boost for the user. I put the button between "Save" and "Close" so it should be hard to click by accident (though since you just saved, it's hardly a problem).
This code to do it when added to Window_SaveGameMenu.cs. It's the same code from the ordinary Save button, but with the QuitRequested line added. There is an unfortunate gotcha here, which is that if there's not some sort of sleep between queueing the Save Command and the Quit instruction, you lose the save which is why I have included a very hacky 5 second sleep. I'm sure there's a better technique though....
public class bSaveAndQuit : ButtonAbstractBase
{
//This button is like Save, but it also quits the game
public override void GetTextToShow( ArcenDoubleCharacterBuffer Buffer )
{
base.GetTextToShow( Buffer );
Buffer.Add( "Save And Quit" );
}
public override void HandleClick()
{
debug = true;
if ( iCampaignName.Instance.CampaignName.Trim().Length <= 0 )
iCampaignName.Instance.CampaignName = World_AIW2.Instance.Setup.MapType.InternalName + "." + World_AIW2.Instance.Setup.Seed;
Window_SaveGameMenu.Instance.OverallCampaignName = iCampaignName.Instance.CampaignName;
GameCommand command = GameCommand.Create( GameCommandType.SaveGame );
//generate the saveGame entry from the name and state of the game
DateTime dt = DateTime.Now;
//Generate a SaveGameData from the saveGame and campaignName boxes,
//along with game metadata
SaveGameData data = new SaveGameData( iSaveGameName.Instance.SaveName, World_AIW2.Instance.Setup.Seed,
World_AIW2.Instance.GameSecond, iCampaignName.Instance.CampaignName,
dt, World_AIW2.Instance.Setup.MasterAIType.Name, World_AIW2.Instance.Setup.Difficulty.Name );
data.setShortMapType( World_AIW2.Instance.Setup.MapType.InternalName );
command.RelatedString = data.ToString();
World_AIW2.Instance.QueueGameCommand( command, true );
Window_SaveGameMenu.Instance.Close();
System.Threading.Thread.Sleep(5000); /* If you call QuitRequested immediately after queuing the save command
it seems like sometimes the save doesn't take place.
Inserting a 5 seconds sleep seems to alleviate this,
but it feels incredibly hacky. Is there a way to insert a memory fence/barrier
or it's c# equivalent? */
Engine_AIW2.QuitRequested = true;
}
public override void HandleMouseover() { }
public override void OnUpdate() { }
}
And its companion code to KDL_UIWindows.xml
<element prefab_lookup_name="BasicText" x="52.0" y="9.0" y_align="Bottom" width="20" height="4" type_name="tCampaignHeader" />
<element prefab_lookup_name="BasicTextbox" x="66.0" y="9.0" y_align="Bottom" width="15" height="4" type_name="iCampaignName" />
<element prefab_lookup_name="ButtonBlueSmallerCenter" x="11.0" y_align="Bottom" y="3.0" width="12" height="6" type_name="bSaveGameName" />
<element prefab_lookup_name="ButtonBlueSmallerCenter" x="25.0" y_align="Bottom" y="3.0" width="12" height="6" type_name="bSaveAndQuit" /> <!-- This line in particular -->
<element prefab_lookup_name="ButtonBlueSmallerCenter" x="39.0" y_align="Bottom" y="3.0" width="12" height="6" type_name="bClose" />
[\code]