Hi there! You are currently browsing as a guest. Why not create an account? Then you get less ads, can thank creators, post feedback, keep a list of your favourites, and more!
Test Subject
Original Poster
#1 Old 11th Mar 2015 at 7:17 PM
Default How to employ alarm handlers?
Hi all,
I have been trying to figure out how to employ alarms and alarm handlers etc for sometime now.
Frankly, there's something basic I am missing.
I have asked before about them, but the usual response is 'Here, download this package... open the s3sa in ILSPY. You can see what I did there.'
Which really is great, I have learned from that... but not enough to make use of the things.
What I am hoping for is for someone to post the script in which they used an alarm and then explain how it is employed.
Or pointing me to the best possible tutorial for handling them would work too.
It's driving me crazy because I know it isn't as hard as I have, so far, been making it on myself.
Anybody feel like an educator today? Then teach me this witchcraft! I will be eternally grateful.
Thanks
Advertisement
Inventor
#2 Old 11th Mar 2015 at 10:16 PM
Hey, I remember you. Do you remember me? I'm the one who told you "You can see what I did there".

For a one time thing
Code:
private static void ExampleAlarmHandler()
{
    // DO STUFF
}

AlarmManager.Global.AddAlarm(0.1f, TimeUnit.Seconds, new AlarmTimerCallback(ExampleAlarmHandler), "Alarm Name", AlarmType.NeverPersisted, null);


The following is from the script I told you to study the last time.

Code:
public class FloorMarkSummoner
{
// AlarmHandle declaration
private AlarmHandle fmsAlarm = AlarmHandle.kInvalidHandle;

// method to delete/reset the alarm
public void DeleteAlarms()
{
    if (this.fmsAlarm != AlarmHandle.kInvalidHandle)
    {
        base.RemoveAlarm(this.fmsAlarm);
        this.fmsAlarm = AlarmHandle.kInvalidHandle;
    }
}

// method to initialize/restart the alarm
public void RestartAlarms()
{
    this.DeleteAlarms();
    float hour = SimClock.CurrentTime().Hour;
    float startTime = this.info.StartTime;
    float time = (startTime - hour);
    while (time < 0f)
    {
        time += 24f;
    }    
    this.fmsAlarm = base.AddAlarm(time, TimeUnit.Hours, new AlarmTimerCallback(this.SummonEmployy), "FloorMarkSummoner alarm", AlarmType.AlwaysPersisted);
}

// in this case, if you delete the object, there's no point in keeping the alarm
public override void Dispose()
{
    this.DeleteAlarms();
    base.Destroy();
}

// Alarm Handler
private void SummonEmployy() 
{
    this.RestartAlarms();
    // DO STUFF
}
}


You can see the details (what each parameter of AddAlarm means) from VS or ILSpy.
Test Subject
Original Poster
#3 Old 13th Mar 2015 at 6:59 PM
Thank you, Arsil! I do you remember you... you're advice is pretty much always helpful.
When I mentioned the "You can see what I did there" etc bit, I promise I wasn't singling you out.
I've gotten that same advice many times before... it usually works too.
This time I am stumped (or was...) because it's difficult for me to reverse engineer code. I'm just terrible at it.
I was able to gain some insight from your example above though and am just about to go tinker with it.
Greatly appreciated, Arsil! Thank you!

Quote: Originally posted by Arsil
Hey, I remember you. Do you remember me? I'm the one who told you "You can see what I did there".

For a one time thing
Code:
private static void ExampleAlarmHandler()
{
    // DO STUFF
}

AlarmManager.Global.AddAlarm(0.1f, TimeUnit.Seconds, new AlarmTimerCallback(ExampleAlarmHandler), "Alarm Name", AlarmType.NeverPersisted, null);


The following is from the script I told you to study the last time.

Code:
public class FloorMarkSummoner
{
// AlarmHandle declaration
private AlarmHandle fmsAlarm = AlarmHandle.kInvalidHandle;

// method to delete/reset the alarm
public void DeleteAlarms()
{
    if (this.fmsAlarm != AlarmHandle.kInvalidHandle)
    {
        base.RemoveAlarm(this.fmsAlarm);
        this.fmsAlarm = AlarmHandle.kInvalidHandle;
    }
}

// method to initialize/restart the alarm
public void RestartAlarms()
{
    this.DeleteAlarms();
    float hour = SimClock.CurrentTime().Hour;
    float startTime = this.info.StartTime;
    float time = (startTime - hour);
    while (time < 0f)
    {
        time += 24f;
    }    
    this.fmsAlarm = base.AddAlarm(time, TimeUnit.Hours, new AlarmTimerCallback(this.SummonEmployy), "FloorMarkSummoner alarm", AlarmType.AlwaysPersisted);
}

// in this case, if you delete the object, there's no point in keeping the alarm
public override void Dispose()
{
    this.DeleteAlarms();
    base.Destroy();
}

// Alarm Handler
private void SummonEmployy() 
{
    this.RestartAlarms();
    // DO STUFF
}
}


You can see the details (what each parameter of AddAlarm means) from VS or ILSpy.
Inventor
#4 Old 13th Mar 2015 at 7:48 PM
Keep in mind that mine were just examples. In the second one the part were the alarm is started
for the first time is missing (the first time RestartAlarms is called). To make a recurring alarm, you
can also use the first example, simply resetting the alarm timer inside the handler function itself.
I don't remember if it's mandatory to delete it and then create a new one every time. I guess there
are a lot of variants you can use. Anyway, I hope they can help you get the idea of how alarms work.
Test Subject
Original Poster
#5 Old 19th Mar 2015 at 5:38 PM
Arsil, I haven't had the time to really delve into this any more... yet.
I just wanted to thank you again for the help. Thank you!
Hopefully I can find the time soon.

Quote: Originally posted by Arsil
Keep in mind that mine were just examples. In the second one the part were the alarm is started
for the first time is missing (the first time RestartAlarms is called). To make a recurring alarm, you
can also use the first example, simply resetting the alarm timer inside the handler function itself.
I don't remember if it's mandatory to delete it and then create a new one every time. I guess there
are a lot of variants you can use. Anyway, I hope they can help you get the idea of how alarms work.
Inventor
#6 Old 8th May 2015 at 6:27 PM Last edited by Arsil : 8th May 2015 at 7:43 PM.
No need to open a new thread. Any idea why the alarm it's not repeating? It's executed only the first time.
Code:
private static void OnWorldLoadFinished(object sender, EventArgs e)
{
    AlarmManager.Global.AddAlarmRepeating(1f, TimeUnit.Minutes, new AlarmTimerCallback(LeavingHomeAintEasy), kRepeatLength, TimeUnit.Hours, "LeavingHomeAintEasy", AlarmType.NeverPersisted, null);
}

private static void LeavingHomeAintEasy()
{
    // do stuff
}


EDIT: maybe because I'm an idiot? I used TimeUnit.Hours instead of TimeUnit.Minutes for kRepeatLength
and I didn't waited enough for it to be executed again. SOLVED.
Back to top