0 votes

Hello,

i have the following Porblem. the player can create some buildings while playing. that for i put a preview on the cursor. this preview has an area2d to detect if its colliding with some solide shapes.
but unfortunately its detecting itself all the time.
i already tryed to use a different layer and a different mask but that dosnt work.
its detecting itself.

func PreShow(): #zeigt das zu Platzierende element an der Maus an
var childCount = get_child_count()
if Globals.placeItem == true:
    if childCount == 0:
        whatToPlace = MAIS.instance()
        whatToPlace.add_to_group("DoNotCollide")
        whatToPlace.connect("body_entered",self,"CheckForCollide")

        add_child(whatToPlace)
    else:
        var xy = Globals.transformMousepositionToMap()
        var x = xy[0]
        var y = xy[1]

        whatToPlace.position.x = x
        whatToPlace.position.y = y
        if prePosition != Vector2(x,y):
            CheckForCollide(whatToPlace)
            prePosition = Vector2(x,y)
        if Globals.willCollideWithSolid:
            whatToPlace.modulate.r =3.0
        else:
            whatToPlace.modulate.r = 1.0
if Globals.placeItem == false and childCount > 0:
    for i in get_children():
        i.queue_free()

func CheckForCollide(item):
if item:
if item != whatToPlace:
Globals.willCollideWithSolid = true
else:
Globals.willCollideWithSolid = false

print(Globals.willCollideWithSolid)
Godot version v3.2.3.stable.official
in Engine by (68 points)

1 Answer

0 votes

with "itself" you mean the area2d used for the check is detecting the area2d used for the "future" solid building, is that right?

if that's the case you can either temporary remove the are2D used for the future solid building (activate it only after it is placed) or if you really need it, work with different collision layer

by (1,213 points)
edited by

not quite

the area2d node is detected to the cursor. its only function is to detect if the place under the cursor is already build or still free.
the area2d node on the curser fires all the time a collision message, but there should be none. if i print the body whos colliding i see that it is the area2d itself.

i'm pretty sure no area can detect itself, what would even be the point?

are you sure there are no other bodies behind it? try creating a test scene with nothing but the "check" area to see what happens.
also, the collision signal comes with the area/body information, maybe you can print the area/body name to check what is colliding with what

finally, i just noticed that in your code you call CheckForCollide(WhatToPlace) inside the PreShow() function: this automatically resolve in Globals.willCollideWithSolid = false, what's the reason behind this?


ps: your code seems overcomplicated, while this is a relatively simple task: you can achive this with a area2D (let's call it checker), that fires a signal to itself when body is entered/exited (you can use the signal menu in the editor, faster and safer than writing the connect code). on the checker code you can simply add a new parameter, let's call it free_land=true, and the signal will change this value directly.

func _on_Checker_area_entered(body):
    free_land=false

func _on_Checker_area_exited(body):
    free_land=true

if there is the possibility of multiple bodies laying together behind the same checker, you can either keep count of them (func _on_Checker_area_entered(body): body_count+=1), or you can use Checker.get_overlapping_bodies() which returns an array of all the current colliding bodies, or a mixture of all the 3 techniques.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.