How to get script values from Body/Area Entered?

Godot Version

4.1.2

Question

Hi, I’m still not great with Godot but I’m getting there (hopefully!). I used to develop in Unity, so this question relates to the GetComponent concept from there. Essentially, what I want to do is when two bodies collide (in this case an arrow and another object that I call an Interactable) I want to use data from the Arrow script in the other Interactable’s script to apply a certain effect. Problem is, I can’t figure out a good solution that isn’t really slow. With unity, you would just use something like arrow.GetComponent().value in the collision, but I’m struggling to find a good counterpart in Godot. I’m using the GetNode function right now with the name of the arrow to get the Arrow node, but that seems clunky and wrong. If there’s a good way to do this, let me know!

This is what I have currently for the Interactable script, to make it a bit clearer:

public void OnBodyEntered(Node body) 
	else if(body.IsInGroup("arrow"))
	{
                // ApplyEffect takes an integer id as a parameter
		ApplyEffect(body.GetNode<Arrow>(body.Name.ToString()).id);
	}
}

How is your physics body node tree structured?

(post deleted, i totally misread your post)

temp
Like this, RigidBody2D as the root, and a sprite and collision shape as the children.

What node are you trying to get with body.GetNode<Arrow>(body.Name.ToString())?

Ok, sorry for the confusion, I solved it. I wanted to access my Arrow node by using body from collision, and did not know you could just cast Node body to Arrow and it would just work. Definitely a big misunderstanding from me. Thank you for the help.

1 Like