Jump to content

Make code execute on new game start & on game loads


Recommended Posts

You can assign your methods to GameEvents to do this.

To add your mehtods to the GameEvents use this in your Start or Awake method:

GameEvents.onGameStateCreated.Add(new EventData<Game>.OnEvent(yourMethod1));
GameEvents.onGameStateLoad.Add(new EventData<ConfigNode>.OnEvent(yourMethod2));

You should remove the methods in your OnDestroy method like this:

GameEvents.onGameStateCreated.Remove(new EventData<Game>.OnEvent(yourMethod1));
GameEvents.onGameStateLoad.Remove(new EventData<ConfigNode>.OnEvent(yourMethod2));

At last you have to create the two methods yourMethod1(Game game) and yourMethod2(ConfigNode node), where "game" and "node" are the objects that the GameEvent returns.

Edited by MoreUmpf
Link to comment
Share on other sites

That's some good information but I don't think it entirely answers his question. Unless I'm misinterpreting, he also wants to detect when a new game was actually created for the first time. onGameStateCreated triggers when a game is loaded and nothing identifies it as a new game.

paul23, if that's what you're looking for or need one-time code execution for a new game you should look into the Scenario class. Give it a flag to be set when you've initialized anything needing initializing so it knows not to do it again. Bind the events that MoreUmpf mentioned and any other events you need.

Here's some example usage of the Scenario class that I've coded

https://github.com/Starwaster/Ioncross-Crew-Support/blob/Dev/Source/IoncrossCrewSupport/IonLifeSupportScenario.cs

Here's another that I did for enneract's Real Roster to implement crew rotation per save game.

https://github.com/Starwaster/KSPRealRoster/blob/master/RRScenario.cs

Link to comment
Share on other sites

Does the state of those scenarios save/load when loading/saving? I change quite a few settings of the game - which are normally not in the save file, and I wish those settings to remain when saving/loading. (IE: location of the KSC on kerbin).

I tried the "onGameStateCreated"events but they actually also execute the moment you "Resume game" -before actually loading the save game, and also for each save game in the list. How to prevent this?

Ok to show what I wrote so far, (and an addon that triggers once you return to the main menu to set i back to 0).

    [KSPScenario(ScenarioCreationOptions.AddToNewGames, new GameScenes[]{
GameScenes.LOADINGBUFFER,
GameScenes.SPACECENTER,
GameScenes.FLIGHT,
GameScenes.EDITOR,
GameScenes.SPH,
GameScenes.TRACKSTATION
})]
class WSGameScenarioSpawn : ScenarioModule
{
public static int i = 0;
public override void OnLoad(ConfigNode node)
{
Debug.LogWarning("scenario onload" + i++);
if (i == 0) {
if (node.HasValue("omg omg")) {
Debug.Log("node has already the value: " + node.GetValue("omg omg"));
Debug.Log("Current main body: " + FlightGlobals.getMainBody().name);
} else {
Debug.Log("node value not found");
}
}
}
public override void OnSave(ConfigNode node)
{
Debug.LogWarning("scenario onsave" + i++);
node.AddValue("omg omg", "will it work?");
}
}

The problem I see is that OnSave doesn't trigger at saving? What to do so it will actually trigger?

Edited by paul23
Link to comment
Share on other sites

I haven't seen any problems with it not saving.

OnLoad / OnSave fire anytime the game loads or saves.

I've used it in three mods now and it works in all three. They all depend on it for various functionality. Deadly Reentry for difficulty settings that have to be stored on a per savegame basis. Ioncross to enable/disable the mod per save game and in enneract's Real Roster to store and manipulate the Kerbal roster. It's very dependable. Check output_log.txt for errors.

Actually, looking at your code... is that ACTUAL code? You can't put spaces in node names. It causes problems.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...