Jump to content

How to control the vessel throttle from the plugin (without throttle resetting)


Recommended Posts

Hey everyone :)

So I've searched this forum for a solution on how to control a vessel from within a plugin. I did find an answer which suggested using the OnFlyByWire, but it is incapable of doing what I want. Here's the short story: I'm creating a mod that plays an authentic count-down sound on launch, and then finally launches the rocket automatically. The sound playing works, and the launching works. What I would like is to make the plugin automatically throttle full up before launch. To do this I've attempted something like this:


public void Start()
{
FlightGlobals.ActiveVessel.OnFlyByWire += new FlightInputCallback(fly);
}

And then do something like:


private void fly(FlightCtrlState e)
{
if (!didLaunch)
e.mainThrottle = 1.0f;
}

Now this works to a certain extend. As long as the rocket has not yet launched, but is still on the countdown, the bool didLaunch is false, and as such the main throttle remains 100%. The problem happens as soon as the rocket activates the stage automatically and launches, as at this point, e.mainThrottle does not remain at 100% but resets to 0%. I obviously need it to stay there.

How would I go about achieving this?

And as a bonus question if anything feels so inclinced to answer it :) Another problem I'm facing is that my plugin currently only activates the first stage. Unfortunately I do not know whether or not the user has used those launch legs that you need to decouple in order to fly off. If the user have used these, and they are in the first stage it is obviously not a problem. If, however, they are in, say, the second stage, my plugin would only activate the first stage and thus the rocket would not really have launched. I've thought that maybe my only option is to somehow check all the parts of the vessel and make sure those legs are not included in the list, and if they are, I will have to keep activating stages until the one with the legs have been activated. Would this approach work?

Thank you all very much for your time :)

Link to comment
Share on other sites

Hmmm.

I have two comments, first one is that for throttle I don't use the FlightCtrlState function. I just directly call the throttle via:

FlightInputHandler.state.mainThrottle = 0.5f;

sets the throttle to 50%. Note that you have to error trap this. If you pass an invalid value, you will screw the GUI up and lose throttle control until the Flight scene reloads.

For directional control, I do it slightly differently as well.

public void Start()
{
FlightGlobals.ActiveVessel.OnFlyByWire += MyAutoControl
}

public void MyAutoControl(FlightCtrlState MyFlightCtrlState)
{
MyFlightCtrlState.pitch = 1f;
}

would be equal to holding down S to pitch up. (I think up, can't remember if 1 or -1 is pitch up.)

On the launchclamps, you can check all parts on the active vessel to see if they are launch clamps (using part.name I think) and if there are, getting their stage via part.stage (or the actual command KSP uses). Careful with it though as I recall reading somewhere that the stage are backwards. (IE: a 10 stage vessel at launch, hitting the Spacebar activates stage 10, not stage 1)

Hope that helps.

D.

Edited by Diazo
Link to comment
Share on other sites

Hey Diazo! Thank you very much for the help! The FlightInputHandler did the trick! :)

Interesting about the launch clamps problem, I will try out this approach when I get the time!

Thank you very much for the help, it is much appreciated!

Link to comment
Share on other sites

You send it to the module, to be precise. ModuleResourceIntake has "public void ToggleAction (KSPActionParam param)" for example, so you stuff a param in there (which is often not actually used). For the launch clamp it looks like it's called "ReleaseClamp".

Link to comment
Share on other sites

Thank you for the kind help! :) It all worked out. Here's a small demonstration of the simple, yet fun, final product :)

I want to switch the countdown audio clip with something more authentic if I can find such an audio clip that has a license which allows it :)

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