|Topic was automatically imported from the old Question2Answer platform.
I’d like to create a sign in my 2D game which, when you interact with it, shows a display with text in it. How can I do it? specifically I’m not creating an RPG but just platform 2D game. Thank you.
You will need:
Sprite node for rendering the sign;
Area2D node for detecting if the player is over it with
CollisionPolygon2D inside to define the shape of the area;
Label node for displaying the text (turn off visibility in the editor to not show the text by default).
First, you will need to detect if the player is over the sign and is ready to interact with it.
Create a script in the parent node (if you haven’t done it yet);
body_exited signals from
Area2D to your parent node;
Define a boolean variable that will let you know if the player is over the sign;
Then, in the code of the parent node in the
Area2D_body_entered method check, if
body parameter is the player, and if it is, set the “player over sign” variable to
true(the player is over the sign);
The same for the
Area2D_body_exited method, but set it to
false (the player isn’t over the sign).
Now you can check if the player is over your sign.
Then, you need to make the player able to interact with the sign.
Project > Project Settings > Input Map;
ui_interact action and add a key, for example,
When the player will press that key, he will be able to interact with the sign.
_input(event) method to the node script.
To check if the interaction key is pressed try
is_action_just_pressed("ui_interact") or your action name instead of “ui_interact”.
And if the player is over the sign (the “player over sign” variable) at the same time, you need to show the text of the sign.
To do this, you can simply call
show() method on the
And don’t forget that the
Label node needs to hide when the player moves out from the interactive zone, so at the end of
Area2D_body_exited method hide the
Label by calling
hide() on it.
It is a basic interactive text sign, so you can add some more abilities to it, like when the player stands over the sign, above his head pops up a symbol (
E) that he’ll need to press to interact with the sign.