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])
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.
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
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.