Call a function in a script from another script

Version 4.3

Hello, i’m new (sorry for my english, i’m italian and trying my best ahah)
I have a little problem. I want to call a function from a script in another script and i’ve tried to do that in a few ways that works but not correctly. i’m trying to implement a death animation in the final game created following the tutorial by Brakeys.
I have a firt scene called killzone, with the script in witch i want to call a function from another scene with another script called Player. The function in Player play the “death” animation. The scene killzone is also in the scene called Slime, this is a enemy. All these scene is in another scene called Level 1 (actualy there is only a level, but i want to create more levels). The game is a 2d platform

i’ve tried few things. The first try was using get_node(“path”) and that way worked really well but i can’t create new level because in the path i have to add the name of the scene “Levelx”. for example for level 1 a had to use “root/level1/…”, but the script in the level 2 have to be “root/level2/…” and i dont want to create scenes of the killzone for each level.
The second way i tried was using the signals. killzone emit a signal and a function in Player is activated. That way works when the player fall down but not when the player touch the enemis (slimes).
I don’t add any code 'couse i think that they’re not really usefull, but if you ask i immediately add them <3
here the scenes trees
Screenshot 2024-11-30 194119
Screenshot 2024-11-30 194129
Screenshot 2024-11-30 194140
Screenshot 2024-11-30 194145

sorry again for my bad english. love u, if u help me and if you doesn’t <3

You could try to connect the killzones’ signal when each slime is instantiated and the level is created. The player would be in a group named “player” that the killzone can reference using get_tree().find_node_in_group(“player”).

E.g.
var player = get_tree().find_node_in_group(“player”)
killzone_signal.connect(player.death_anim)

Sorry man, i don’t think i undestand :point_right: :point_left:

I have to create the signal and the var in killzone script

signal killzone_signal
var player = get_tree().find_node_in_group(“player”)

and then i have to create the function death_anim in the player script, but first i have to put the player node in a global group?
if it is, i’ve tried but i have this error “@implicit_new(): parameter “data.tree” is null”

about the first thing that you said, how do i connect the killzones’ signal when each slime is instantiated and the level is created?

Recursive helper function to search for a function in the node hierarchy
func _recursive_find_function(node: Node, function_name: String) → Node:
if node.has_method(function_name):
return node

# Recursively search the children of the node
for child in node.get_children():
	var found_node = _recursive_find_function(child, function_name)
	if found_node != null:
		return found_node

return null

Let see if i understand well, you’re trying to call the death animation when player dies, right? If that’s the case you’re detecting if player touches the killzone using Area2D and the body_entered signal, body_entered signal also sends the body that triggered the signal so the body parameter should be your player, with that you can call body.func_that_trigger_the_death_anim().


Otherwise you can use groups for that:

# In your player script
func _ready() -> void:
	add_to_group("player")
# And whatever you need to call any function in player script
get_tree().call_group("player", "your_function_name")
# Or 
var player_node = get_tree().get_first_node_in_group("player")
player_node.your_function()

it worked!!!
the funny thing is that I had tried it and it didn’t work, but now it works ahah
Thanks a lot <3