Jump to content

[SOLVED] Action Groups - hacks and tests, RUNTIME WORKS !!!


Recommended Posts

One last ditch effort.

I can add and remove actions groups in runtime in my PartModule plugin, but they can't persist from editor to flight (probably also on vessel load/spawn), so i gave up on that front.

To elaborate : i've tried with Action Groups Extended by  @Diazo to have a look into mechanics of action groups in flight scene, and while i can add actions on the fly and use them, KSP "forgets" them from SPH/VAB to vessel launch.

Next, i've discovered by "type - compile - run - duck and cover" testing i can add multiple [KSPAction("somename"]) above a single function and have fully working multiple actions that persist from editor to flight scenes ! What bugs me is that i can't find out WHICH KSPAction attribute fired the function, there is no apparent link to trace back to corresponding action. Sigh...

Example:
 

[KSPAction("action one")]

[KSPAction("action two")]

[KSPAction("action three")]

public void DoSomething(KSPAction param)

{

// ...stuff.. note that received "param" has only [float] Cooldown, [KSPActionGroup] group and [KSPActiontype] type, no backtracking from this

}

If there is a way to determine a calling action, this could save some space and simplify code.

As one can guess from the above, i really would like to save up on repeating the same blocks of code (i need a lot of them for my plugin).

This is me more sharing this discovery rather than me following up on this, i'll take the usual one-attribute-one-function route until this is resolved either as a failure or a success.

Edited by fatcargo
finally resolved the issue
Link to comment
Share on other sites

WOOHOO ! I found a way for a runtime added action group to persist from editor to vessel launch !!! And AGX recognizes it, no NREs !!!

Here is what i've done.

public override void OnSave (ConfigNode node)
{
    node.AddNode("ACTIONS").AddNode("base action name 1").AddValue("actionGroup","None");
}

public override void OnStart (StartState state)
{
    if (HighLogic.LoadedSceneIsEditor) SingleRuntimeAction();
}

public override void OnLoad (ConfigNode node)
{
    if (!HighLogic.LoadedSceneIsEditor) SingleRuntimeAction();
}

public void SingleRuntimeAction()
{
    KSPAction ksp_action = new KSPAction("AG test 1", KSPActionGroup.None, false, false);
    BaseAction base_action = new BaseAction(Actions,"base action name 1",
    (a) =>
    {
        ScreenMessages.PostScreenMessage("test 1");
    },
    ksp_action);
    Actions.Add(base_action);
}

After inserting this in your PartModule, add this group (it's name is displayed in action group editor as "AG test 1") to custom group 1, launch vessel and then activate action group. Watch screen message appear. Note how "base action name 1" is defined in BaseAction class and later added in config node.

BEFORE the above test, i had also written two functions with KSPAction[] attributes BuiltInAction() and BuiltInAction2().

This is what config node contained during OnSave() :

{MODULE
{
    stagingEnabled = True
    isEnabled = True
    name = TestPartModule
    EVENTS
    {
    }
    ACTIONS
    {
        BuiltInAction
        {
            actionGroup = None
        }
        BuiltInAction2
        {
            actionGroup = None
        }
    }
}
}

During OnLoad() vessel's config node has simiar (if not exactly the same) content. Above config node sample shows also EVENTS{}, but currently i'm not interested in these. If the above test proves stable (though everything seems straightforward), this should add one more configuration option in my part module i'm developing.

For me, this would be next to impossible to discover without excellent guide about debugging with VS/MonoDevelop by @sarbian . Note that the above works with Xamarin Studio Community Edition (i've got it by trying to download MonoDevelop, links there are replaced with Xamarin Studio CE).

Also a great help is Action Group Extended by @Diazo which i used to visualize and test action groups (if i didn't do something right, AGX would start throwing NREs, even before i tinkered with action groups).

Edited by fatcargo
Link to comment
Share on other sites

  • 3 weeks later...

More problems and ... more questions.

I've discovered that i've made a mistake in above example code.

The line with "new BaseAction(Actions,"name of action"" ... etc should have "name of action" string as valid identifier for C# and ConfigNode ie no whitespace whatsoever, no preceding numbers etc. This can be tested by writing a simple function with [KSPAction] attribute and then checking for it's name property as Debug.Log(Actions["YourActionFunction"].name) .

"name" property will match the name used in array operator to reference this specific action.

Not also that i have another roadblock. Again my example shows how to add runtime actions to config but that is also "static" as it ALWAYS adds those actions.

 

While developing my plugin, i have managed to:

-load and parse config with PartModule.OnStart() during editor scene

-save config with my additional ConfigNode in PartModule.OnSave() in editor into craft file

 

If i understand this correctly, when launching a craft from editor, PartModule.OnSave() is called three times : 1st for saving to craft file, 2nd to save with craft file with added "autosaved ship" string and 3rd time into persistence.sfs file.

However, i did not managed to retain my custom ConfigNode while changing scenes from

editor to flight (aka when launching a craft), it gets lost on scene change.

How do i keep data i saved in craft file to transfer to persistence file (the above mentioned 3rd step) ?

Link to comment
Share on other sites

  • 2 weeks later...

Another update. Hope this closes the issue. My previous source example may look functional but it is essentialy flawed. Although craft's ConfigNode contains info on action groups (either auto pre-assigned or user-defined), they should be maintained by game itself, no tinkering should be done in OnSave() / OnLoad() phase (unless some more magic need be done :) ).

I've found way to make action groups in runtime and maintain their persistence between various scenes. Note if runtime action groups are not found by game they won't be listed and available to user.

Use of OnStart () to create action groups is wrong for flight scene. It is too late and interestingly, only works for custom-assigned action groups. However, using OnAwake() also solves this for persisting pre-built action groups (abort,sas,rcs, stage etc) as this event fires early enough to create runtime action groups before game starts trying to match them to vessel config inside persistence.sfs.

Example code

        public override void OnAwake ()
        {
            string guiName = "Name Displayed To Player";
            string validIdentifierName = "runtimeAction";
            //make your choice from below.
            //note the "custom" is assigned to None by default so user can later decide
            string choice = "";
            KSPActionGroup group = KSPActionGroup.None;
            switch (choice)
            {
                case "abort": group = KSPActionGroup.Abort; break;
                case "gear": group = KSPActionGroup.Gear; break;
                case "light": group = KSPActionGroup.Light; break;
                case "sas": group = KSPActionGroup.SAS; break;
                case "rcs": group = KSPActionGroup.RCS; break;
                case "stage": group = KSPActionGroup.Stage; break;
                case "custom": group = KSPActionGroup.None; break;
                default : group = KSPActionGroup.None; break;
            }
            //note in next line that last parameter is "isPersistent" bool set to "true" !
            KSPAction ksp_action = new KSPAction(guiName,group,false,true);
            BaseAction base_action = new BaseAction(Actions,validIdentifierName,
                         (a) => {
                         ScreenMessages.PostScreenMessage("Your runtime action has fired !");
                         },ksp_action);
            Actions.Add(base_action);
        }

 

Edited by fatcargo
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...