Jump to content

Run code once per game session on loading space center.


Recommended Posts

Alright.

In the mod in question, I do not have a master module that runs in all scenes, I have modules specific to each scene that only run in that scene.

I currently have a piece of code that runs in the GameScene.SpaceCenter OnStart method, so it runs everytime the player switches back from the VAB/SPH or flight scenes to the space center, as well as running when you load the game and switch from the Main Menu to the space center.

How would I go about running this code only when switching from the MainMenu to the space center, so once per game session? There is no "switching from (scene)" variable that I am aware of that I can query, and without a master module, I don't have the data persistence across scenes to save a value. I also don't want to write this to my config file as I specifically want it to reset when you quit the game.

Anyone have any ideas?

D.

Link to comment
Share on other sites

I'm not aware of such an event either..

But here's somewhat of a work around

Let your mod run also in main menu

in al scenes, you save a var named "current_scene_backup" or so with the name of the current scene..

if your actual scene is space center AND current_scene_backup is "mainmenu" you have at least skipped all other scene changes..

Additionally, you can there set an other bool-var named "spacecenter_done" to true, this could be checked in main menu => if a user switches back to main menu to reenter the game, the code will not run again.

I'm sure this will still leave some holes - but you might be better off allready..

Or try to write a "onSceneChange" event with the same methode to determine a changed scene in it..

you could share it as a CommunityUtil

Link to comment
Share on other sites

Why not something like this?:

[KSPAddon(KSPAddon.Startup.MainMenu, false)]
class MainMenuSentinel : MonoBehaviour
{
public static bool JustRan = true;

private void Awake() { JustRan = true; }
}

[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
class RunIfJustEnteredSpaceCenter : MonoBehaviour
{
private void Start()
{
if (MainMenuSentinel.JustRan)
{
MainMenuSentinel.JustRan = false;

/* your logic */
}
}
}

Link to comment
Share on other sites

Doesn't GameEvents.onGameSceneLoadRequested fire before HighLogic.LoadedGameScene changes? All you have to do is drop something like "public static bool run = false;" in your KSPAddon that needs to run when doing MainMenu->SpaceCenter. Then, when the game launches, you can simply add a method to onGameSceneLoadRequested that checks which scene it is changing to, and which is the current scene. Probably something like this:

public void OnSceneChange(GameScene scene)
{
if (scene == GameScenes.SpaceCenter && HighLogic.LoadedScene == GameScenes.MainMenu)
{
SpaceCenterAddon.run = true;
}
}

Link to comment
Share on other sites

@xEvilReeperx: So making the bool static does some trickery that keeps it in existence so the MainMenu start method can see it? I assumed that because it was running as a KSPAddon.MainMenu, it would be destroyed when leaving the main menu and so not available to the SpaceCenter Start code.

@stupid_chris: That sounds like while there is no variable for last loaded scene I can check, that GameEvent would work essentially the same from a logic perspective.

I'll run some tests and see what happens, thanks for the suggestions all.

D.

Link to comment
Share on other sites

@xEvilReeperx: So making the bool static does some trickery that keeps it in existence so the MainMenu start method can see it? I assumed that because it was running as a KSPAddon.MainMenu, it would be destroyed when leaving the main menu and so not available to the SpaceCenter Start code.

@stupid_chris: That sounds like while there is no variable for last loaded scene I can check, that GameEvent would work essentially the same from a logic perspective.

I'll run some tests and see what happens, thanks for the suggestions all.

D.

To answer two of your questions: static variables are constant to all the objects of a same type, if you modify it in one, it's modified for all the other objects fo the same type, as well as for all the objects of the same type that /will/ be created. Theres no real concept of loading/unloading with static variables. It's essentially a memory slot pinned to an object.

For the second part, as I said, there is no reference to the previous scene, but I'm pretty sure that the HighLogic.LoadedGameScene property is changed /after/ the GameEvent is fired, while the GameEvent. The GameEvent triggers in my experience a request to load a new scene. Since the scene isn't changed yet and the event passes the scene to be loaded as a variable, you can check where you are right now and where you're going, opposite to where you were, which is pretty much the same thing in terms of information.

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...