Jump to content

New SAS buttons?


Recommended Posts

First, I know C#
Second, I DO NOT know about the KSP Plugin API, a walk-through would be great.

What I want to do:
turning on SAS enables buttons that orient the current vessel in that particular direction, but I want to add 4 new buttons for North, East, South, West - to be used for asssisting rover and spaceplane piloting.

whenever these buttons are active, it should only control the yaw of the vessel, roll and pitch should remain unaffected.


in addition, should I succeed in this, I'd also like some information about releasing the mod on CKAN.

---thanks in advance! Edited by Xyphos
Link to comment
Share on other sites

This could easily be one heck of a way to get started :P

1) Starting with the basics, getting setup for compilation of plugins for KSP is covered on the [URL="http://wiki.kerbalspaceprogram.com/wiki/Plugins"]wiki[/URL] (NOTE: Compiling against .NET 3.5 is [B]NOT[/B] outdated information). It also has some vector math that'll give you your direction vectors in the examples.
2) There are two types of plugins that would be suited to this, depending on whether you require control for all loaded vessels or only the active one. A single instance plugin (best used for controlling only the active vessel) Inherits from MonoBehaviour and is tagged with the KSP addon attribute like so:
[code][KSPAddon(KSPAddon.Startup.Flight, false)] // KSP will create and call an instance every time you enter the flight scene
public class ExampleClass : MonoBehaviour { }[/code]

For multi vessel control, it's best to use a class inheriting [URL="http://forum.kerbalspaceprogram.com/threads/126229-Vessel-Modules-Discussion"]VesselModule[/URL]. Behaviour is near identical, but you get an instance per vessel instead of an instance per scene.

3) To control just one axis, you need to write your own control system that takes in an angle in some form (rad/deg/dot) and outputs a float between 1 and -1 that represents how much yaw authority will be used. Stock SAS controls all three axes as one and deactivates if it receives any user input (May or may not apply to the rover driving / translation bindings) so probably isn't suitable for the task.

4) GUI's are typically created using Unity's [URL="http://docs.unity3d.com/ScriptReference/GUI.Button.html"]GUI[/URL] or [URL="http://docs.unity3d.com/ScriptReference/GUILayout.Button.html"]GUILayout[/URL] classes. I don't know any scripter who likes GUI work, so uh, good luck...

I should note here that if you just want the functionality, Pilot Assistant's SSAS system should do the job (and if not it should be easy enough for me to make it work)
Link to comment
Share on other sites

That's something I've not seen before. I would check to see if other things from Assembly-C# are resolving (eg. see if it knows about the classes Part, Vessel, and/or FlightGlobals), but if that works and the attribute doesn't I really have no idea Edited by Crzyrndm
Link to comment
Share on other sites

OK, I fixed the reference error, but I'm still confused as to what I need to do to make it work.

I want to add a button to the stock toolbar, like the other mods do, toolbar mod isnt required, but would be nice.
I want that toolbar button to open a window, that window should have a label top center, and 9 radio/option buttons in a 3x3 grid, center being "off" or "disabled" state.
the others should direct SAS to turn to that direction when depressed.
when the window is closed, the disabled option should automatically selected.


..at least, that's how I envisioned things.
please bear in mind that this will be my first KSP plugin so excuse my noobishness. Edited by Xyphos
Link to comment
Share on other sites

As we don't really have a documented API, the most common way of figuring stuff out is to look at another mod's code.

I load my stock toolbar button for AGX this way: [url]https://github.com/SirDiazo/AGExt/blob/master/AGExt/Editor.cs#L456[/url]

The rest I'm not sure how to go about, maybe see how MechJeb does it's headings and vessel control stuff?

D.
Link to comment
Share on other sites

Here's an [URL="https://github.com/Crzyrndm/Pilot-Assistant/blob/master/PilotAssistant/PilotAssistantFlightCore.cs#L61-L64"]example[/URL] using [URL="https://github.com/Crzyrndm/Pilot-Assistant/blob/master/PilotAssistant/Toolbar/AppLauncherFlight.cs"]Stock[/URL] or [URL="https://github.com/Crzyrndm/Pilot-Assistant/blob/master/PilotAssistant/Toolbar/ToolbarMod.cs"]Blizzy Toolbar[/URL].

Window with a label top center => That normally comes from the text field of the window functions (eg. the blank string [URL="https://github.com/Crzyrndm/Pilot-Assistant/blob/master/PilotAssistant/PilotAssistantFlightCore.cs#L212"]here[/URL])
Array of buttons => You want the [URL="http://docs.unity3d.com/ScriptReference/GUILayout.SelectionGrid.html"]selection grid[/URL]. [URL="https://github.com/Crzyrndm/Pilot-Assistant/blob/master/PilotAssistant/FlightModules/PilotAssistant.cs#L1311-L1313"]Some example usage[/URL].

The control part is where things get fun. Simplest system is just some p-control (output = constant * error). Getting your current heading is done like [URL="https://github.com/Crzyrndm/Pilot-Assistant/blob/master/PilotAssistant/FlightModules/VesselData.cs#L77"]this[/URL] (backtrack a little in that function for the planet vectors), and subtract from your target heading for an error. Feed that error through the p-control, clamp it to <-1 - 1> and pass the output to the vessel.ctrlstate(.yaw or .wheelyaw, I don't know which will work here)

Setting Stock SAS is a little more complex, because it requires using Quats. These [URL="https://github.com/Crzyrndm/Pilot-Assistant/blob/master/PilotAssistant/FlightModules/SurfSAS.cs#L156-L157"]two lines[/URL] will generate a Quat facing the heading you want parallel to the surface which you then repeatedly assign to the [URL="http://vesModule.vesselRef.Autopilot.SAS.currentRotation"]SAS target[/URL] from memory. I remember there being a number of frames required between updates, but I've never actually dealt with this directly Edited by Crzyrndm
Link to comment
Share on other sites

That's because [URL="https://github.com/Crzyrndm/Pilot-Assistant/blob/master/PilotAssistant/Utility/GeneralUI.cs"]GeneralUI[/URL] is my static data store that I create when the player goes through the main menu the first time. I use it for storing a handful of custom skins so I can have things like [URL="https://github.com/Crzyrndm/Pilot-Assistant/blob/master/PilotAssistant/FlightModules/PilotAssistant.cs#L1296"]a toggle that looks like a button[/URL] without going through all the fuss of creating a GUIStyle to suit inline a dozen times (really, I ended up using it a lot less than I could have though. I could probably shift quite a few things into that class now I think about it).
Link to comment
Share on other sites

Not required at all. GUI.Skin holds all the styles used for the default appearances of the various UI elements and defaults to the Unity skin (mildly transparent fairly dark grey + white text). There is also the skin Squad uses (opaque medium grey + white/green text) which you can find at Highlogic.Skin IIRC (so GUI.Skin = Highlogic.skin; makes all your UI elements use squad's style).

Toggles that look like buttons is done by telling it to use the button style instead of the default style (you can actually do this with any of the controls, just not much use having a button look like a text box most of the time >.>)
[code]state = GUILayout.Toggle(state, "text", GUI.Skin.button)[/code]
I'm not really a fan of the default button style for buttons (the white background for selected just doesn't work for me), so I made a [URL="https://github.com/Crzyrndm/Pilot-Assistant/blob/master/PilotAssistant/Utility/GeneralUI.cs#L74-L81"]custom toggle style[/URL] that uses some colours that stand out nicely and the nice opaque background from the Highlogic skin Edited by Crzyrndm
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...