I'm trying to make a chest for the player to interact with to gain something which was inside of the chest. I watched brackeys video on making first game specificly the coin collision part but that part doesn't go over checking if the player is both inside the collision and has pressed the interact button. Would love some help with this because I perfer real human commucation than videos or docs. Thank you
#Check for collision and released interact button
1 messages · Page 1 of 1 (latest)
Just add both conditions to your check.
If the conditions are read from different functions, store one of them in a variable so you use both in the same function.
I don't understand
U can simply do this to an Are3D object. (Also works for Area2d)
func _on_area_entered(_area : Area3D):
set_process(true)
func _on_area_exited(_area : Area3D):
set_process(false)
An inside _process use
if Input.is_action_just_pressed("interact") then your logic to give player item
I agree with robotdeha but I love event driven input processing and also nicely formatted answers. So I would slightly modify:
#// Interactable.gd
#// When a player -- assumed CharacterBody2D? -- wanders closely enough, listens for their keypress.//#
#// These weird comments are to fool the discord formatter. //#
extends Area2D
@export action:= &"ui_confirm"
signal time_to_do_the_thing()
func _init():
body_entered.connect(
set_process_unhandled_key_input
.bind(true)
)
body_exited.connect(
set_process_unhandled_key_input
.bind(false)
)
#// Assume we're waiting for player arrival initially//#
set_process_unhandled_key_input(false)
func _unhandled_key_input(e: InputEvent):
if e.is_action(action):
time_to_do_the_thing.emit()
(this is equivalent to their solution -- I do my input handling in an event instead of a process function; if you don't like that, then hopefully you can see how to switch from one way to the other, since both are valid!)
I don't undersatnd most of this. pretty please expain
also, sorry for replying to things so late
also also, if I were to say,
var player_in_collision = false
func _on_body_entered(body):
player_in_collision = true
func _physics_process(delta):
if player_in_collision and Input.is_action_just_released("interact"):
print("yeah it worked")```
would that work?
Re: your code sample: it doesn't set player_in_collision=false when the player leaves, so once you can interact you can always interact. It works other than that.though!
My sol'n: in addition to magic builtin callback methods like _process and _physics_process which you already know about, Godot also has magic builtin callback methods that are called at different times for different purposes. I'm using that in my solution; _unhandled_key_input is called when there is a key press which other things in the game didn't consume (other things like: a popup menu overlay)
You can turn on and off most of the magic builtin methods; I'm taking advantage of that to build the feature you wanted. When the player is far away, this object isn't handling input at all.
Also, I'm connecting the signals in _init, the objects constructor; this is automatically called very early in object lifecycle (before even _ready)
Read it line by line and ask questions if I missed anything else!
for some reason, when I run the program I go inside the collision, press the interaction button but nothing get's printed. I think something's wrong with my output..?
It is more likely that some connection or setup of the scene is at fault