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 :)

in Engine by (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

by (19 points)
edited by
0 votes

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.

by (381 points)
edited by

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.