Jump to content

Alarm Clock App - API Discussion


flart

Recommended Posts

Until, hopefully, examples appear in the Modders Notes 1.12, it is the place where you can share your shenanigan or ask questions.

For a starter, there is a api/class_alarm_clock_scenario.html, where you can start your research.

 

2 hours ago, R-T-B said:

Please ping me when someone opens a thread for Alarm Clock App, I'd be glad to comment on my findings as to how to work the native alarm.

there it is, @R-T-B

@TriggerAu you also probably may want to have a link to that thread.

 

Could someone publish "Example Usage" as https://triggerau.github.io/KerbalAlarmClock/api.html, but for the Alarm Clock App?

  • List the current Alarms
  • Create a new Alarm
  • Delete an Alarm
  • Use Alarm Events
Edited by flart
Link to comment
Share on other sites

An example snippet I made that works to set a manual alarm that kills warp and pops a dialog in game:

AlarmTypeRaw alarmToSet = new AlarmTypeRaw
{
    title = alarmTitle,
    description = alarmName,
    actions =
    {
        warp = AlarmActions.WarpEnum.KillWarp,
        message = AlarmActions.MessageEnum.Yes
    },
    ut = alarmTime
};
AlarmClockScenario.AddAlarm(alarmToSet);

 

3 hours ago, flart said:

List the current Alarms

is trickier.  RIght now I iterate through them until I reach the end of array which since I don't know the length generates an exception, so I generally wrap this in a try-catch. and build an internal array to compare them.  Then we can decide if something already exists, remove unneccesary alarms with AlarmClockScenario.DeleteAlarm(Alarm), etc.

Example code from KCT test release:

KCTDebug.Log("KAC Alarm being created!");
AlarmTypeBase alarmCheck = AlarmClockScenario.GetNextAlarm(UT);
AlarmTypeBase thisAlarm = alarmCheck;
try
{
    while (true)
    {
        if (thisAlarm.description.Contains("KCT:"))
        {
            AlarmClockScenario.DeleteAlarm(thisAlarm);
            throw new Exception();
        }
        else
        {
            alarmCheck = thisAlarm;
        }
        thisAlarm = AlarmClockScenario.GetNextAlarm(alarmCheck.ut);
    }
}
catch
{
//alarm creation logic.
}

ugly but it works.

1 hour ago, TriggerAu said:

Its coming in the modders notes yes, but do feel free to ask questions as we go

Was wondering where those were.  No rush, I like reverse engineering things anyways, lol.  But not everyone may share that sentiment. ;)

Edited by R-T-B
Link to comment
Share on other sites

9 hours ago, R-T-B said:

is trickier.

it supports an iteration, but there is GetListEnumerator() (not GetEnumerator() so it is not IEnumerable, so no foreach)

var alarms = AlarmClockScenario.Instance.alarms;
var enumerator = alarms.GetListEnumerator();

while (enumerator.MoveNext())
{
    Log(enumerator.Current.title);
}

 

Edited by flart
Link to comment
Share on other sites

Ill put my hand up and say - I totally missed pitting the GetAlarms method in there.

AlarmClockScenario.Instance.alarms is a DictionaryValueList - can be used as a dictionary and a List

The quickest way to get at the list is: AlarmClockScenario.Instance.alarms.ValueList - which returns the List<AlarmTypeBase> that underpins the dictionary, etc

 

The Modders notes are up now too: 

 

Link to comment
Share on other sites

48 minutes ago, TriggerAu said:

Ill put my hand up and say - I totally missed pitting the GetAlarms method in there.

AlarmClockScenario.Instance.alarms is a DictionaryValueList - can be used as a dictionary and a List

The quickest way to get at the list is: AlarmClockScenario.Instance.alarms.ValueList - which returns the List<AlarmTypeBase> that underpins the dictionary, etc

 

The Modders notes are up now too: 

 

That is helpful thank you.  Now I can dispose of my weird try-catch logic that depends on a generated exception, lol.

Link to comment
Share on other sites

3 minutes ago, R-T-B said:

That is helpful thank you.  Now I can dispose of my weird try-catch logic that depends on a generated exception, lol.

Twas a very cool mechanism I have to say :) 

Theres a number of lists in KSP that use that DictionaryValueList class - its very useful

 

8 minutes ago, flart said:

Is this public?

OezCCkM.png

Hmm, I thought it was, but now Im a wonderin... to the code

EDIT: Darn sorry its internal. the way we do it in other place is a loop. This will work in the meantime

for (int i = 0; i < AlarmClockScenario.Instance.alarms.Count; i++)
{
    AlarmTypeBase a = AlarmClockScenario.Instance.alarms.At(i);
}

 

Link to comment
Share on other sites

In the VAB, if I open an alarm GUI in the game, and run AlarmClockScenario.AddAlarm(), added alarm is showed with the 0:00:00
ZMDkFHW.png

and only after scene change it show correct value:

6H9Zlax.png

In the case alarm is added without opened alarm window in the VAB, the alarm is added correctly.

 

@TriggerAu Does it have some AlarmClockScenario.update() ?

 

Edited by flart
Link to comment
Share on other sites

15 minutes ago, flart said:

AlarmClockScenario.AddAlarm(alarm);
alarm.OnScenarioUpdate();
alarm.UIInputPanelUpdate();

adding both of the "update" methods doesn't fix the 0:00:00 problem

thats a weird one, ill dig into it

Link to comment
Share on other sites

  • 4 weeks later...
On 6/28/2021 at 2:47 PM, flart said:

In the VAB, if I open an alarm GUI in the game, and run AlarmClockScenario.AddAlarm(), added alarm is showed with the 0:00:00
ZMDkFHW.png

and only after scene change it show correct value:

6H9Zlax.png

In the case alarm is added without opened alarm window in the VAB, the alarm is added correctly.

 

On 6/29/2021 at 1:38 AM, TriggerAu said:

thats a weird one, ill dig into it

Any update?
There is my code:
https://github.com/yalov/KVASS/blob/c3dc556c5618955b1150b12ec182ea0254df9002/KVASS/Alarm.cs#L109

Looks like @linuxgurugamer have the same problem in the KCT.

Spoiler

xv3fIQm.jpeg

 

Edited by flart
Link to comment
Share on other sites

  • 2 weeks later...

@TriggerAu What is AlarmTypeRaw.timeEntry ?
Looks like AlarmTypeRaw.timeEntry is reverted to 300 after scene change.

For example, I want to postpone an alarm for 1 hour, and something like that didn't work because of timeEntry  was reset to 300 (so it became 300+3600)

double shift = 3600;
alarms[i].ut += shift;
alarms[i].timeEntry += shift;  // = alarms[i].ut - Utils.CurrentUT()
alarms[i].OnScenarioUpdate();
alarms[i].UIInputPanelUpdate();

Replacing += shift with the commented  assignment works ok.

Edited by flart
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...