Jump to content

FlightCtrlState not working as I expect


Recommended Posts

Answered, see last post.

Okay, this is a pretty big question and I figure the FlightCtrlState module is important enough to deserve its own thread so it shows up on the search function.

First off, I am making a mod for controlling KSP via touchscreen and I have the flight control working as long as the SAS is turned off. As soon as the SAS is turned on, my button on screen stop working.

Trying different things has not worked so I'm hoping someone can help me out.

First, my setup:


//trim everything excess
//inside my OnWindow() I have this
if (GUI.RepeatButton(new Rect(101 + (3 * OSKWinScale), 25, 32 + OSKWinScale, 32 + OSKWinScale), PitchUpTx))
{
OSKPitchDown = true; //so OSKPitchDown is false until the button is pressed, and they becomes true as long as the button is held down. Note I'm using GUI.RepeatButton, not GUI.Button.
}

//OnWindow() ends, and start a new method

public void OSKAutoControl(FlightCtrlState OSKFlightCtrlState)
{

if (OSKPitchDown)
{
if (OSKFlipPitch)
{
OSKFlightCtrlState.pitch = -1f;
}
else
{
OSKFlightCtrlState.pitch = 1f;
}

} //end OSKAutoControl method


public void FixedUpdate()
{
OSKVessel.OnFlyByWire += OSKAutoControl; //apply actual control inputs here, this I think is the problem
}

So, when the button is down (using RepeatButton so this is true as long as the button is held), I'm inputting a pitch input of 1f.

Now, here's where I get confused.

If I leave the last line at:

OSKVessel.OnFlyByWire += OSKAutoControl;

My controls work, as long as SAS is off. As soon as I turn SAS on, my on screen buttons stop working.

If I change the last line from += to just =, so

OSKVessel.OnFlyByWire = OSKAutoControl;

, my buttons on screen work 100% of the time (so with SAS both on and off), but I'm overriding the SAS inputs so SAS is effectively off regardless of it's actual state.

Anyone know how to make it so the on screen keys work as keyboard keys and I can move my ship around with SAS on?

D.

Edited by Diazo
Link to comment
Share on other sites

Hm, looks like the new SAS does set values instead of modifying it. So you have to make sure your wire callback is called after those mod-unfriendly new SAS.

- Do not make the same mistake as the SAS module. Modify the values already in the flybywire data struct whenever possible instead of setting them. Though respect their valid ranges. This way you can have both.

- Do NOT add a callback multiple times. In the worst case you add anther callback every frame. So after lets say 500 frames your flybywire gets called 500 times per frame.

- One stupid... uhm, lets call it "easy"... solution to make sure your callback is called after SAS is to re-add you callback every frame. But please remove it, first.

So your basic approach would work after a few improvements and I haven't found any better one.

Link to comment
Share on other sites

Okay, so populate my mod's flightctrlstate with the current values somehow and modify them, I think I can do that.

However, I'm not clear on what you mean by call back.

If you mean the

public void OSKAutoControl(FlightCtrlState OSKFlightCtrlState)

method, that is not inside any Update() call, it is directly under the OSK namespace.

If you mean

OSKVessel.OnFlyByWire += OSKAutoControl;

, does not that need to be called every frame for smooth control?

I suspect I've cut too much code out of my example and you are assuming I've done something with the callback that I've missed that is causing this problem I'm seeing.

D.

edit: I just checked the API docs and they say the

public void OSKAutoControl(FlightCtrlState OSKFlightCtrlState)

call back does in fact get called once every FixedUpdate() automatically even though I don't have it inside the FixedUpdate() method.

Having said that, it gets called once per fixedupdate once registered, so how do I keep it from registering every frame? (I'm totally lost on this part.)

edit the 2nd: Wait, you mean I only need

OSKVessel.OnFlyByWire += OSKAutoControl;

once in my Start() or Awake() method, and then it will call

public void OSKAutoControl(FlightCtrlState OSKFlightCtrlState)

once every FixedUpdate() for me?

Edited by Diazo
Link to comment
Share on other sites

Callback is a method that is called back from someone elses code. Names like eventhandler or delegate can mean sth similar / the same. So yes, OSKFlightCtrlState is your callback method.

edit the 2nd: Wait, you mean I only need
OSKVessel.OnFlyByWire += OSKAutoControl;

once in my Start() or Awake() method, and then it will call

public void OSKAutoControl(FlightCtrlState OSKFlightCtrlState)

once every FixedUpdate() for me?

Yes, doing it once in Start would be enough to let the game call your eventhandler / callback / OSKAutoControl method once per update. If you have a problem with other mods / SAS overwriting your values you could do sth like

FixedUpdate(){
vessel.OnFlyByWire -= OSKAutoControl;
vessel.OnFlyByWire += OSKAutoControl;
}

This would remove and re-add your callback as the last in the list on every frame. It isn't a very elegant solution, though.

Link to comment
Share on other sites

Alright, it works significantly differently then I expected then.

I thought

OSKVessel.OnFlyByWire += OSKAutoControl;

was the actual passing of the modified inputs back to KSP (and so needed every FixedUpdate()), not that it was registering OSKAutoControl as what I needed to update to pass the inputs back.

So yes, the code snippet in my opening post is quite badly malformed to say the least.

I'll pull this apart and report back on how things go.

D.

Link to comment
Share on other sites

Bleh, for the record when the SAS is enabled, it overrides OnFlyByWire, you have to set vessel.VesselSAS.ManualOverride(true), do your control inputs, then set vessel.VesselSAS.ManualOverride(false) on the next update without a control input to resume sas control.

Anyways, thank you for the callback advice, I moved public void OSKAutoControl(FlightCtrlState OSKFlightCtrlState) to the Start() method and added the .ManualOverride = true logic when I'm trying to input controls and everything works.

D.

Link to comment
Share on other sites

I wonder if this should be reported as a bug to the developers, so that they can fix SAS to work with mod-provided user input. They probably just didn't consider this case when refactoring the SAS. I had this problem too in my mouse input patch for SteamGauges HUD.

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