Search code examples
godotgdscript

Godot: Instance scene lost it's group


I'm creating a scene calling "Circle" that inherits from scene "Obj":

extends Obj

func set_color(color):
    match color:
        COLORS_CONST.COLORS_DICT.red.name: 
            $RigidBody/Shape.color = COLORS_CONST.COLORS_DICT.red.value
            add_to_group(COLORS_CONST.COLOR_GROUPS.hot)
        COLORS_CONST.COLORS_DICT.green.name: 
            $RigidBody/Shape.color = COLORS_CONST.COLORS_DICT.green.value
            add_to_group(COLORS_CONST.COLOR_GROUPS.cold)

In the "Main" scene I create "Circle" instance, this is "Main" script:

    var circle = obj.instance()
    circle.set_color(color_name)
    add_child(circle)
    print(circle.is_in_group(COLORS_CONST.COLOR_GROUPS.hot))

Also use an Area2D call "ScoreCount". To detect when "Cirlce" is collide with "ScoreCount" to count score, I connect with "ScoreCount" signal:

    $ScoreCount.connect("left_count", self , "on_score_count")
    $ScoreCount.connect("right_count", self , "on_score_count")

this is "ScoreCount" script:

signal left_count
signal right_count

func on_ScoreCount_bodyEnter(body):
    print(body.is_in_group(COLORS_CONST.COLOR_GROUPS.hot))
    if body.is_in_group(COLORS_CONST.COLOR_GROUPS.hot):
        emit_signal('left_count')

But I dont know why when I put print in "Main" script and in "ScoreCount" script, it print different value. In "Main" script, after create instance and add to main tree, it still have group. But when collide with "ScoreCount", it has no group, so counting score not working correctly.

Anyone know about this, pls help me out.


Solution

  • The object colliding is the physics body. Which seems to be a RigidBody2D. But you didn't set the group on that Node:

    $RigidBody/Shape.color = COLORS_CONST.COLORS_DICT.red.value
    add_to_group(COLORS_CONST.COLOR_GROUPS.hot)
    

    Do this instead:

    $RigidBody/Shape.color = COLORS_CONST.COLORS_DICT.red.value
    $RigidBody.add_to_group(COLORS_CONST.COLOR_GROUPS.hot)
    

    And the equivalent on the other branch.