1 people like it.

Kerbal Space Program Unity Add-on

A snippet with a working (minimal) example of a Kerbal Space Program (www.kerbalspaceprogram.com) add-on in F#. The snippet is intended to help you get started with Unity add-on development in F#.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
namespace RodhernF

open UnityEngine
open KSP

[<KSPAddon(KSPAddon.Startup.EveryScene, true)>]
type RodhernF () = 
    inherit MonoBehaviour ()
    
    /// Write message to the KSP.log file.
    let log s =
        do Debug.Log ("[RodhernF] " + s)
   
    /// Write message on screen and to log file.
    let msg s =
        do log s
        do ScreenMessages.PostScreenMessage(s, 5.f) |> ignore
    
    /// A handler called when a change of scene is requested.
    let onSceneSwitchRequest =
        let handler (actionFromTo: GameEvents.FromToAction<GameScenes,GameScenes>) =
            do sprintf "Going from '%A' to '%A'."
                       (actionFromTo.``from``) (actionFromTo.``to``)
               |> msg
        new EventData<_>.OnEvent(handler)
    
    /// A handler called when a vessel is selected in the launch dialog.
    let onVesselSelect =
        let handler (template: ShipTemplate) =
            do sprintf "Selected '%s'."
                       (template.shipName)
               |> msg
        new EventData<_>.OnEvent(handler)
    
    /// This is the method that KSP/Unity will invoke.
    member public rodhernf.Start () =
        do log "rodhernf.Start begin"
        GameEvents.onGameSceneSwitchRequested.Add onSceneSwitchRequest
        GameEvents.onGUILaunchScreenVesselSelected.Add onVesselSelect
        do log "rodhernf.Start end"
Multiple items
type RodhernF =
  inherit obj
  new : unit -> RodhernF
  member Start : unit -> 'a

Full name: RodhernF.RodhernF

--------------------
new : unit -> RodhernF
val log : value:'T -> 'T (requires member Log)

Full name: Microsoft.FSharp.Core.Operators.log
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
member RodhernF.Start : unit -> 'a

Full name: RodhernF.RodhernF.Start


 This is the method that KSP/Unity will invoke.
Raw view Test code New version

More information

Link:http://fssnip.net/rD
Posted:9 years ago
Author:Robert Nielsen
Tags: ksp , addon , plugin , mod , unity , template