| | 1 | = HowTo: Timer = |
| | 2 | [[TracNav(TracNav/TOC_Development)]] |
| | 3 | |
| | 4 | It's possible to delay a function call by using a [wiki:Timer]. In fact it's even possible to delay a whole call including an object and several arguments. This is achievec by using an [wiki:Executor]. |
| | 5 | |
| | 6 | == Static functions == |
| | 7 | For static functions, use StaticTimer. A StaticTimer is created either in the constructor or by calling setTimer: |
| | 8 | * '''StaticTimer('''''interval, bLoop, executor''''')''' |
| | 9 | * '''setTimer('''''interval, bLoop, executor''''')''' |
| | 10 | ''interval'' is the time in seconds until the function gets executed.[[br]] |
| | 11 | ''bLoop'' is a bool. If set to true, the Timer will continue calling the function after every ''interval'' seconds.[[br]] |
| | 12 | ''executor'' is an ExecutorStatic of a static functio |
| | 13 | |
| | 14 | {{{ |
| | 15 | #include "tools/Timer.h" |
| | 16 | |
| | 17 | void myFunction(); |
| | 18 | void myAdvancedFunction(int value); |
| | 19 | |
| | 20 | // Call myFunctionafter 5 seconds |
| | 21 | StaticTimer myTimer(5.0f, false, createExecutor(createFunctor(&myFunction))); |
| | 22 | |
| | 23 | // Call myAdvancedFunction(10) after 3 seconds |
| | 24 | ExectorStatic* executor = createExecutor(createFunctor(&myAdvancedFunction)); |
| | 25 | executor->setDefaultValue(0, 10.0f); // Bind the value |
| | 26 | StaticTimer myAdvancedTimer(3.0f, false, executor); |
| | 27 | }}} |
| | 28 | |
| | 29 | See [wiki:Executor#DefaultValues] for more information on how to find values. |
| | 30 | |
| | 31 | == Member functions == |
| | 32 | For member functions you additionally have to add the object: |
| | 33 | * '''StaticTimer('''''interval, bLoop, object, executor''''')''' |
| | 34 | * '''setTimer('''''interval, bLoop, object, executor''''')''' |
| | 35 | ''interval'', ''bLoop'' and ''executor'' are like in StaticTimer (except executor has to be an ExecutorMember).[[br]] |
| | 36 | ''object'' the object on which to call the function. |
| | 37 | |
| | 38 | {{{ |
| | 39 | #include "tools/Timer.h" |
| | 40 | |
| | 41 | class MyClass |
| | 42 | { |
| | 43 | void myFunction(); |
| | 44 | }; |
| | 45 | |
| | 46 | // Create an instance of MyClass: |
| | 47 | MyClass* object = new MyClass(); |
| | 48 | |
| | 49 | // Call MyClass::myFunctionafter after 5 seconds |
| | 50 | Timer myTimer(5.0f, false, object, createExecutor(createFunctor(&MyClass::myFunction))); |
| | 51 | }}} |