How can I get a reference to the window object in C++, similar to get_window().mouse_passthrough = true in GDScript?

Godot Version

4.4.1 windows

Question

Hello everyone,
I’m trying to get a reference to the window object using C++, but haven’t had much success so far.
For example, I’m attempting something like this:

#include "game_init_window.h"
#include <godot_cpp/classes/display_server.hpp>
#include <godot_cpp/classes/viewport.hpp>
#include <godot_cpp/classes/canvas_item.hpp>  // REQUIRED for update()
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/classes/window.hpp>
#include <godot_cpp/classes/engine.hpp>

void GameInitWindow::_bind_methods() {

}

void GameInitWindow::_ready() {
    DisplayServer* ds = DisplayServer::get_singleton();
        
    MainLoop* ml = Engine::get_singleton()->get_main_loop();
    SceneTree* tree = Object::cast_to<SceneTree>(ml);
    if (!tree) return;
   

    
    //ds->window_set_flag(DisplayServer::WINDOW_FLAG_TRANSPARENT, true);
    

    int screen_index = ds->get_primary_screen();  // Or use screen_get_current()
    Vector2 screen_size = ds->screen_get_size(screen_index);

    int width = static_cast<int>(screen_size.x);  // Full screen width
    int height = 300;

   // ds->window_set_size(Vector2(width, height));
   // ds->window_set_position(Vector2(0, screen_size.y - height));
    //ds->window_set_flag(DisplayServer::WINDOW_FLAG_BORDERLESS, true);
    //ds->window_set_flag(DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP, true);
     
    Vector2 usable_size = ds->screen_get_usable_rect().size;
    //ds->window_set_position(Vector2(0, usable_size.y - height));

    //get_viewport()->set_transparent_background(true);
    
    //CanvasItem::update();  // Explicit to avoid ambiguity
   
    


}
scons: `C:\dev\my\godot\cpp_new\godot-cpp\godot-cpp\bin\libgodot-cpp.windows.template_debug.dev.x86_64.lib' is up to date.
Compiling src\game_init_window.cpp ...
src\game_init_window.cpp(16): error C2672: 'godot::Object::cast_to': no matching overloaded function found
C:\dev\my\godot\cpp_new\godot-cpp\godot-cpp\gen\include\godot_cpp/classes/object.hpp(160): note: could be 'const T *godot::Object::cast_to(const godot::Object *)'
src\game_init_window.cpp(16): note: 'initializing': cannot convert from 'godot::MainLoop *' to 'const godot::Object *'
src\game_init_window.cpp(16): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or parenthesized function-style cast
C:\dev\my\godot\cpp_new\godot-cpp\godot-cpp\gen\include\godot_cpp/classes/object.hpp(158): note: or       'T *godot::Object::cast_to(godot::Object *)'
src\game_init_window.cpp(16): note: 'initializing': cannot convert from 'godot::MainLoop *' to 'godot::Object *'
src\game_init_window.cpp(16): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or parenthesized function-style cast
src\game_init_window.cpp(20): error C2027: use of undefined type 'godot::SceneTree'
C:\dev\my\godot\cpp_new\godot-cpp\godot-cpp\gen\include\godot_cpp/classes/node.hpp(55): note: see declaration of 'godot::SceneTree'
src\game_init_window.cpp(28): error C2039: 'get_native_handle': is not a member of 'godot::Window'
C:\dev\my\godot\cpp_new\godot-cpp\godot-cpp\gen\include\godot_cpp/classes/window.hpp(58): note: see declaration of 'godot::Window'
scons: *** [obj\game_init_window.o] Error 2
scons: building terminated because of errors.

gives me compilation errors , chatgpt can’t crack it ..

It would have been helpful to show us the errors, and a complete minimal example, as there is not enough here to give accurate response.

I’m going to assume that it says something about an incomplete type?
if thats the case you are missing headers.

Make sure you are including both the engine and the scenetree

#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/scene_tree.hpp>

Sometimes classes will be pre-declared in headers so that pointers to those objects can be created without causing a dependency loop of include files including each other, or to avoid diamond patterns.

sure updated the question

so yeah, you havent included scene_tree.hpp

src\game_init_window.cpp(20): error C2027: use of undefined type 'godot::SceneTree'

the MainLoop class is pre-declared in the engine header, but that doesnt include any information other than there exists some class called MainLoop, and doesnt tell you anything about it.

By incuding scene_tree.hpp you then get MainLoop and how SceneTree relates to MainLoop as a derived class, and the dynamic cast inside object::cast_to will then know how to make the conversion.

1 Like

Thanks working now