[Scripters] How to use StopWatch/Timer

free(?!) clues!

[Scripters] How to use StopWatch/Timer

Postby Kannkor » 18 May 2024, 02:16

Patched out with Ogre 17.134

This is for scripters. If you don't script at all, this won't be of any help to you.

What is StopWatch?
Simply put, there are times when we need to wait xxx time, this is an easy way to keep track of it, or even have an event triggered when your stop watch hits a specific value, or every time it hits that value (a lap, or repeating timer, if you will).

Include the file! (This will likely be included in things like IC files by default with the include that is already there).
Code: Select all
#include "${LavishScript.HomeDirectory}/Scripts/OgreCommon/Object_StopWatch.iss"


Create yourself a stop watch variable. I recommend you use a prefix of "SW_" (stands for stop watch).
Code: Select all
variable Object_StopWatch SW_Test



Now for a few examples.

Example 1: A straight forward as you can get. You hit start, and when you want to see how much time has passed, you check it.
The first parameter of Start is always a name you want to give it.
Code: Select all
#include "${LavishScript.HomeDirectory}/Scripts/OgreCommon/Object_StopWatch.iss"
function main()
{
   variable Object_StopWatch SW_Test
   SW_Test:Start["SW_MyTest"]
   wait 22
   echo MS: ${SW_Test.DurationMS} * Seconds: ${SW_Test.DurationSeconds} * Minutes: ${SW_Test.DurationMinutes}
}


Example 2: This is the bread and butter of this. Setting up automatic events. Lets take a little scenario. A monster says "Red circle at my feet incoming!". This means you have to joust away. Lets assume we know, in 5 seconds, it's safe to go back in.
One thing to note, make sure your stopwatch never goes out of scope or it's deleted. I feel like that goes without saying... :)
I personally prefer objects, but you could make it a script level variable also. Here's a short example of NOT using an object.

Code: Select all
#include "${LavishScript.HomeDirectory}/Scripts/OgreCommon/Object_StopWatch.iss"
function main()
{
   variable Object_StopWatch SW_Test

   ; We are pretending to joust here to location 100,100,100
   Ogre_CampSpot:Set_Relative["all",100,100,100]

   ; Since we want it to trigger an event, we need to register the event. Calling it "SW_Test_Event". Then we attach this script to that event.
   LavishScript:RegisterEvent[SW_Test_Event]
   Event[SW_Test_Event]:AttachAtom[SW_Test_Event]

   
   ; The parameters here should be pretty clear.
   ;    TriggerEventAt is in MS. 5500 = 5.5 seconds.
   ;    TriggerEventName - This is the event we just created. It will trigger this event after 5.5 seconds
   ;    TriggerEventForWho - When the event is triggered, who will it trigger for? (Keep in mind, if you are doing it more than just for ourself, which is default, you must have it registered on everyone prior)
   ;    TriggerEventPulseTime - How often should it check. This is done via timedcommand. 1 is the lowest possible value (and default) which is 100ms. 2 is 200ms, etc.
   ; The only two required event parameters here are EventAt and EventName. The others you can leave at default if you wish.

   SW_Test:Start["SW_MyTest","-TriggerEventAt",5500,"-TriggerEventName","SW_Test_Event","-TriggerEventForWho","${Me.Name}","-TriggerEventPulseTime",2]
   ; The wait is just a random amount of time for the event to trigger. You could be doing other things with your script here.
   wait 120
   ; There is currently a bug in Innerspace, if you do not have ANY additional code here, it breaks the events. So have more code, or simply do a noop (literally does nothing)
   noop
}
atom SW_Test_Event(string _Name, jsonvalue _JSONValue)
{
   echo ${Time}: SW_Test_event: Name: ${_Name} ${_JSONValue.AsJSON}
       ; Our 5.5 seconds is over, lets clear out our joust
   Ogre_CampSpot:ClearRelative["all"]
}


Example 3: Repeating
I don't really have a real world example for this, but it came up so I added it.
Lets say we want to do something every 2.5 seconds.

Code: Select all
#include "${LavishScript.HomeDirectory}/Scripts/OgreCommon/Object_StopWatch.iss"
function main()
{
   variable Object_StopWatch SW_Test

   ; Since we want it to trigger an event, we need to register the event. Calling it "SW_Test_Event". Then we attach this script to that event.
   LavishScript:RegisterEvent[SW_Test_Event]
   Event[SW_Test_Event]:AttachAtom[SW_Test_Event]

   
   ; The parameters here should be pretty clear.
   ;    TriggerEventAt is in MS. 2500 = 2.5 seconds.
   ;    TriggerEventName - This is the event we just created. It will trigger this event after 5.5 seconds
   ;    TriggerEventForWho - When the event is triggered, who will it trigger for? (Keep in mind, if you are doing it more than just for ourself, which is default, you must have it registered on everyone prior)
   ;    TriggerEventPulseTime - How often should it check. This is done via timedcommand. 1 is the lowest possible value (and default) which is 100ms. 2 is 200ms, etc.
   ;    TriggerEventToComplete - How many times do you want it reporting?
   ; The only two required event parameters here are EventAt and EventName. The others you can leave at default if you wish.

   SW_Test:Start["SW_MyTest","-TriggerEventAt",2500,"-TriggerEventName","SW_Test_Event","-TriggerEventForWho","${Me.Name}","-TriggerEventPulseTime",2,"-TriggerEventToComplete",3]
   ; The wait is just a random amount of time for the event to trigger. You could be doing other things with your script here.
   wait 120
   ; There is currently a bug in Innerspace, if you do not have ANY additional code here, it breaks the events. So have more code, or simply do a noop (literally does nothing)
   noop
}

atom SW_Test_Event(string _Name, jsonvalue _JSONValue)
{
   echo ${Time}: SW_Test_event: Name: ${_Name} ${_JSONValue.AsJSON}
}}

outputs
Code: Select all
13:39:33: SW_Test_event: Name: SW_MyTest {"Name":"SW_MyTest","StartTime":78,"DurationMS":2609,"TriggerEventCompleted":1,"TriggerEventToComplete":3}
13:39:35: SW_Test_event: Name: SW_MyTest {"Name":"SW_MyTest","StartTime":78,"DurationMS":5094,"TriggerEventCompleted":2,"TriggerEventToComplete":3}
13:39:38: SW_Test_event: Name: SW_MyTest {"Name":"SW_MyTest","StartTime":78,"DurationMS":7656,"TriggerEventCompleted":3,"TriggerEventToComplete":3}


Example 4 - multiple things all at once.
You can have each stop watch use a different event if you want, or you can use 1 event. Here I show both.
SW_Test 1-3 use 1 event, and 4 uses a different event. I put echos for each one, but obviously you can code each one to do anything you want.
Code: Select all
#include "${LavishScript.HomeDirectory}/Scripts/OgreCommon/Object_StopWatch.iss"
function main()
{
   variable Object_StopWatch SW_Test
   variable Object_StopWatch SW_Test2
   variable Object_StopWatch SW_Test3
   variable Object_StopWatch SW_Test4

   ; Since we want it to trigger an event, we need to register the event. Calling it "SW_Test_Event". Then we attach this script to that event.
   LavishScript:RegisterEvent[SW_Test_Event]
   Event[SW_Test_Event]:AttachAtom[SW_Test_Event]

   LavishScript:RegisterEvent[SW_Test_Event4]
   Event[SW_Test_Event4]:AttachAtom[SW_Test_Event4]
   ; The parameters here should be pretty clear.
   ;    TriggerEventAt is in MS. 2500 = 2.5 seconds.
   ;    TriggerEventName - This is the event we just created. It will trigger this event after 5.5 seconds
   ;    TriggerEventForWho - When the event is triggered, who will it trigger for? (Keep in mind, if you are doing it more than just for ourself, which is default, you must have it registered on everyone prior)
   ;    TriggerEventPulseTime - How often should it check. This is done via timedcommand. 1 is the lowest possible value (and default) which is 100ms. 2 is 200ms, etc.
   ;    TriggerEventToComplete - How many times do you want it reporting?
   ; The only two required event parameters here are EventAt and EventName. The others you can leave at default if you wish.

   SW_Test:Start["SW_MyTest","-TriggerEventAt",2500,"-TriggerEventName","SW_Test_Event","-TriggerEventForWho","${Me.Name}","-TriggerEventPulseTime",2,"-TriggerEventToComplete",3]
   SW_Test2:Start["SW_MyTest2","-TriggerEventAt",3000,"-TriggerEventName","SW_Test_Event","-TriggerEventForWho","${Me.Name}","-TriggerEventPulseTime",2]
   SW_Test3:Start["SW_MyTest3","-TriggerEventAt",6000,"-TriggerEventName","SW_Test_Event","-TriggerEventForWho","${Me.Name}","-TriggerEventPulseTime",2]
   SW_Test4:Start["SW_MyTest4","-TriggerEventAt",7000,"-TriggerEventName","SW_Test_Event4","-TriggerEventForWho","${Me.Name}","-TriggerEventPulseTime",2]
   ; The wait is just a random amount of time for the event to trigger. You could be doing other things with your script here.
   wait 120
   ; There is currently a bug in Innerspace, if you do not have ANY additional code here, it breaks the events. So have more code, or simply do a noop (literally does nothing)
   noop
}

atom SW_Test_Event(string _Name, jsonvalue _JSONValue)
{
   echo ${Time}: SW_Test_event: Name: ${_Name} ${_JSONValue.AsJSON}
   ; can use if statements here if you prefer.. IE: if ${_Name.Equal["SW_MyTest2"]}
   switch ${_Name}
   {
      case SW_MyTest
         echo SW_MyTest - a repeating stop watch
      break
      case SW_MyTest2
         echo SW_MyTest2 - maybe a joust out?
      break
      case SW_MyTest3
         echo SW_MyTest3 - maybe a clear joust?
      break
   }
}
atom SW_Test_Event4(string _Name, jsonvalue _JSONValue)
{
   echo ${Time}: SW_Test_event4: Name: ${_Name} ${_JSONValue.AsJSON}
}

Output
Code: Select all
14:26:27: SW_Test_event: Name: SW_MyTest {"Name":"SW_MyTest","StartTime":125,"DurationMS":2531,"TriggerEventCompleted":1,"TriggerEventToComplete":3}
SW_MyTest - a repeating stop watch

14:26:28: SW_Test_event: Name: SW_MyTest2 {"Name":"SW_MyTest2","StartTime":125,"DurationMS":3172,"TriggerEventCompleted":1,"TriggerEventToComplete":1}
SW_MyTest2 - maybe a joust out?

14:26:30: SW_Test_event: Name: SW_MyTest {"Name":"SW_MyTest","StartTime":125,"DurationMS":5156,"TriggerEventCompleted":2,"TriggerEventToComplete":3}
SW_MyTest - a repeating stop watch

14:26:31: SW_Test_event: Name: SW_MyTest3 {"Name":"SW_MyTest3","StartTime":125,"DurationMS":6016,"TriggerEventCompleted":1,"TriggerEventToComplete":1}
SW_MyTest3 - maybe a clear joust?

14:26:32: SW_Test_event4: Name: SW_MyTest4 {"Name":"SW_MyTest4","StartTime":125,"DurationMS":7078,"TriggerEventCompleted":1,"TriggerEventToComplete":1}

14:26:32: SW_Test_event: Name: SW_MyTest {"Name":"SW_MyTest","StartTime":125,"DurationMS":7500,"TriggerEventCompleted":3,"TriggerEventToComplete":3}
SW_MyTest - a repeating stop watch
Kannkor
 
Posts: 361
Joined: 06 Nov 2013, 11:49

Re: [Scripters] How to use StopWatch/Timer

Postby Kannkor » 19 May 2024, 13:49

Timer - this is kind of the reverse of a stop watch. This existed first, and could likely be replaced by using stopwatch, but many people use these regularly, so I updated it also.
All of the event functionality of the stop watch has been added to the timer also.

Code: Select all
#include "${LavishScript.HomeDirectory}/Scripts/OgreCommon/Object_Timer.iss"
function main()
{
   variable Object_Timer Timer_Test
   variable Object_Timer Timer_Test2
   variable Object_Timer Timer_IncTest
   variable Object_Timer Timer_Boring
   variable int iCounter

   LavishScript:RegisterEvent[Timer_Test_Event]
   Event[Timer_Test_Event]:AttachAtom[Timer_Test_Event]

   ; This "boring" timer, is just a timer object that we have used in the past, with no events. You need to check .TimeLeft to use it
   Timer_Boring:SetSeconds[1]
   Timer_Test:Set[2250,"MyTestTimer","-TriggerEventName","Timer_Test_Event"]
   Timer_Test2:SetSeconds[3,"MyTestTimer2","-TriggerEventName","Timer_Test_Event"]
   Timer_IncTest:SetSeconds[5,"MyTestIncTimer","-TriggerEventName","Timer_Test_Event"]

   while ${Timer_Boring.TimeLeft}
   {
      ; echo waiting for ${Timer_Boring.TimeLeft}
      wait 2
   }
   echo ${Timer_Boring.TimeLeft} completed. Lets wait for the events to finish
   echo Timer_IncTest.TimeLeft: ${Timer_IncTest.TimeLeft}
   ; IncSeconds DOES support adding the event name, but since we added it above, we do not need to re-add it again.
   Timer_IncTest:IncSeconds[2]
   echo Timer_IncTest.TimeLeft: ${Timer_IncTest.TimeLeft}

   while ${Timer_IncTest.TimeLeft}
      wait 2
   wait 20
   noop

}
atom Timer_Test_Event(string _Name, jsonvalue _JSONValue)
{
   echo ${Time}: Timer_Test_Event: Name: ${_Name} ${_JSONValue.AsJSON}
}


Output
Code: Select all
0 completed. Lets wait for the events to finish
Timer_IncTest.TimeLeft: 3937
Timer_IncTest.TimeLeft: 5937
14:44:12: Timer_Test_Event: Name: MyTestTimer {"Name":"MyTestTimer","EndTime":2328}
14:44:13: Timer_Test_Event: Name: MyTestTimer2 {"Name":"MyTestTimer2","EndTime":3078}
14:44:17: Timer_Test_Event: Name: MyTestIncTimer {"Name":"MyTestIncTimer","EndTime":7078}
Kannkor
 
Posts: 361
Joined: 06 Nov 2013, 11:49


Return to Guides & Strats

Who is online

Users browsing this forum: No registered users and 1 guest

cron