0 votes

My script can't use functions defined in the extended class.

I have a script "res://scripts/tile.gd" with the code

extends Node2D
class_name tile

var cellPos: Vector2
var type: String
export var texture: Texture setget setTexture

func setTexture(newTexture):
    self.texture = newTexture

func update():
    $Sprite.texture = self.texture

func _init():
    var sprite = Sprite.new()

and an extending script "res://scripts/floor.gd" with

extends tile

func _ready():
    update()  # error(6,1): The method "update" isn't declared in the current class.

It seems that for some reason floor doesn't inherit any functions or variables at all since print(type) also doesn't work.

I have tested around a bit and believe it has something to do with the setget setTexture or that functions are called from other functions.

I hope someone has also encountered this and knows how to fix it.
Thanks in advance for any feedback :)

asked Jun 4 in Engine by Suicuiune (19 points)

2 Answers

0 votes
Best answer

TLDR: don't use self.variable_name in a function specified by setget

Thanks to a suggestion by MaaaxIKIng I got a different error that led me to a solution.
After their change there was a Script isn't fully loaded error. As I suspected it was an error with the setget setTexture method. A setget method is triggered when changing a variable from outside the owner script or by using self.variable. As such the tile.gd script couldn't be loaded externally because the setTexture method just called itself into recursion oblivion.

setget documentation

answered Jun 4 by Suicuiune (19 points)
edited Jun 4 by Suicuiune
+1 vote

extends is used with classes or paths, not with class names.
Remove class_name tile and do extends "res://scripts/tile.gd" instead of extends tile.

answered Jun 4 by MaaaxiKing (291 points)
edited Jun 4 by MaaaxiKing

Thanks for your answer!

It wasn't quite the solution but sent me down another rabbit hole by showing a different error that led me to the solution. :)

Also as far as I know there should be no difference between extending from a path or class_name

inheritance documentation

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.