Jump to content

How can I make KSPField give real time response to a part that its been used on? (Parts Action Window)


Recommended Posts

I have a button that I press called "Turn On" and when I click on it it gives me current in game data once.

How do I have it give me current constant data like, the science parts giving temperature or atmospheric information?

I have KSPField determine the variable, but it needs to run within a KSPEvent to add the button and only updates on pressing the button.

I want to take away the button and just have it update the number automatically.

Any help is appreciated

omg. 2 minutes after I figure it out. Sorry

Edited by Anth12
Link to comment
Share on other sites

using UnityEngine;

namespace Example
{
    class ExampleModule : PartModule
    {
        public static void Log(string message)
        {
            //Debug.Log("Example:" + message);
        }

        [KSPField(isPersistant = false, guiActive = true)]
        public float exampleFloat = 0;

        public override void OnUpdate()
        {
            exampleFloat = this.part.persistentId;
        }

    }

    [KSPAddon(KSPAddon.Startup.Flight, false)]
    public class Example : MonoBehaviour
    {

        public static void Log(string message)
        {
            //Debug.Log("Example:" + message);
        }

        public void Start()
        {
            if (HighLogic.LoadedSceneIsFlight)
            {
                for (var i = 0; i < FlightGlobals.Vessels.Count; i++)
                {
                    for (var j = 0; j < FlightGlobals.Vessels.parts.Count; j++)
                    {
                        for (int k = 0; k < FlightGlobals.Vessels.parts[j].Modules.Count; k++)
                        {
                            Log(FlightGlobals.Vessels.parts[j].Modules[k].moduleName);
                            if (FlightGlobals.Vessels.parts[j].Modules[k].moduleName == "ModuleCargoPart" || FlightGlobals.Vessels.parts[j].Modules[k].moduleName == "ModulePartVariants" || FlightGlobals.Vessels.parts[j].Modules[k].moduleName == "ModuleIRVariant")
                            {
                                FlightGlobals.Vessels.parts[j].AddModule("ExampleModule", false);
                            }
                        }
                    }
                }
            }
        }
    }
}

That's an example of what the code is. Dont want to show what its for yet.

OnUpdate was what I was missing. 

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