How to inherit functions from scripts

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Suicuiune

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
    update()

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

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

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

tool
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 :slight_smile:

:bust_in_silhouette: Reply From: MaaaxiKing

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.

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. :slight_smile:

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

inheritance documentation

Suicuiune | 2020-06-04 19:20

:bust_in_silhouette: Reply From: Suicuiune

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