-1

add_child: Parameter "p_child" is null

I suspect this is a Godot editor error because the game runs fine.

This one is hard to describe but basically I have a "memory" card game that runs from a grid. You select your difficulty and then press New Game. The first time, there are no errors but if you win or select "New Game" the errors start piling up and they seem to double each time, maxing out at 100 errors each time you press the new game button.

The only thing on the googles seems to be related to C++ (which that p_child name definitely suggests) but I'm not using C++, I'm using GDScript... this is why I'm thinking maybe it's just an editor error and Godot is just getting confused that I didn't add_child in the C++ way.

Any thoughts?

EDIT the code generating the errors is this:

func dealDeck():
    randomize()
    deck.shuffle()
    var c = 0
    while c < deck.size():
        Game.get_node('grid').add_child(deck[c])
        c += 1

Game is just a reference to the main scene (/root/Memory) and deck is an array created previously that contains all the cards. I'm 99% sure that the problem isn't in the deck creation.

and to be clear, I am clearing the grid when a new game is started but I have my suspicions that something is going wrong with the whole process (even though the game is working)

Buck Russell
  • 131
  • 1
  • 9
  • Is the error on code on an autoload? I'm guessing the error is caused by a change of scene. Which would imply there is some code that remains despite the scene changing that reads something from the scene. And autoload code could do that. – Theraot Jun 22 '21 at 11:10
  • Interesting. I am using an autoload of my main script file (I suspected this wasn't the proper way of doing it, but I'm still learning here!) – Buck Russell Jun 22 '21 at 12:41
  • If that code is the source of the problem, it would mean the cards became invalid. But if that is the case, it does not explain why it continues to work. Are you sure you get the cards in a different order each time? Are there other add_child calls? I assume something else is messing it up, and that would be something that happens on new game. But I'm having a hard time imagining what. – Theraot Jun 23 '21 at 05:18
  • this is why I suspect it might be an editor error/glitch type thing. because if that item were null, then there wouldn't be a deck to play the game with (and the errors don't cause a crash). I'm not saying I didn't do something wrong. it's probably the most likely answer. but i'm pretty good about squashing bugs/errors in my code and this one has me stymied. – Buck Russell Jun 23 '21 at 20:42
  • I want to reiterate these are failed assertions. Crashing Godot is not as easy as passing an invalid reference somewhere. I think you should try to make a minimal project that reproduce the problem. Either you figure out the source of the problem in the process of making it, or you have an bug to open on Github and the minimal project will help the engine developer track it down. – Theraot Jun 24 '21 at 00:12

1 Answers1

0

Since Godot was written in C++, the errors given in the console are from the C++ side of the editor. This does not mean that your GDScript code is not responsible for the error. It seems like you're trying to add a child to an object but the child you are trying to add is null. This may be due to many reasons. You should start by looking at how you add the cards to your scene when a new game starts. Also, if you could show us some of the code we may be able to help you further.

  • The weird thing is that, despite the errors, the game still runs fine. A player would never know there were errors. In the comment above, someone mentioned that it might have something to do with using Autoload, which I am doing with my main script file. The cards are being generated and then sent to the main scene (which the script is not attached to). It's confusing though because if that were the case, why is it still working exactly how I intended (minus the weird errors of course)? – Buck Russell Jun 22 '21 at 12:51
  • @BuckRussell It is a failed assertion. Something is calling `add_child` with an invalid reference. That is wasted effort. Yet, that it happens suggest something is up with your code. Presumably you didn't call `add_child(null)`, but there must be a call to `add_child` and whatever is being passed it became invalid or null somehow. Since you say the game works correctly, I presume the code that is calling `add_child` passing a null or invalid reference is running when it shouldn't. – Theraot Jun 22 '21 at 15:57
  • @BuckRussell Here, this is the line with the assertion: https://github.com/godotengine/godot/blob/64cfd5cbaaa7a71283d460456ab098092da39fab/scene/main/node.cpp#L1160 - What is calling it? I don't know. You could search every `add_child` in your source. Now, why do I suggest Autoload? Because, in your question you say the error begins to happen after the game is completed for the first time, and Autoloads will remain there despite scenes changing or being reloaded. So that is where I would look first. – Theraot Jun 22 '21 at 16:02
  • This is the code that seems to be throwing the errors: `func dealDeck():` `randomize()` `deck.shuffle()` `var c = 0` `while c < deck.size():` `Game.get_node('grid').add_child(deck[c])` `c += 1` Game is a reference to "/root/Memory" which is the main scene. I've tried moving the code in to Memory.gd (the main scene's script) but it made no difference, the errors still happen but it's definitely that add_child (even though the deck is dealing fine). i tried editing the code to look better but apparently SO can't properly format code... – Buck Russell Jun 22 '21 at 18:34