Calling a function from one script to another

Godot Version 4.2.1

I have a RayCast3D called InteractRay set up that detects interactable objects, in this case a weapon. When you press the interact key, my plan was to call the weapons_manager node script that handles everything weapon related and add it to the weapon stack. I’ve seen some suggestions of using signals for situations, but I don’t see any that would help in this situation. I’ve attached an image of the scene tree as well as the InteractRay and weapons_manager code below. Any help or suggestions is appreciated.

image

InteractRay

func _physics_process(delta):
	InteractPrompt.text = ""
	Reticle.show()
	if is_colliding():
		var Detected = get_collider()
		
		if Detected is Interactable_Weapon:
			Reticle.hide()
			InteractPrompt.text = "[E] Pick Up " + Detected.name
			# if Input.is_action_pressed("interact"):
				# Call Pick_Up_Weapon function in Weapons Manager
				# pass along Detected variable

weapons_manager

func Pick_Up_Weapon(body):
	
	var Weapon_In_Stack = Weapon_Stack.find(body.weapon_name,0)
	
	if Weapon_In_Stack == -1: # Pick Up Weapon
		Weapon_Stack.insert(Weapon_Indicator,body.weapon_name) # Weapon_Indicator = 0
		
		# Zero Out Ammo Resource
		Weapon_List[body.weapon_name].Current_Ammo = body.current_ammo
		
		# Update the weapon stack and run exit function to update current weapon
		emit_signal("Update_Weapon_Stack", Weapon_Stack)
		exit(body.weapon_name)
		body.queue_free()

Global EventBus signals would be helpful, as it connect through EventBus AutoLoad that’s specifically exists to take any signal and any nodes can connect through it

the weapon manager node is added via editor? then you can just

@export var weapon_manager_node:Node3D

on interractRay code , to call something on weapon manager

1 Like

This worked perfectly :+1: Thank you!

1 Like