Call in other script the function _on_area_2d_area_entered(area)

Godot Version

4.1.3

Question

Hi!, for begin my english is not so good, then the question is:
How do I call this function (_on_area_2d_entered) for use?
“area” I don’t know what parameters they respond to, so I was stuck

_on_area_2d_area_entered is a signal. It gets called automatically.

Docs: Area2D — Godot Engine (stable) documentation in English

How do I declare this function it in another script?

Expanding in the OP, it is a signal which is automatically called when another Area2D enters into it:
Area2Dentered
i.e. a godot built in function / method call which: is not intended for you to call manually.
“area” would be the parameter area:Area2D which entered into it the area:Area2D triggering the call (singular parameter).
Area2Dentered_2

It is local to the script it is in i.e. you would not “declare” it in another script.
You can have an identical fuction / method in another script, but it will be local to that script.

That’s the magic of signals… an object instanciated while inheriting from an “signalable” class… just “receives” this signal by the already implemented system environment by implementing the specific signal method…
…so by implementing it it will already be called by the system.

This said:
But in this case i think the Node2D is not enough and it should extends Area2D (??) with it’s (signal) methods from area_entered(.. to body_shape_exited(.. (as shown above)
…but also i’m not to deep into Godot yet and there might be a way to “connect” it via the IDE !?

If you want to call it manually you need to add a parameter to the function call. It’s expecting an area2d to be passed.

1 Like

oops my goof, misread / misunderstood, sorry! :upside_down_face:
I read it as adding a second parameter.

leave this here anyway:
No pro but as far as I understand “calling it” with 1 parameter (its inteded parameter) is not problematic using the proper paths, it simply returns the invasive area:Area2D and I believe that is the purpose: to not have to do it manually.
If you want it to do more would require more parameters depending on what you want to do.

could you give me an example?

Don’t ever call it manually. Just move code inside of it into a separate function, and call that function from both there and anywhere else where you want same code to execute:

func _on_area_2d_area_entered(area):
     if area.get_parent().is_in_group("player"):
          cute_function()

func _ready():
     cute_function()

func cute_function():
     # do something

That said, I can’t imagine where you’d need to call exact same function from both “ready” and “on area entered” since those are 2 very different events. But just wanted to illustrate the general concept.

I assume by the nature of question that you’re relatively new to programming; I’d recommend some general books/material on program structures. It will take a bit of time now, but save you a lot of time in the long run as general programming concepts are necessary to code (almost) any game.

Good luck and have fun!

2 Likes

thank you so much

Thank you for setting it in [clay : tablet]
“Don’t ever call it manually”

how does the saying go:

just because you can, doesn’t mean you should. :upside_down_face: