Help trying to connect Area2D _on_area_2d_area_entered func to custom class

Godot Version

4.7

Question

Hi I’m newer to Godot and I was trying to make a clickable item that would show text based off a description put in an Array. But the way I’m trying to connect the Area2D to the items doesn’t let me call the _on_area_2d_area_entered/exited. I was wondering if there was a way to make this work or just a better way to call this function?

class_name Item
extends Node2D


@export var area: Area2D
@export var item_sprite: Sprite2D
@export var item_description: Array[String] = [" "]
var selected: bool = false
var counter: int
var event: InputEvent


func _ready() -> void:
	area._on_area_2d_area_entered.connect(_on_area_2d_area_entered)
	area._on_area_2d_area_exited.connect(_on_area_2d_area_exited)

func _on_area_2d_area_entered(area: Area2D) -> void:
	selected = true

func _on_area_2d_area_exited(area: Area2D) -> void:
	selected = false



func _process(delta: float) -> void:
	if selected:
		if event.is_action_pressed("interact"):
			if item_description.size() > 0:
				if counter > item_description.size() - 1:
					counter = item_description.size() - 1
					print(item_description[counter])
				else:
					counter += 1
					print(item_description[counter])

This should be

func _ready() -> void:
	area.area_entered.connect(_on_area_2d_area_entered)
	area.area_exited.connect(_on_area_2d_area_exited)

You need to call connect() on the signal, not on the handling function:

node.signal_name.connect(handling_function)

In your case that would be:

area.area_entered.connect(_on_area_2d_area_entered)
area.area_exited.connect(_on_area_2d_area_exited)

To find the list of signals and their correct names for a node, look at the Signals section in node’s class reference page. For Area2D that would be here

Also don’t forget to assign the actual Area2D node to your area variable in the inspector.

So I made the change and made sure everything was assigned, even added in a print(“selected”) function to see if it was detecting the area being entered but it still doesn’t seem to be detecting it for some reason?

Here’s the code now

class_name Item
extends Node2D


@export var area: Area2D
@export var item_sprite: Sprite2D
@export var item_description: Array[String] = [" "]
var selected: bool = false
var counter: int
var event: InputEvent


func _ready() -> void:
	area.area_entered.connect(_on_area_2d_area_entered)
	area.area_exited.connect(_on_area_2d_area_exited)

func _on_area_2d_area_entered(area: Area2D) -> void:
	print("selected")
	selected = true

func _on_area_2d_area_exited(area: Area2D) -> void:
	print("not selected")
	selected = false



func _process(delta: float) -> void:
	if selected:
		if event.is_action_pressed("interact"):
			if item_description.size() > 0:
				if counter > item_description.size() - 1:
					counter = item_description.size() - 1
					print(item_description[counter])
				else:
					counter += 1
					print(item_description[counter])

Post your scene tree and check if there are any errors in the output reported by the engine after you start the project. Also make sure that your collision layers and masks are set up properly.

So this is the separate item scene

And here’s the game scene tree (I have to post these in multiple comments since my account is new it says)

These are the errors that pop up when I load the game

Which node runs the script you posted and which Area2D node is assigned to area?

Well I was trying to make it a class so that all the nodes that extend it would run the script so the ones named “Crystal” and “First Display” and they each have their own area2D nodes assigned to them

Enable Debug > Visible Collision Shapes and visually check that the areas are indeed colliding.

I mean I think they’re colliding here. Right?

Are your collision layers properly set up and monitoring/monitorable flags enabled?

I believe so all their collisions and monitoring/monitorable flags look like this

Print this in _ready():

print(self.get_path())
print(area.get_path())

What’s the printout?

So it looks like the problem might be that the _ready(): function isn’t working because there was no printout?

Check to which nodes is the script attached.

Ok so I checked and it seems they did have the wrong scripts attached. Though now that I have the correct script attached I get this error when I click them.

image

That’s a different problem entirely. Check the array size before indexing it, so your index is not out of array bounds.

How would I go about doing that?