Tutorial: Timers

Simple Timers Tutorial

 

This tutorial will show how to do a simple timer.  This tutorial adds to MyDemoScene.cs which was created using My First Tutorial.

 

1.  To the start of MyDemoScene.cs add:

 

using Visual3D.Activities;

 

2. To start the “Hello World” message, I use the “H” key.  To add the “H” key and event controller, I add the following to OnInitialized:

 

           Input.InputHandler _controller;

 

            base.OnInitialized();

            Log.Info("My Message From: OnInitialized");

 

            new Input.KeyMap("MyDemoKeyMap", Input.KeyMap.Ids.FullMouseHandlerKeyCodes,

 

 

                new Input.KeyMapEntry(Key.H, "setup-1", false, ModifierKeyDependencies.NoDependencies)

 

                );

 

            Input.InputHandlerConfig config = Input.InputHandlerConfig.CreateKeyboardOnly("MyDemoSceneInput", "MyDemoKeyMap", true, ModifierKeys.None, false);

            _controller = new Input.InputHandler(World.Input, "MyDemoSceneInput", config);

            _controller.InputEventProcessed += _controller_InputEventProcessed;

            _controller.Activate(this);

 

 

3. Add input controller

 

        private void _controller_InputEventProcessed(InputEventArgs inputEventArgs)

        {

 

            NameID actionName = AvatarCommands.MoveForward;

 

            if (inputEventArgs.Key == Key.H)

            {

 

 

               // World.Gui.ShowStatus("Test Message -H Key Pressed");

             

                if (timer_is_on == 0)

                {

                    StartTheRepeat();

                    timer_is_on = 1;

                }

                else

                {

                    World.Gui.ShowStatus("Stopping Timer");

                    StopTheRepeat();

                    timer_is_on = 0;

                }

               

            }

      }

 

4.  Add the following global variables to the start of mydemoregion

 

 

        public int timer_is_on = 0;

        public TimeInterval MyTimeInterval = new TimeInterval(1000);

        public Activities.RepeatedSimpleMethodCommand UpdatedMethod;

 

 

 

5.  Add the following to the end of  mydemoregion

 

        public void StartTheRepeat()

        {

            UpdatedMethod = new Activities.RepeatedSimpleMethodCommand("UpdatedMethod", MyTimeInterval, HelloWorld);

            UpdatedMethod.Initialize();

            UpdatedMethod.Activate();

        }

 

        public void StopTheRepeat()

        {

            //UpdatedMethod = new Activities.RepeatedSimpleMethodCommand("UpdatedMethod", MyTimeInterval, HelloWorld);

            //UpdatedMethod.Initialize();

            UpdatedMethod.Deactivate();

        }

 

        public void HelloWorld()

        {

            World.Gui.ShowStatus("Hello World");

        }

 

6. Build and run Tech Demos/ my demo scene

 

7.  Type the “H” key and see the hello world message.  Since the status message goes away after the 1 second timer, it will always show up. If the timer was changed to 10 seconds, you would see the message go away and come back every 10 seconds.

  

 

 

 

8.  Type the “H” key again and see the message go away.

 
Mike Hoisington, Mike Wetherill