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
To access the API from your plugin is a couple of simple (I hope) steps:
// TODO: Change this namespace to something specific to your plugin here. //EG: // namespace MyPlugin_KACWrapper namespace KACWrapper { ...
// TODO: Change this namespace to something specific to your plugin here. //EG: // namespace MyPlugin_KACWrapper namespace KACAPITester_KACWrapper { ...
internal override void Start() { KACWrapper.InitKACWrapper(); if (KACWrapper.APIReady) { //All good to go Debug.Log(KACWrapper.KAC.Alarms.Count); } }
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
APIReady | Boolean | Whether the object has been wrapped and the APIReady flag is set in the real KAC | |
AssemblyExists | Boolean | Whether we found the KerbalAlarmClock assembly in the loadedassemblies | |
InstanceExists | Boolean | Whether we managed to hook the running Instance from the assembly | |
InitKACWrapper | This method will set up the KAC object and wrap all the methods/functions | ||
KAC | KACAPI | This is the Kerbal Alarm Clock object that you can access via the API once wrapped |
APIReady | Boolean | Whether the APIReady flag is set in the real KAC | |
Alarms | KACAlarmList (List<KACAlarm>) | The list of Alarms that are currently active in game | |
CreateAlarm | String | Create 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 | |
DeleteAlarm | Boolean | Delete an Alarm Takes a String of an Alarms ID and returns A Success Boolean | |
onAlarmStateChanged | Event that fires when the State of an Alarm changes |
AlarmAction | AlarmActionEnum | What should the Alarm Clock do when the alarm fires | |
AlarmMargin | Double | In game seconds the alarm will fire before the event it is for | |
AlarmTime | Double | In game UT value of the alarm | |
AlarmType | AlarmTypeEnum (ReadOnly) | What type of Alarm is this - affects icon displayed and some calc options | |
ID | String (Readonly) | Unique Identifier of this alarm | |
Name | String | Short Text Name for the Alarm | |
Notes | String | Longer Text Description for the Alarm | |
Remaining | Double (Readonly) | How much Game time is left before the alarm fires | |
RepeatAlarm | Boolean | Should the alarm be repeated once it fires | |
RepeatAlarmPeriod | Double | how long after the alarm fires should the next alarm be set up | |
VesselID | String | Unique Identifier of the Vessel that the alarm is attached to | |
XferOrginBodyname | String | name of the body the vessel is departing from | |
XferTargetBodyname | String | name of the body the vessel is arriving at |
Below you can see simple examples of ways you can use some of the properties and Methods above
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 )); } }
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; ... } }
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)); } }
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)); }
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/