How can I make Player Input (from player script) effect my Area node (from area scripts)

Godot Version

4.6.3

Question

My player script is pilled with stuff inside of func _physics_process(delta: float) → void: I’m trying to make it cleaner by giving all my areas scripts, but idk how to make them interact with my player input.

In my player script I want to click E to pickup an item from my area script. (Tomato, noodles, etc.) then if E is clicked again it drops the sprite (it shows a sprite and disables it, it will also run a var that says whats in my players hands) . The issue is that I have no idea how to let my players input translate over to each area. I will have like 8 different areas each with slight differences, but all should listen for E…

The only thing I have atm is @onready Player = player thing. that works for reading when my player enters the area but not for “clicked E”

Thankful for any help! sorry for any confusion with how i worded this. I’ve been trying to understand this for 2 days now

each area and the player should have an array variable .

i think its helpful , but maybe this code needs to some correctation .

the tomato codes ( i think your stuff better to be areas ) :

func on_body_entered(body:Node2D): #body is your player
    body.stuff += self
    queue_free()

func _ready() → void:
    $CollisionShape2D.set_deffered ("disabled",true)
    await get_tree().create_timer(3).timeout
    $CollisionShape2D.set_deffered ("disabled",false)

codes for player :

var stuff : Array = [] #it will have the tomato and noodles .
var tomato = preload("the tomato location")
var IAmInArea : bool = false
func  _physics_process(delta: float) → void:
    if Input.is_action_just_released("E") and stuff != [] and !IAmInArea:
        stuff.remove(0) # removing one of tomatos or something
        # if that stuff was a tomato :
        var toma = tomato.instantiate()
        toma.global_position = global_position
        get_tree().get_root().add_child(toma) #this is puting stuff out . also you can add the node to other nodes as child. this code is trandition .
        

codes for areas :

var capasity : Array = []
var is_player_in : bool = false

func on_body_entered(body:Node2D):
    is_player_in = true
    body.IAmInArea = true

func on_body_exited(body:Node2D):
    is_player_in = false
    body.IAmInArea = false
func on_body_entered(body: Node2D):
    body.stuff += self
    queue_free()

You should really check if the body even IS the player. They said they had a reference to the player, so use it.

func on_body_entered(body: Node2D):
    if body == PLAYER_REFERENCE:
        body.stuff += self
        queue_free()

Just a suggestion. It could cause problems when, say, a beach ball falls into the area when you’re away from it, and you’re suddenly able to pick up some noodles from across the map…

Just a thought, though.

yeah you right :smiley: , i often use this

if body.is_in_group("player"):
    pass #code to execute

can you tell me if *unhandled_*input is okay for area? and player.get_node? I tried this and it works I just don’t know if it’s bad practice. I made area script for each area kinda like this.

extends Area2D

@onready var player = %Player

var player_inside: bool = false #is player inside of an area

func _on_body_entered(body: Node2D) -> void:
	if body == player:
		player_inside = true
func _on_body_exited(body: Node2D) -> void:
	if body == player:
		player_inside = false

func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("Interact") and player_inside:
		handle_tomato_pickup()

func handle_tomato_pickup() -> void:
	if player.is_holding == "nothing":
		player.is_holding = "tomato" #this is for my cutting board to read so it knows what to display
		player.get_node("TomatoSprite").visible = true
	elif player.is_holding == "tomato":
		player.is_holding = "nothing"
		player.get_node("TomatoSprite").visible = false

Still cleaning up my player script, but this is the important stuff

extends CharacterBody2D

@onready var Animated_sprite = $PlayerAnimation
var can_move = true #stops player movement if false
var is_holding = "nothing" #allows player to pickup or drop items
var tomato_cuts = 0 #for my cutting board to change the sprite with each click up to 3rd click
var fullness = "empty"#potFullness checks my foods condition ie just noodles, noodles and water
var plate = "clean" #does plate have noodles or sauce or both
var hands = "none"#playerHands This is a seperate var I made just for my pot to check for what kinda pot im holding. water, noodles, both (I will prob change this after i clean my script more.
@onready var label = $"../timer/Label" # for my timer that shows how long noddles need to cook
@onready var timer = $"../timer/Timer"
var SpagTime = false # just triggers endgame atm. if I finish making food, the game ends for now
var total_time_seconds = 10 #timers amount of time

Sorry idk how to change color of font to make it easier to read!

Ps. thank you for the reply. I will study what you posted! atm I don’t understand all of the code (because im new)