I am making a small game where you play as a bear trying to remove expired food from your house before it explodes. I am trying to make it so that if you are infront of the fridge and press the action button then the fridge will open. I am not very experienced in Godot so I would like to know if it is possible to put a process() function inside a *_*body_entered function like in the script shown below:
func _on_fridge_body_entered:
if Input.is_action_just_pressed("action"):
func _process(_delta):
if (body.name == "PlayerSprite"):
$Fridge/FridgeSprite.play("Open")
else:
$Fridge/FridgeSprite.play("Close")
Thank you. I do not have my computer with me right now to test it but I will test it when I get back home. Now that the mention set_process I think I recall having used it in the past, guess I just forgot. Thanks!
the body_entered signal only triggers this function once, at the exact moment the area/body is entered. You probably don’t want _process at all, instead tracking if the player is within the area and using that as a condition in your input.
This sample tracks if a body named “PlayerSprite” is within the area, and plays one of the animations depending on if the body is nearby, though I think you want your animation to be toggled, not based on proximity?
var nearby_player: Node2D = null
func _on_fridge_body_entered(body: Node2D) -> void:
if body.name == "PlayerSprite":
nearby_player = body
func _on_fridge_body_exited(body: Node2D) -> void:
if body == nearby_player:
nearby_player = null
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("action"):
if nearby_player != null:
$Fridge/FridgeSprite.play("Open")
else:
$Fridge/FridgeSprite.play("Close")
I know I am answering my own question here, but (at least for my situation) this seemed to work the best. I was playing around and I realized that if you put _delta into the end of the on body entered it turns it into a function. func _on_fridge_body_entered(_delta):$Fridge/FridgeSprite.play(“Open”) func _on_fridge_body_exited(_delta):$Fridge/FridgeSprite.play(“Close”)
your _on_fridge_body_entered was already a function, that didn’t change by adding _delta. In fact _delta has no special meaning, it would work the same with _body (which also has no special meaning, you could name it _floorp, same function), while the code’s function doesn’t change the meaning makes a lot more sense as body_enterednever gives you a frame-delta value, it will always give a physics body.
Your code plays the open animation once something enters the area, and plays close when something exits, no interaction or “action” action is used. Probably nicer from a player point of view.
So, in other words, on fridge body entered was already a process function and the animation would have worked? Besides, the animation works perfectly now, so I’m happy, and I’ve learned a lot
not a “process” function, a normal function connected to the signal body_entered, I wanted to clarify that the name of the variable _delta didn’t matter, and may be a misnomer.
_process is an override function, it along with a few others are somewhat special. In the editor you will see a blue arrow beside override functions, in the docs they will be marked “virtual”. Override functions are called automatically at certain times, much like connected functions are automatically called when a signal emits.
So why did my fridge only open when I added _delta? At least that is what happened when I tried. When I get back I could open up Godot to check but that is what I remember happening. Not questioning your knowledge at all, just curious about how Godot works.
without exactly one argument the function isn’t a valid override, you could use any name, body: Node2D is normally filled in, but without an argument the function signature isn’t the same.
set_process only affects the current script, you can apply it to other scripts with dot notation, other_object.set_process(false)
You can use set_physics_process to enable/disable _physics_process, similarly set_process_input and set_process_unhandled_input to enable/disable _input and _unhandled_input.