Godot doesn't find nodes despite paths being correct

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By byjove

Hey.
I’m currently writing the code with C++ Godot bindings to manage my main menu GUI and I felt free to declare pointers to some of my GUI scene nodes in the header file ; I then declared them in the Godot-inherited _init function.

void MainMenu::_init(){
    // Define pointers
    m_mainMenuAnimator = get_node<AnimationPlayer>("quickMenu_Animator");
    m_bgmPlayer = get_node<AudioStreamPlayer>("BGMPlayer");
    m_sfxPlayer = get_node<AudioStreamPlayer>("SFXPlayer");

    m_mainMenuBtn_Lay = get_node<HBoxContainer>("menuBtn_Lay");

    m_newGame_Btn = get_node<Button>("menu_Pnl/menuBtn_Lay/newGame_Btn");
    m_loadGame_Btn = get_node<Button>("loadGame_Btn");
    m_options_Btn = get_node<Button>("options_Btn");
    m_credits_Btn = get_node<Button>("credits_Btn");
    m_quitGame_Btn = get_node<Button>("quit_Btn");
}

But strangely, when I playtest the game and try to get any interaction wth the buttons, nothing works. After looking into the debugger, I find these errors:

Godot just doesn’t find any node I tried to define in my _init function. I tried to use another way to write the node paths, but nothing works : it seems like Godot is actually unable to interpret them.

Is there a way to get quickly out of this problem? Thanks in advance.

:bust_in_silhouette: Reply From: sash-rc

If your task is to get children of MainMenu, you should use _ready(), not _init() as it is an object constructor, and called when object just created, but not entered scene tree yet.

Please, read about overridable functions, as well as other basics of scene tree, nodes ans scripting.

Thank you, it parly solved my problem.
But despite of no longer getting the errors in the debugger, it seems like Godot still doesn’t handle my nodes because I tried to disable some of these buttons directly in the _ready function, just to see if it worked, and nothing happened.

It’s even worse in another case when I’m checking with a custom function, called in çprocess, if a button is pressed : Godot just doesn’t give any error in the debugger but crashes. I guess it’s implicating it just can’t guess what node I’m talking about.

I know right Godot is using mostly signals and slots to get UI working, but shouldn’t it be able to manage it this way?

byjove | 2021-09-04 07:40

it parly solved my problem.

If the answer works for you about this particular question, don’t ask other ones here (you may optionally mark it answered). Instead, create new separate questions, as rules suggest README: How to use this Q&A? - Archive - Godot Forum

sash-rc | 2021-09-04 11:24

I just did a quick test and get_node does work in _ready. Keep in mind that _ready has to be registered for it to be called by godot:

static void _register_methods()
{
     register_method("_ready", &<class name>::_ready);
    ....
}

Are you 100% sure your paths are correct? Maybe you can try translating your logic to a GDScript script so you can make sure that it is actually a C++ problem.

omggomb | 2021-09-04 11:37

Well you should re-read the first post because the last I posted still relevant.

@omggomb: Nope I forgot to register it, guess I’ll just do it and see what happens. Will give news.

byjove | 2021-09-04 13:35

Ok thank you, my problem is now solved. I just didn’t got the priority of Godot’s API functions, but now my project is working.

byjove | 2021-09-04 13:46