Stack overflow when trying to make a stack (this is not irony)

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

Hello! I’m making a 3D game about burgers where I have the Ingredients (like bread, burger, cheese, etc.) that are RigidBodies and can be stacked up on top of a Tray (so you have to put the ingredients on the right order, but this is not important for this question).

I have an Area node inside the Tray that detects when an Ingredient intersects with the Tray, and when it does, it executes the following code:

func _on_Area_body_entered(body):
    if body is Ingredient: # Ingredient is inhereted from RigidBody
	    body.get_parent().remove_child(body)
	    $Ingredients.add_child(body) # Node that serves as a container for ingredients
	    body.mode = RigidBody.MODE_STATIC

…and then I get a stack overflow error.

Sounds like this might be your problem? There are some workarounds suggested there.

jgodfrey | 2023-01-10 03:50

:bust_in_silhouette: Reply From: SteveSmith

It looks like when an Ingredient is entering the area, you’re adding a child to the Ingredient, which automatically enters the area when it’s created. A child is then added to this child, which enters the area, and so on. I might be wrong, it depends what $Ingredients is.