If pressed and area has bodies doesn't work

Godot Version

4.3

Question

why doesn’t this double if work

func _physics_process(delta):
	if Input.is_action_just_pressed("1"):
		if $Area2D.has_overlapping_bodies():
			one = false```
i try with only one of either "if" ,it works, i try with both ,even with"and",it doesn't, why such?
and `func _input(event: InputEvent) -> void:`doesn't work either ,for some reason if you were asking

Can you put print statements inside the if-clauses to check if they are being reached?

func _physics_process(delta):
	if Input.is_action_just_pressed("1"):
		print("1 is being pressed")
		if $Area2D.has_overlapping_bodies():
			print("Area has overlapping bodies")
			one = false

Then check the output and see if something gets printed when you press 1

1 Like

sorry if i’m late but it doesn’t print that^

But you claim that there are bodies overlapping the area when you press 1?

sorry ,late again, yes i do claim that there are, i checked myself

And the physics mask and layer are the same?

try this then please:

func _physics_process(delta):
	if Input.is_action_just_pressed("1"):
		print("1 is being pressed")
		if $Area2D.has_overlapping_bodies():
			print("Area has overlapping bodies")
			one = false
	if $Area2D.has_overlapping_bodies():
		print("Area definitly has overlapping bodies")

gives me first and third message but not the third

first, third and not third?

i meant not second f#####k

This probably means that they are true at seperate frames. You can test it by doing this:

func _physics_process(delta):
	print("New Frame!")
	if Input.is_action_just_pressed("1"):
		print("1 is being pressed")
		if $Area2D.has_overlapping_bodies():
			print("Area has overlapping bodies")
			one = false
	if $Area2D.has_overlapping_bodies():
		print("Area definitly has overlapping bodies")
1 Like

alirght i got a bad new and good new, the good one is that i found a solution, the bad one is that i lied about the good new…just kidding :stuck_out_tongue:
here’s the solution:

func _physics_process(delta):
	if Input.is_action_just_pressed("1"):
		if one==true:
			one = false
            inspect()
func inspect():
	if $Area2D.has_overlapping_bodies():
		one = true

this is just a part of the code, not the whole code :kissing:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.