Connecting signals through code with instantiated Nodes

Godot Version

4.5.1 stable

Question

Hello, thanks in advance if you reading this! The last few days i been trying to get GameDev more seriously and i have been doing a free course by Clear Code, to get things right since i felt like falling into “tutorial hell” starting completly on my own, having to search the most basic things to get something done and not having that much return in terms of learning. The course helped me a lot to get the start on basics and i trying to improve the last game made during it.

The idea is this 3D “bullet hell” game, where you have a spaceship and objects gonna fly in your direction. Expanding the idea to practice coding, i decided to add “classes” with different meshs, bullet cooldown and speed, and thinking about it passed to my mind the idea of having different player scenes, each with the same main node name with @export in the variables to change those values.

In the main menu the player would choose a ship, and it would store in a Global variable the node, until it, is fine, works well the system, but i have 3 signals (✦ For the player shooting to send it’s position to create the bullet ✦ For the HUD to show health ✦ for the Levels’ node to manage what happens when player dies) in the scene than are linked to the player, and since it will be instantiated, i can’t use the editor to connect, so i used GDScript, but i’m running on the problem that the nodes throw an error since when “_ready” of the other scripts are read, the player isn’t loaded yet, since as far as i understand the “bases” nodes of the scene are loaded at the same time so then you wouldn’t have it’s children.

I tried using “_init” on the Level’s node to instantiate the player, since it occurs earlier than “_ready” but i bump into a error since the node where the player you be instantiated ins’t loaded yet, and trying to do the same in the Level’s node throw the “Breakpoint” error.

One way around i found was storing a number on Global as the selected type of Ship, in the Main scene i would have the Player node (with the hitbox and meshes of the 3 types of Ships), since doing that way the signals would connect without problem, and in the “_ready” of the player node it would check the number and free() the unselected meshs and collisions, and assign the correct values to speed, cooldown and health. Works, but appears to be too much “brute force” and manual in case i try to reutilise this system for a future project!

Knowing that, what would be recommend to do since the signals not connecting are the problem? (or there are other?) I’m really trying to understend the basics and get best practices, even knowing than i will bump in a lot of bad ones since i’m only starting. Thanks for your time reading this!

A game jams is the best way to learn a game engine because you have limited time and you don’t have time to care how something doesn’t work, you just have to make it work or scrape it which I feel makes you understand the engine better.

One thing about thinking with signal:

  • The script creating it should be the one connecting the signal so you don’t run into those walls your talking about
3 Likes

If you instantiate and connect the player scene in level’s top node _ready(), there shouldn’t be any problems.
Btw “breakpoint” is not an error.

1 Like

I believe you misunderstand, the Node’s _ready function is called after all of it’s children are finish their _ready function. Sibling and especially Parent nodes aren’t ready at a given node’s _ready function.


Breakpoints are a debugging tool, you either have the keyword breakpoint written in a script, or clicked the gutter and a red dot appeared beside the line number, you can click the red dot again to remove the breakpoint.


I prefer to keep bullet spawning as a function on the player, usually adding bullets though add_sibling works perfectly fine for me; if you want to use a signal from the player then you must connect it after the player is instantiated. You may need to post some of your code for more specific help.

Similarly instead of a signal I would add HUD as a child of the player scene and update it through the player script where needed, not signaling up.

Sounds like your “Global” should be more specific, instead of just “Global” use it as a LevelManager and name it appropriately. It sounds like it already manages loading the player into the level, so it could also manage what happens when the player dies (maybe through a signal on the player).

1 Like

Thanks! I really misunderstood how things were loaded, and I must have clicked by mistake, activating the breakpoint. I agree with you in changing the bullet spawn to the Player’s node (was one of those things in the course that i haven’t changed but will!!) and with renaming conventions, I’m trying to have specifics for everything, but this one just got completely off my radar haha.

And at the moment, the Global script (autoload) was just holding a variable containing the chosen ship scene and passing it to the higher node of the game scene (which is just called Level and i going to rename it too) to then instantiate the player. Would be good to change how it works?