Current Version: Querying...

From v3 and onwards I have provided a wrapper file so that other mods can access the Alarm Clocks alarm list to modify existing alarms, create new alarms and delete alarms. This can be used by other mods to create time based alarms

This page is under construction - the work is done and you can see an example in the API Tester on github if you are super eager

Implementation Details

To access the API from your plugin is a couple of simple (I hope) steps:

  1. Add the KACWrapper.cs file to your project
    https://github.com/TriggerAu/KerbalAlarmClock/blob/master/KerbalAlarmClock/API/KACWrapper.cs
  2. Edit the Namespace definition at the top of the KACWrapper you have added to be specific to your plugin
    Wrapper Before
    // TODO: Change this namespace to something specific to your plugin here.
    //EG:
    // namespace MyPlugin_KACWrapper
    namespace KACWrapper
    {
    ...
                
    Wrapper After
    // TODO: Change this namespace to something specific to your plugin here.
    //EG:
    // namespace MyPlugin_KACWrapper
    namespace KACAPITester_KACWrapper
    {
    ...
                
  3. Add a using statement to the top of your codefile to use your new namespace
  4. In the Start Method of your Monobehaviour (or anytime after that) Initialise the wrapper. ie. you need to do this after KSP has awaken all the DLLs, if you try and Init the wrapper in the Awake method then the alphabetical order of loading may cause a failure
    internal override void Start()
    {
        KACWrapper.InitKACWrapper();
        if (KACWrapper.APIReady)
        {
            //All good to go
            Debug.Log(KACWrapper.KAC.Alarms.Count);
        }
    }
    

Exposed Methods and Properties

The KACWrapper exposes three types - The KAC itself, an KACAlarm (the details of a single alarm object) and a KACAlarmList (a list of all the current alarms). The wrapper has XML commenting throughout so that the autocomplete text should prompt with suitable details, and here's some more detail

Type details to go here

class  KACWrapper The hook in to the KAC Assembly

propAPIReadyBooleanWhether the object has been wrapped and the APIReady flag is set in the real KAC
propAssemblyExistsBooleanWhether we found the KerbalAlarmClock assembly in the loadedassemblies
propInstanceExistsBooleanWhether we managed to hook the running Instance from the assembly
methodInitKACWrapperThis method will set up the KAC object and wrap all the methods/functions
fieldKACKACAPIThis is the Kerbal Alarm Clock object that you can access via the API once wrapped

class  KACAPI The Kerbal Alarm Clock Object with attached props and methods

propAPIReadyBooleanWhether the APIReady flag is set in the real KAC
propAlarmsKACAlarmList (List<KACAlarm>)The list of Alarms that are currently active in game
methodCreateAlarmStringCreate a New Alarm
Takes an AlarmTypeEnum for the type of alarm, a String for the Name and a Game UT and returns the ID of the newly created alarm
methodDeleteAlarmBooleanDelete an Alarm
Takes a String of an Alarms ID and returns A Success Boolean
eventonAlarmStateChangedEvent that fires when the State of an Alarm changes

class  KACAlarm An Alarm and its properties

propAlarmActionAlarmActionEnumWhat should the Alarm Clock do when the alarm fires
propAlarmMarginDoubleIn game seconds the alarm will fire before the event it is for
propAlarmTimeDoubleIn game UT value of the alarm
propAlarmTypeAlarmTypeEnum (ReadOnly)What type of Alarm is this - affects icon displayed and some calc options
propIDString (Readonly)Unique Identifier of this alarm
propNameStringShort Text Name for the Alarm
propNotesStringLonger Text Description for the Alarm
propRemainingDouble (Readonly)How much Game time is left before the alarm fires
propRepeatAlarmBooleanShould the alarm be repeated once it fires
propRepeatAlarmPeriodDoublehow long after the alarm fires should the next alarm be set up
propVesselIDStringUnique Identifier of the Vessel that the alarm is attached to
propXferOrginBodynameStringname of the body the vessel is departing from
propXferTargetBodynameStringname of the body the vessel is arriving at

Example Usage

Below you can see simple examples of ways you can use some of the properties and Methods above

  1. List the current Alarms
    if (KACWrapper.APIReady)
    {
        //Get the list of alarms from the KAC Object
        KACWrapper.KACAPI.KACAlarmList alarms = KACWrapper.KAC.Alarms;
        Debug.Log(String.Format("Alarms Count:{0}", alarms.count ));
        
        //Now loop through them to display some details
        foreach (KACWrapper.KACAPI.KACAlarm a in alarms)
        {
            Debug.Log(String.Format("{0}-{1}-{2} ({3})", a.Name, a.AlarmType,a.Notes,a.ID ));
        }
    }
    
  2. Create a new Alarm
    if (KACWrapper.APIReady)
    {
        //Create a raw alarm 15 mins from now game time and get the id back
        String aID = KACWrapper.KAC.CreateAlarm(KACWrapper.KACAPI.AlarmTypeEnum.Raw, "New Alarm", Planetarium.GetUniversalTime() + 900);
        
        if (aID !="") {
            //if the alarm was made get the object so we can update it
            KACWrapper.KACAPI.KACAlarm a = KACWrapper.KAC.Alarms.First(z=>z.ID==aID);
            
            //Now update some of the other properties
            a.Notes = "This is a test raw alarm with some notes";
            a.AlarmAction = KACWrapper.KACAPI.AlarmActionEnum.PauseGame;
            ...
        }
    }
    
  3. Delete an Alarm
    if (KACWrapper.APIReady)
    {
        //Get the list of alarms from the KAC Object
        KACWrapper.KACAPI.KACAlarmList alarms = KACWrapper.KAC.Alarms;
        if (alarms.count > 0) {
            //Store the name and ID
            String aName = alarms[0].Name;
            String aID = alarms[0].ID;
    
            //Delete the Alarm using its ID and get the result
            Boolean result = KACWrapper.KAC.DeleteAlarm(aID);
    
            //Did it work?
            Debug.Log(String.Format("Deleted:{0} Result Success:{1}",aName,result));
        }
    }
    
  4. Use Alarm Events - This ones a little more interesting as we need to register for an event
        
        internal override void Start()
        {
            Debug.Log("Start the Monobehaviour");
            KACWrapper.InitKACWrapper();
    
            //Register the event handler
            KACWrapper.KAC.onAlarmStateChanged += KAC_onAlarmStateChanged;
        }
    
        void KAC_onAlarmStateChanged(KACWrapper.KACAPI.AlarmStateChangedEventArgs e)
        {
            //Write out the alarm and event name
            Debug.Log(String.Format("{0}->{1}", e.alarm.Name, e.eventType));
        }
    

Example Plugin

You can see an example of this code in use in the KACAPITester Project, which can be found here:
https://github.com/TriggerAu/KerbalAlarmClock/tree/feature/v3refresh/KerbalAlarmClock_APITester/