Jump to content

Get a list of all ActionGroups that have actions in them


Recommended Posts

I'm writing a small mod that exports the current state of the vessel to json. I've been searching this forum and the API for about three hours now without finding any hints on how to compile a list of ActionGroups that actually do stuff with the ship. The ActionGroups-Array on the ship only tells me, if they are toggled or not. Is there an easy way to get all ActionGroups that affect at least one part in the vessel?

Also I'm having trouble finding a oneliner to figure out whether RCS is switched on or off. SAS was easy via Vessel.Autopilot.Enabled.

Link to comment
Share on other sites

14 minutes ago, FauserneEist said:

I'm writing a small mod that exports the current state of the vessel to json. I've been searching this forum and the API for about three hours now without finding any hints on how to compile a list of ActionGroups that actually do stuff with the ship. The ActionGroups-Array on the ship only tells me, if they are toggled or not. Is there an easy way to get all ActionGroups that affect at least one part in the vessel?

Also I'm having trouble finding a oneliner to figure out whether RCS is switched on or off. SAS was easy via Vessel.Autopilot.Enabled.

Try "FlightInputHandler.RCSLock". I think that's it.

Link to comment
Share on other sites

I have gone through finding all actions myself and the only way to do it is to cycle through every partModule on every Part and check it's ActionGroups array to see if that Action is assigned or not.

Get all actions on a vessel:

defaultActionsListAll.Clear(); //a List<BaseAction> obejct
                        {
                            foreach (Part p in FlightGlobals.ActiveVessel.parts)
                            {
                                defaultActionsListAll.AddRange(p.Actions);
                                foreach (PartModule pm in p.Modules)
                                {
                                    defaultActionsListAll.AddRange(pm.Actions);
                                }
                            }
                        }
  //defaultActionsListAll now holds every action on the vessel across all partModules/Parts

 

Check for assigned actions:

public void RefreshDefaultActionsListType()
        {
            defaultActionsListThisType.Clear(); //another List<BaseAction> object, note defaultActionsListAll and defaultActionsListThisType are different
            foreach (BaseAction act in defaultActionsListAll)
            {
                if ((act.actionGroup & defaultGroupToShow) == defaultGroupToShow) //defaultGroupToShow the KSP action enum, replace with KSPActionGroup.Light to find all actions assigned to the Lights action group.
                {
                    defaultActionsListThisType.Add(act);
                }
            }
        }
              //defaultActionslistThisType now contains all actions assigned to the specified action group

 

Actions are something of a pain to work with, it's just a matter of working the issue until it cooperates.

D.

Link to comment
Share on other sites

 

I managed to figure it out thanks to you. I ended up with the following function:

        private void findUsableActionGroups()
        {
            List<BaseAction> allActionsList = new List<BaseAction>();

            foreach (KSPActionGroup group in Enum.GetValues(typeof(KSPActionGroup)).Cast<KSPActionGroup>())
                this.actionGroups.Add(group, false);

            foreach(Part p in currentVessel.parts)
            {
                allActionsList.AddRange(p.Actions);
                foreach(PartModule pm in p.Modules)
                    allActionsList.AddRange(pm.Actions);
            }

            foreach (BaseAction action in allActionsList)
                foreach (KSPActionGroup group in Enum.GetValues(typeof(KSPActionGroup)).Cast<KSPActionGroup>())
                    actionGroups[group] = actionGroups[group] || ((action.actionGroup & group) == group);

        }

I end up with the following dictionary:

Dictionary<KSPActionGroup, Boolean> actionGroups

... which contains a nice list of all action groups in the game with the Boolean value indicating, whether they are actually in use or not. Thanks a lot for the help.

Advice to anyone who might be interested in copying the listed functions: use them only on vessel changes, since they are quite expensive, runtime-wise.

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