# TimerEvent observer not working for VR (OpenXR)

**URL:** https://discourse.vtk.org/t/timerevent-observer-not-working-for-vr-openxr/14611
**Category:** VR/AR
**Created:** [September 24, 2024, 3:21pm UTC](https://discourse.vtk.org/t/timerevent-observer-not-working-for-vr-openxr/14611 "2024-09-24T15:21:49Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![petr.raunigr](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@petr.raunigr](https://discourse.vtk.org/u/petr.raunigr)
#### Post date: [September 24, 2024, 3:21pm UTC](https://discourse.vtk.org/t/timerevent-observer-not-working-for-vr-openxr/14611/1 "2024-09-24T15:21:49Z")

</div>

Hello, I came across a problem when trying to execute a TImerEvent callback in my VR (OpenXR) c++ app.

**The problem is: The TimerEvent callback is never called when working with VR.**

When in a non-VR enviroment an observer works fine and a callback is fired. To simplify things I created a test app based on the [Timer example](https://examples.vtk.org/site/Cxx/Utilities/Timer/).

Below you can see a simple code that should print out a message each XX miliseconds but it doesn’t matter what I try it just don’t.

```auto
#include <vtkSmartPointer.h>
#include <vtkCommand.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkOpenXRRenderer.h>
#include <vtkOpenXRRenderWindow.h>
#include <vtkOpenXRRenderWindowInteractor.h>
#include <vtkPolyDataMapper.h>

class TimerCallback : public vtkCommand
{
public:
    static TimerCallback* New()
    {
        cout << "TimerCallback constructor hit" << endl;
        return new TimerCallback;
    }

    void Execute(vtkObject* caller, unsigned long eventId, void* callData) override
    {
        cout << "Pre-IF step" << endl;
        if (vtkCommand::TimerEvent == eventId)
        {
            cout << "Timer event triggered!" << endl;
        }
    }
};

int main() {
    vtkSmartPointer<vtkOpenXRRenderer> renderer = vtkSmartPointer<vtkOpenXRRenderer>::New();

    vtkSmartPointer<vtkOpenXRRenderWindow> renderWindow = vtkSmartPointer<vtkOpenXRRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    vtkSmartPointer<vtkOpenXRRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkOpenXRRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderWindow->Render();
    renderWindowInteractor->Initialize();

    vtkSmartPointer<TimerCallback> timerCallback = vtkSmartPointer<TimerCallback>::New();

    renderWindowInteractor->AddObserver(vtkCommand::TimerEvent, timerCallback);
    renderWindowInteractor->CreateRepeatingTimer(100);
    
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

```

I understand that _RenderWindow_ must call _Render()_ method before setting up observers as well as _RenderWindowInteractor_ must be _initialized_ too.

I tried setting up different miliseconds values, I tried moving _Render()_ and _Initialize()_ methods to a different place in my code and non worked.

I also tried a VR controller _vtkCommand::Move3DEvent and vtkCommand::Select3DEvent_ observers and they both worked just fine.

**Did I miss something?** Is there a way for a VR app to be able to work with a _TimerEvent_ observer?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [September 25, 2024, 12:31pm UTC](https://discourse.vtk.org/t/timerevent-observer-not-working-for-vr-openxr/14611/2 "2024-09-25T12:31:59Z")

</div>

Since you are working in C++, you can very easily determine what’s happening using the debugger. You can add a breakpoint into your callback’s Execute method and see what is calling it when you are not using VR. Probably the feature is just missing from the corresponding VR class and you can implement it by copy pasting it there (or if it is something longer and so you don’t want to duplicate code then you can move the code to some base class and call it from both the non-VR and VR class).
