0 votes

So i have a kinematicbody as my player
When the player goes near a door, a button shows up what key he needs to press to go through that door.
So i made an area2d node around the door. When the player enters that area the image of the button shows up. I did that well
Now i want to check if the interact key is pressed while the player's inside that area
I tried with the signal onareabodyenter and i tried to print something when the player enters that area and pressed the interact key:

func _on_area_body_enter(body):
     if body.get_name() == 'Player':
     if Input.is_action_pressed('interact')
      print('interact')

The 'interact' key is set to be 'E' key.
That doesn't work.
Also, when the interact button is pressed i want to change the scene. I need to initialize the scene first? How?
(Im new to Godot)...
Help...

in Engine by (397 points)
edited by

2 Answers

+1 vote
Best answer

That can be done in many ways.

One could be to give a reference of the door to the player when body_enter, then make the "open door" or "action" input always work regardless the referenced object so it can trigger anything, not just doors.

When the player tries to trigger something, if null can make a sound or ignore it, if there is something, try to execute a common function on these objects (you can do a group check).

Then null the reference when body_exit.


How to change scenes will depend on your design, if you want to cut a branch of your scene or change all, there is a demo about scene change that uses an autoloaded script but can be done directly from the tree.

Check docs to understand how the scene tree works:
http://docs.godotengine.org/en/stable/learning/step_by_step/scene_tree.html?highlight=change_scene

by (7,818 points)
selected by

Im going to try that. That's a good idea! TY

Can you give me a short example? I can't figure it out

On door body_enter check if the body can interact with doors (is in a group that does that), then do something like body.set_active_object where a reference to the door is passed.

On player (or whatever body) action to open door, check if that active object is not null and execute it's function to activate it like active_object.activate() (the function to atually open the door).


Other way to make it is adding a "interactive detection area/raycast" to the player, and make the player detect the objects directly, but depending on the design this can be straightforward or more complicated.

0 votes

I usedifstatements with a boolean Area2D.overlaps_body(playernode) instead of _on_area_body_enter() function

by (16 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories