Trouble updating variable from shooting script in Godot

Godot Version 4.2.2

Question

Hi,

I’ve got an issue with updating a variable (Last_Interacted) from my shooting script in Godot. Here’s an overview of the problem:

  • I have a shooting script attached to a RayCast3D node in my scene.
  • The shooting script is responsible for detecting collisions with objects in the scene and updating a variable (What_Hit) with the object that was hit.
  • I’m trying to update another variable (Last_Interacted) in my Game Manager node script with the value of What_Hit when the shooting action is triggered.
  • However, despite the shooting script appearing to detect collisions correctly and update What_Hit, Last_Interacted does not seem to be updated from the shooting script.

Here’s my shooting script:

extends RayCast3D

var What_Hit

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if is_colliding():
		What_Hit = get_collider()


func _input(event):
	if event.is_action_pressed("Shoot"):
		var game_manager = get_node("/root/Node3D/Game Manager")
		if game_manager != null:
			game_manager.Last_Interacted = What_Hit
			print(game_manager.Last_Interacted)
		else:
			print("GameManager node not found!")

Game Manager script:

extends Node

var Last_Interacted

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	print(Last_Interacted)

And my relevant scene tree:

root
Dialogic
Node3D
-Game Manager
-Player (sub-scene)
- -Collision
- -Head
- - -Camera
- - -RayCast3D (shooting script)

Have I misunderstood how scripts interact with eachother in Godot? Thanks in advance

(I first tried using signals for this, but found no way to connect it to the main scene)

Can you update the project for me take a look?

1 Like

This might work instead:
What_Hit = get_collider_id()
get_collider() is only going to return information useful to whatevers involved in that current collision, where get_collider_id() will return the instances global ID. I’d bet the code is technically working fine, your game manager just doesn’t know what to do with it.

note- I’m pretty new at this, so I’d verify my explanation for accuracy.

1 Like

Apologies for not closing this earlier- I’ve gone with another solution that works much better. Thanks for being willing to take a look anyhow!

Apologies for not closing this earlier- I’ve gone with another solution that works much better that ill type up for anyone searching for a solution in the future.

I’ll be sure to remember get_collider_id() though! Thanks for helping out a fellow newbie!

For anyone searching for a solution in the future my raycast3d script now looks like this:

func _ready():
	add_exception(owner)

func _physics_process(delta):
	prompt.text = "" #irrelivant to solution
	if(is_colliding()):
		var detected = get_collider() 
		if detected is Interactable:
			prompt.text = detected.get_prompt() #irrelivant to solution
			if Input.is_action_just_pressed(detected.prompt_action):
				detected.interact(owner)

For the objects that I want to be interactable, I have just given them a class called “Interactable”.
class_name Interactable

class_name Interactable

And the raycast script will trigger this function on the interactable node to emit this signal which is then received by an interaction manager node that I set up to handle logic:

signal interacted(body)
func interact(body):
    emit_signal("interacted", body)

(Thanks to Nagi’s interaction tutorial)