0 votes

extends StaticBody2D

signal chest_opened

var keytaken = false
var inchestzone = false

func _on
Area2Dbodyentered(body: PhysicsBody2D): #area2d - > key
if keytaken == false:
keytaken = true

func process(delta):
if keytaken == true:
if in
chestzone == true:

func _on
chestzonebodyentered(body: PhysicsBody2D): #chest zone -> key
chest_zone = true
extends StaticBody2D

onready var only_once : bool = true
onready var animationplayer = $AnimationPlayer

func _ready():

func onKeychestopened():
if onlyonce == true:
once = false
If the player enters the chest's zone before having the key, the chest stays locked.
Although, as soon as the player obtains the key, the chest instantly opens.
Can anyone help me come up with a solution for this issue?
Thanks in advance!

Godot version 3.5.1
in Engine by (15 points)

1 Answer

0 votes

You want to check to open the chest really only on two instances.

  1. The player obtains the key. Check if they are in the chest zone.
  2. The player enters the chest zone. Check if they have the key.

You can removed the process function. The reason the chest instantly opens when the player obtains the key is because in_chest_zone is never set to false. Set it to false when the player exists the chest zone.

by (8,528 points)
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 Frequently asked questions and 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 [email protected] with your username.