Search code examples
gdscriptgodot4

the main scene just works fine but player doesn't collide with other scenes which are instanced in other level scenes so script works for main scene


(new here) Following a tutorial, this is a platformer game and they made a system where the level.gd can work on all levels although that doesn't work in my case the player doesn't collide with deathzone or ending collision shapes. so it doesn't emit the signals and basically, my game becomes unplayable, I thought it was an engine error so I made this project in Godot 4.0.3 but even now loading in 4.1.1 the problem is the same maybe its something as simple as doing something from inspector tab but I don't know

heres my level.gd

# same script is used on all my lvls 
extends Node2D

@export var next_level: PackedScene = null

@onready var start = $Start
@onready var exit = $Exit
@onready var deathzone = $deathzone
var player = null

func _ready():
    
    player = get_tree().get_first_node_in_group("player")
    if player!=null:
        player.global_position = start.get_spawn_pos()
    var traps = get_tree().get_nodes_in_group("traps")
    for trap in traps:
        # trap.connect("touched_player", _on_trap_touched_player)
        trap.touched_player.connect(_on_trap_touched_player)
    
    exit.body_entered.connect(on_exit_body_entered)
#   deathzone.body_entered.connect(_on_deathzone_body_entered)
#   deathzone.connect("body_entered", _on_deathzone_body_entered)
    deathzone.body_entered.connect(_on_deathzone_body_entered)

func _process(delta):
    if Input.is_action_just_pressed("quit"):
        get_tree().quit()
    elif Input.is_action_just_pressed("reset"):
        get_tree().reload_current_scene()


func _on_trap_touched_player():
    reset_player()

func _on_deathzone_body_enetered():
    reset_player()

func reset_player():
    player.velocity = Vector2.ZERO
    player.global_position = start.get_spawn_pos()

func on_exit_body_entered(body):
    if next_level != null:
        if body is Player:
            exit.animate()
            player.active = false
            await get_tree().create_timer(1.5).timeout
            get_tree().change_scene_to_packed(next_level)

func _on_deathzone_body_entered(body):
    reset_player()

main LVL NODE OREDR LVL2 Node order

I did try to reloading project or making the simple death zone scene again but its always like from which lvl I create the deathzone it works, so instead of saving brach as a scene I created its scene separately so now it works in other lvls but doesn't work in main lvl on other hand the exit part only work for main scene the other spike trap and saw traps are not working on other scenes as well

one thing that I noticed that all the nodes that are not working are all area2d or have area 2d inside their scene (obv) since it looks like a collision problem

any info on this would be appreciated


Solution

  • Since you are having problem with Area2D, I suggest enable Visible Collision Shapes from the debug menu to see if they are positioned as expected while the game is running.

    It might also be a good idea to review their collision_mask to see if it is correct.


    I suspect that the problem is something not being stored when you create the scenes.

    Thus, compare the properties of the instances you create in the other scenes and with the ones in scene where they work. Ideally, whatever discrepancy you find you would also set in the exit and deathzone instances, so they are set for future instances you might create.

    A workaround might be to copy the instance from the scene where they work to the others.