Trying to setup interact system / trying to use variable from 1 scene in another

Godot Version



Okay so sorry if this is badly worded, I’m still very much learning.

Basically, I am trying to setup an interaction system, my player scene has a raycast with a script attached that I added to autoload and in that script I declare the variable player_interact with the assumption that I just made it a global variable that other scenes/scripts can call upon. Unless I am wrong.

I am able to confirm if I comment out the function in the crate script that the player interact script functions and prints “opening” into the output whenever I hit E on the crates.

I have a basic crate script that basically (I think at least) will print the word success if I go up to the crate object and hit the E key.

But my program crashes and this is what is in the log

E 0:00:00:0682 @ _physics_process(): Node not found: “Player/Head/Camera3D/PlayerInteract” (relative to “/root/Environment/Crate”).
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1638 @ get_node() @ _physics_process()

And bonus picture of my node tree

Now for the crate script, I tried declaring the variable outside of the function, inside of it, I tried right clicking the PlayerInteract node and copying the node path and pasting but nothing. I am at a loss.

I understand the error is complaining about that variable not existing or being invalid but not sure what else I can do to try fixing it.

Crate Script
Player Interact

Reading more and I’m starting to suspect this would work if the crates weren’t separate scenes but were nodes that were part of the player scene. So maybe this won’t work then.

the problem is on
show your script, something doesnt work there

NodePaths are relative to the node you attach the script to. That’s why your script is failing.

A way to solve this would be to implement a method in your crate script like open():

func open() -> void:
    print('open crate')

and in your player interact script you can do:

func _process(delta):
	if player_interact.is_colliding() and Input.is_action_just_pressed("Interact"):
		var obj = player_interact.get_collider()
		if obj and obj.has_method('open'):
1 Like

I made the changes to my player interact and added that to the crate script, should my crate script still have the stuff I already had or should that be taken out? Because with it commented out I do not get any printed messages in the log and with it in the game crashes with the same error.

I might be overthinking the crate part

The script needs to be attached to whatever physics object your crate is or it won’t work.

And, yes, you should remove the _process() function from it.

I have it attached to the crates scene, unless you mean it has to be attached to the CSGBox3D instead

Oh yep that IS what you meant. That worked! Thank you dude!

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