Help with inputs

Godot Version

Godot_V.4.3

Question

I have a area2d with a signal body_entered,and i added in the Project/Project Settings/Input map i added “Use” and i want that “Use” can be used only in the Area2d.Please help

You can attach a script to area2d:

func _input(input_event):
    if Input.is_action_just_pressed("Use"):
        if player != null: # make sure to set the player when he enters the area and set it to null when he leaves the area
            player.use() # The method you want to call

It says invald call.Nonexisten function use in base 'CharacterBody2D (player.gd).
Am i supposed to make own func use or what?

1 Like

Yeah you have to make your own function, you can replace player.use() with any script.

1 Like

i still dont get it.I wrote the code and added func that just prints Hello, but it prints even if i am not in the area2D. Sorry for dont understanding something i am a begginer

Can you paste your script as well? Maybe you can use set_process_input(true) when they enter the area and false when they exit, also on _ready()


This is the code.
I dont undrestand this “# make sure to set the player when he enters the area”,because this is my first time using null

I see, make sure to paste scripts instead of screen shots; though your scene tree has some warnings with the S1 and S2 character bodies, what are those about?

The script is three parts, body_entered as you’ve connected, _input, and the player.

Since the Area2D gives us the colliding body we can deduce the player by collision. And store this later, when we need inputs.

var player: CharacterBody2D = null
func _on_body_entered(body: Node2D) -> void:
    if body.name == "Player":
        player = body

func _on_body_exited(body: Node2D) -> void:
    if body == player:
        player = null

Now we only want to recieve inputs when the player is in the area, we could make use of set_process_input or check for a player value within our _input function.

func _unhandled_input(event: InputEvent) -> void:
    if player != null:
        if event.is_action_pressed("Use"):
            player.idk()

That works just fine,but i actually spent some more time and get it to work on my own, take a look:
@onready var player: CharacterBody2D = $“…/…/…/Player”
var pok = false

func _process(delta: float) → void:
idk()

func idk():
if pok == true:
if Input.is_action_just_pressed(“Use”):
Lever.flip_h = true

func _on_body_entered(body: Node2D) → void:
pok = true

func _on_body_exited(body: Node2D) → void:
pok = false

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.