-1

I faced a stack overflow problem in GDScript.

Debugger

(Code to reproduce :)

extends Node
class_name MatchSession

func add_child(ch, un=true):
   add_child(ch, un)
   if get_child_count() == 2:
      _start_match_session()
Rob
  • 14,746
  • 28
  • 47
  • 65
  • 1
    Welcome to SO. While I don't know anything about Godot nor gdscript, the possible reason why you're getting a stack overflow is because you're infinitely recursing into add_child(). – ewokx May 10 '21 at 08:26
  • **DO NOT post images of code, data, error messages, etc.** - copy or type the text into the question. [ask] – Rob May 10 '21 at 08:30

1 Answers1

0

In your code, add_child calls add_child recursively, no stop condition:

func add_child(ch, un=true):
   add_child(ch, un) # <--
   if get_child_count() == 2:
      _start_match_session()

Note also that Node.add_child is not virtual. You are shadowing it. I suggest pick another name for your function.

Theraot
  • 31,890
  • 5
  • 57
  • 86
  • can i call with dot ` .add_child(ch, un) ` ? and keep my override of Nodes add_child() method? – Aspirational May 10 '21 at 08:44
  • @Aspirational that should work. However, I must reiterate that `add_child` is not virtual (edit: and thus this is not an override). If some other code tries to call `add_child` without the proper type, it will call `Node.add_child`. Similarly, Godot will not call your `add_child`. – Theraot May 10 '21 at 08:48