Triggering text when sprite enters Area2D
Asked Answered
R

7

0

Hi there!
Would anyone have any suggestions for a way to trigger text when the Sprite enters a specific Area2D? I've tried quite a few different things and none of them are working and triggering the text.
I'm a beginner, so any feedback would be appreciated.
Thank you!

Edit: to specify, by "triggering text" I refer to a label using AnimationPlayer. I've attached a screenshot of what I have so far by following a tutorial.

Retiarius answered 14/3 at 14:22 Comment(0)
S
1

Retiarius What do you mean by "triggering text"? Where do you want that text to appear? I guess, you could just make a label, enter the text you want to display and make the label invisible until the sprite enters the area. To detect that, you need collision detection. Sprites do not have collision shapes in Godot, so you probably need to make your character (or other moving object that you want to detect) a PhysicsBody2D of some sort, probably a CharacterBody2D. Then you attach a CollisionShape to it. Your Area2D has a body_entered signal, which will trigger, when any PhysicsBody2D enters it. (There is also an area_entered one, so you could also make your sprite another Area2D if that serves your use case more). You can find an introduction to Godot's 2D physics (yes, detecting collisions is part of physics) here:
https://docs.godotengine.org/en/stable/tutorials/physics/physics_introduction.html

Soni answered 14/3 at 14:42 Comment(0)
S
0

Retiarius Would anyone have any suggestions for a way to trigger text when the Sprite enters a specific Area2D? I've tried quite a few different things and none of them are working and triggering the text.

What would "text" be? You mean a label? Or something else? We need more context because there are many ways to do what you are asking.

1 - area2D is a physics object, it can only collide with another physics object. So you need an area2D or rigidbody or character on the sprite.
2 - the way everyone will suggest is signals. You go to signals and connect the signal of something entering the area2D to a script. Then in the function you check if the object that collided is the one you want. You can use groups, the has_method() function, physics layers, variables or metadata for this.
3 - then you need a reference to whatever you want to trigger in the script. You can use the $ symbol for local nodes, an export, or a path.
4 - finally you use the reference to do stuff.

Let's say you use a label:

var mylabel : Label = $Label1

func do_stuff() -> void: #call this from the function connected to the signal
     mylabel.text = "text"
Somerset answered 14/3 at 14:49 Comment(0)
R
0

Somerset Thank you for your feedback! Both my Sprite and the target space have an Area2D attached. I edited my original post to include a screenshot of what I have so far.

Retiarius answered 14/3 at 18:52 Comment(0)
S
0

Retiarius manualy connecting signals is too advanced for what you are trying to do.
With the area2D selected, click on the tab above the inspector and go to groups&signals. Select a signal and press connect, and select a node with script.
It will create a function in the script and connect it. Put your code in the function to do stuff. That's it.
You don't need to manualy connect signals when working with local nodes, use the signals tab instead.

Somerset answered 14/3 at 19:33 Comment(0)
C
1

If you do want to connect signals in the code:

You're using Godot 4 but using the Godot 3 syntax for connect().

In Godot 4, it would be:
$Area2D.area_entered.connect(_play_animation)

And the connect() inside the _play_animation function has no purpose and should be removed.

Coda answered 14/3 at 20:54 Comment(0)
R
0

Somerset Thanks for the help! So something like this? The text still doesn't show up when the Sprite enters the area - do I need to attach the signal to the Sprite's Area2D?

Retiarius answered 15/3 at 16:56 Comment(0)
S
0

Retiarius what's inarea? That's not changing to true in the script, maybe that's why it doesn't work.
You also have to set the area2D to collide with other areas? I don't remember exactly but check the options in area2D. It's also a good idea to put the area and whatever it has to collide with in corresponding physics layers.

Somerset answered 15/3 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.