Calling a function of an instantiated scene

Godot Version

4.1.3

Question

I’m trying to instantiate a Scene and call a function (setColor) from a script (Field.gd) attached to the root node of the scene. Here’s a simplified version of my code:

extends Control

var field: PackedScene = load("res://GUI/field.tscn")

# Called when the node enters the scene tree for the first time.
func _ready():
    if field == null:
        print("Failed to load PackedScene")
        return

    var fieldInstance = field.instantiate()
    fieldInstance.setColor(Color.BLACK)  # Call setColor function

The setColor function is defined in Field.gd like this:

extends Control

func setColor(color):
    # Color setting logic here

When I run the code, I get the error “Invalid call. Nonexistent function ‘setColor’ in base ‘Control’”. This suggests that the setColor function is not found in fieldInstance.

I’ve checked that the Field.gd script is attached to the root node of the field.tscn scene, and that the function name matches exactly. I’ve also tried printing the type of fieldInstance, and it prints 24 and True for fieldInstance is Control.

I’m using Godot 4.1.3. Any help would be appreciated.

Can you add a screenshot of res://GUI/field.tscn?

Side note: have you tried restarting Godot editor?

Yes I tired to restart it more than once

Super weird.

Does fieldInstance.has_method("setColor") return true?

fieldInstance.has_method("setColor")
returns false

What’s the text content of res://GUI/field.tscn?

[gd_scene load_steps=2 format=3 uid="uid://de0au7p8npabc"]

[ext_resource type="Script" path="res://GUI/Field.gd" id="1_dglbo"]

[node name="Field" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_dglbo")

[node name="ColorRect" type="ColorRect" parent="."]
layout_mode = 0
offset_right = 40.0
offset_bottom = 40.0

That’s bonkers, I’m out of ideas.

Can you try replicating this issue in a brand new project?

You need to add the field instance as a child to the current scene

var fieldInstance = field.instantiate()
    fieldInstance.setColor(Color.BLACK)  # Call setColor function
add_child(fieldInstance)

A shot in the dark here.
I see you have the _init() function declared, but you didn’t provide a default value for color. Try giving it a default value. Eg:

func _init(color = Color.WHITE):
4 Likes

Thank you :+1:

It should’ve printed a warning for this in the bottom dock, right?
Because that means that Godot couldn’t construct an instance properly and just skipped the attached script.

2 Likes

I’ve helped a few others address this problem in the Discord as well and they never got an error either. I do agree that it should throw an error, but I’ve yet to see it happen.

Randomly stumbled into this thread and found a solution to a problem I’ve also had a few times lately and nobody seemed to know the answer to. Can confirm I never received an error, it was most mysterious. Thank you for this!

1 Like

Tried creating a minimal reproduction project, but it does show an error for me. So it’s not completely silent.

Godot v4.2.stable.official [46dc27791]

Here’s the project for anyone interested: https://filetransfer.io/data-package/W6HdEeee

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