New to Godot - Question about 2d "Door"

Godot Version

Godot 4.2.2

Question

Hello! First time poster to this forum!

I am working on a 2d platformer as my first godot project (shocker right?!?) and I am running into an issue that I am not quite sure how to deal with.
Area2D is able to detect when other bodies collide with it…which works great for things like collectables (keys or coins)…however…I am trying to create a scene for a “door” object. I want trigger queue_free() when the player collides with it ONLY IF the correct number of coins have been collected.

  1. I am not sure what type of body to use for this scene. I only see the “on_body_entered” signal on Area2D, but when the Area2D doesn’t actually BLOCK the player. I have tried using things like StaticBody2d and RigidBody2d but I don’t know how to look for the player colliding with the door without the “on_body_entered” trigger.
  2. The best way I can think to TEST for the correct number of coins to have been collected before triggering queue_free() is to create EXPORT variable(s) for each door (each require a different number of coins) and then compare the “current coin count” to the object property for each door. Is there a better (easier) way?

Thanks in advance for any help!

I’ve used global variables for collectibles - that way any part of the code can adjust and check the variable.

If you’d like code examples, let me know. :smile:

To answer your questions:

  1. You can use a combination of Area2d and StaticBody2d. Use the Area2d to detect the player, use the StaticBody2d to block the player from entering the door. You can set the StaticBody as a child of the Area2d so that it is freed when you free the Area2d.

  2. I don’t see anything wrong with this approach. Since each door needs different coin requirements, using exports to set the required coins for each door is good. All you need to do is to compare how much coins the player has to the required coins once player enters the door Area2d. If it checks out, queue_free the area2d, if not, do nothing

2 Likes

@pdhales72 @FreakyGoose Thank you both for your replies! It wont let me give TWO solutions so…

Here is a screenshot of how it ended up:

I have two collisionshape2d objects. The one to the left is the Area2d.

I then added two properties to the staticbody2d called nCoins and nKeys that checks the players current count of both items. So far so good.

@pdhales72 , so I am not just ripping code from you, I am going to see what I can come up with on my own if that is ok. I will lean on you if my attempts aren’t working. Thanks!

1 Like

No problem.

Are you going to make the door change shape or disappear once it’s opened?

Sorry @pdhales72 ! Busy Week!

Yes! the plan is to remove the door if the criteria is met.

1 Like

Sounds good. I used similar code to hide items in one of my games. :smile:

func _on_body_entered(body):
	if body.name == ("player"):
		global.score += 10
		print (global.score)
		$AnimatedSprite2D.visible = false
		$CollisionShape2D.set_deferred("disabled",true)
		end_level()

The end_level() is just a check to see if the player has collected enough coins to finish the level.

How about setting two different sprites? One for open and one for closed.

You could do that easily enough by using AnimatedSprite2D and having two animations…CLOSED and OPEN. :door:

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