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