0 votes

Quick question from a Godot newbie. I'm having trouble calling a method from a GDscript custom class.

Tile.gd is a simple class I made with just a few getter methods, which is loaded by gameboard.gd.

However, I've been trying to access any of the class properties in a new Tile object, but I keep receiving a Nonexistent function error from the debugger.

Please take a look at my script files:

Tile.gd

const TileScene = preload("res://scenes/entities/Tile.tscn")

enum TYPES {NULL, GRASS, WATER}

class Tile:
    var type
    var isWall
    var sceneInstance

    func _init(type = TYPES.NULL, isWall = false):
        self.type = type
        self.isWall = isWall
        self.sceneInstance = TileScene.instance()
        self.sceneInstance.get_node("TileSet").set_frame(self.type)

    func get_type():
        return self.type

    func isWall():
        return self.isWall

    func get_scene():
        return self.sceneInstance

gameboard.gd

extends TileMap

var board = null
onready var Tile = preload("res://src/Tile.gd")

func _ready():
    var a = Tile.new()
    print(a.isWall())       # <<< "Nonexistent function" error
    print(a.get_scene())    # <<< Also a "Nonexistent function" error
    print("ready!")

After running the project, the debugger displays: Invalid call. Nonexistent function 'get_scene' in base 'Reference (Tile.gd)'.

I've been scratching my head for a while trying to figure out why I'm unable to call any of the class methods.

If anyone could help guide me in the right direction, I'd really appreciate it.

in Engine by (15 points)

1 Answer

+1 vote
Best answer

You're creating an instance of the script, not the class Tile. Try replacing

var a = Tile.new()

with:

var a = Tile.Tile.new()
by (1,544 points)
selected by

Ah, you are absolutely correct. That was kind of a "duh" moment on my part. I didn't realize I was creating a new script instance. That makes a lot of sense, otherwise, how else would I call my enum than "Tile.TYPES". Thanks for the help!

Corrected code:

gameboard.gd

extends TileMap

var board = null
onready var Tile = preload("res://src/Tile.gd")

func _ready():
    var tile = Tile.Tile.new()
    print(tile.get_scene())    # Output: [Node2D:570]
    print("ready!")
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.
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 webmaster@godotengine.org with your username.