OnAreaEntered(Area2D area) error.
Asked Answered
B

1

0

I have two scenes: Player and Coin. Both of these scenes are inherited from Area2D. Code of these scenes:
Player:

public void OnAreaEntered(Area2D area)
{
	if (area.IsInGroup("coins"))
	{
		area.Pickup();
	}
}

Coin:

public void Pickup()
{
	QueueFree();
}

OnAreaEntered checks if the player has touched an Area2D object, and if he has touched a Coin, on the coin touched by the player, the Pickup() method is launched. But it doesn't work. I can't solve this problem:

Well, I was able to solve this problem by simply changing the data type "Area2D" to the data type "Coin" in the arguments of the OnAreaEntered method. But there is a problem: the OnAreaEntered method will recieve not only data with the data type "Сoin". What I should do?

I know that the OnAreaEntered method really recieves coin when the player touches it:

public void OnAreaEntered(Area2D area) {
    GD.Print(area.HasMethod("Pickup")); // TRUE
}
Brumal answered 24/12, 2023 at 14:9 Comment(0)
B
1

Brumal
I solved this problem using the "area.Call()" method:

public void OnAreaEntered(Area2D area) {
	if (area.IsInGroup("coins")) {
                // Method Call() :
		area.Call("Pickup");
	}
}
Brumal answered 25/12, 2023 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.