Callable Function Error

v4 1.3 Godot Version

So I recently started using Godot and I have fun coding and testing it out but I have had problem with my code right now. I followed a tutorial for interactable objects but I run into a error whenever I launch the scene with said interactable object. Here is the code for the interactable object, interaction manager, and interaction area code.

Interactable object:
extends StaticBody2D

@onready var interaction_area = $InteractionArea
@onready var sprite = $AnimatedSprite2D

func _ready():
interaction_area.interact = Callable(self, “_toggle_chest”)

func _toggle_lamp():
$AnimatedSprite2D.play(“chest”)

Interaction Manager:
extends Node2D

@onready var player = get_tree().get_first_node_in_group(“player”)
@onready var label = $Label

const base_text = "[E] to "

var active_areas =
var can_interact = true

func register_area(area: InteractionArea):
active_areas.push_back(area)

func unregister_area(area: InteractionArea):
var index = active_areas.find(area)
if index != -1:
active_areas.remove_at(index)

func _process(delta):
if active_areas.size() > 0 && can_interact:
active_areas.sort_custom(_sort_by_distance_to_player)
label.global_position = active_areas[0].action_name
label.global_position = active_areas[0].global_position
label.global_position.y -= 36
label.global_position.x -= label.size.x / 2
label.show()
else:
label.hide()

func _sort_by_distance_to_player(area1, area2):
var area1_to_player = player.global_position.distance_to(area1.global_position)
var area2_to_player = player.global_position.distance_to(area2.global_position)
return area1_to_player < area2_to_player

func _input(event):
if event.is_action_pressed(“intereact”) && can_interact:
if active_areas.size() > 0:
can_interact = false
label.hide()

		await active_areas[0].interact.call()
		
		can_interact = true

Interaction Area:
extends Area2D
class_name InteractionArea

@export var action_name: String = “Interact”

var interact: Callable = func():
pass

func _on_body_entered(body):
InteractionManager.register_area(self)

func _on_body_exited(body):
InteractionManager.unregister_area(self)

Sorry if this is an easy fix to be made and here is the tutorial if anyone is wondering. Thanks for the help.

the video is unavailable

does this work?

I’m sorry, but what is the error? I didn’t see the description of the problem in your post.

1 Like

image

These are the errors I run into whenever I run the project


second reply cause i couldn’t put both in the same post

can you show your scene tree?

image
right here

To help yourself and others, use three backticks, the button to the left of your 1 key on either side of your code to put it in a block

func like_this():
    pass

image

You’re trying to create a reference to _toggle_chest function but this object has no such method, it only has _toggle_lamp.

Btw, try to avoid referring to method names inside strings like a plague. If you use another constructor Callable offers, you’ll catch this problem right away at code time, like so:

interaction_area.interact = Callable(_toggle_lamp)

Strony typing saves lives!

1 Like

Thank you but I still am running into the same error when I replaced the line of code with the one you suggested.

extends StaticBody2D

@onready var interaction_area = $InteractionArea
@onready var sprite = $AnimatedSprite2D

func _ready():
	interaction_area.interact = Callable(_toggle_lamp)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _toggle_lamp():
	$AnimatedSprite2D.play("chest")

Sorry for the long wait for the reply I been away from my pc from Winter Break so wasn’t able to actually test it out.

where is the InteractionArea named Node here?

as z said above, in line:

@onready var interaction_area = $InteractionArea

The “$InteractionArea” part is wrong. You must have moved InteractionArea in the tree without updating this path. You can either type “$./” and find Interactionarea in the list that pops up, or delete the line and click on InteractionArea in scene tree and drag it into code while CTRL is pressed (CMD on Mac), release mouse button while still pressing CTRL, and it will write @onready line for you with proper path.