Hi, beginner here. I want to apply key & door to this First Person demo (FPP interation)

Basically (as I understand) the Player has raycast to check if the door has method "interact" then press a button to activate the door open animation

Player script

	if Input.is_action_just_pressed("interact"):
		if $Yaw/Camera/InteractionRay.is_colliding():
			var x = $Yaw/Camera/InteractionRay.get_collider()
			print("COLLIDING")
			print(x.get_name())
			if x.has_method("interact"):
				x.interact(self)

Door script


func interact(relate):
	if !animating:
		if state == CLOSED:
			$AnimationPlayer.play("open")
		else:
			$AnimationPlayer.play_backwards("open")

What I try was add a key variable to the door and a Key object with script

func interact(relate):
	get_node("/root/Main/Street/Door/Door").key = true

then change the door script to

var key = false

func interact(relate):
    	if !animating:
    		if state == CLOSED && key == true:
    			$AnimationPlayer.play("open")

It's not working obviously and I have no idea how to do this correctly or is that a better way, Please help!

Add a variable type bool named lock to the door. Then check the state of that boolean variable in your interact method.

You could even make it an exported variable for convenience so you can easily change the default for lock state via inspector in editor.

Anyways, once you have your lock and you check the lock state on interact() you next query the player for if they have the key. And this is where it can get more tricky.

Does your door need a unique key or can it be shared with other doors? Does your game even have other locked/lockable doors in the game/level?

If the answers should respectively be Yes, no and yes then you probably need to give the door/lock a unique identifier and the key needs to correspond to that.

Add a variable type bool named lock to the door I did added var key = false to the door but somehow get_node does not change its properties to true. Does door being an instance make any different?

you next query the player for if they have the key My understanding is that once the key is pressed (interact) the Door instance would be unlocked right away. Is this not correct?

Does your door need a unique key.. Yes eventually each door will have its key. Sadly right now I can't even get one to work

If your path is right, it should work. Check for errors to see if it actually found the object. If no errors, use a print to see if the door variable actually changed. This is one of those cases where a signal is better than using that type of get_node, but if your path is right, if the object is in the scene, if the variable is in the right scope, it should work.

The error is "Node not found" so I guess the get_node path is not correct. How should I correct the get_node path if the door is an instance? The set up: Main Tree Door Tree

a signal is better How should I implement a signal for this set up? I will also give this method a try

Thank you

maybe it's throwing the error right at the beginning because it's looking for the node before it's been added to the scene tree?

You can make a check for it via is_in_tree() before running the rest of the code.

func interact(relate):
    if is_inside_tree("/root/Main/Street/Door/Door"):
        get_node("/root/Main/Street/Door/Door").key = true

I got error "Too Many Arguments for is_inside_tree()"

Another thread said that both scripts have to be in active scene tree? Maybe that's why my get_node path isn't working?

It feels so simple and I've read possible solutions (signal, preload script) but I've have a hard time implementing it (like what to write and where to put it in script

Try right clicking on the door node and then on "editable children". Once you do that, you'll be able to see all the children from the main scene. Do this in the main tree. Just forget about checking if it's inside the tree. I'm not sure why it's saying you have too many arguments when there is only one. It appears to be having a problem accessing that script from the main tree. It's easiest to put a script on the main node of the sub scene (door) and then call everything from that, then you shouldn't have this problem. I could be wrong, of course, but I ran up against something similar a little while ago.

Yes! I've recreated a new door, its open animation, reconnect the key, put them on main tree and it works

In the beginning I just work with the door instance from original project (which I stripped down). I guess it might be something under the hood I don't know about

Thanks everyone for helping!

@Gowydot said: I got error "Too Many Arguments for is_inside_tree()"

Yeah I was posting that half asleep, it should have been:

func interact(relate):
    var door = $"/root/Main/Street/Door/Door"
    if door.is_inside_tree():
        door.key = true
a year later