0 votes

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.

Godot version 3.3.3
in Engine by (14 points)

1 Answer

0 votes

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.

by (1,438 points)

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?

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 https://godotengine.org/qa/57/readme-how-to-use-this-q%26a

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.

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.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.