0 votes

I have a simple class hierarchy:

class Actor:
var x
var y
var z

func _init():
    x = 0
    y = 0
    z = 0

class Player extends Actor:
var gender

func _init():
    gender = 0

At the moment this is in the main scene for my project. What I want to do is put actor in Actor.gd and player in Player.gd, what I can't figure out is how to?

I can put the class Actor in Actor.gd but it falls apart when I put Player in Player.gd. Even if Player.gd starts with

extends "res://actor.gd"

I get an error about unknown class Actor. Is this just not possible with GDscript?

asked Apr 9 in Engine by imekon (229 points)

1 Answer

0 votes
Best answer

Actor.gd

extends Node

var x
var y
var z

func _init():
    x = 0
    y = 0
    z = 0

Player.gd

extends "res://Actor.gd"

var gender

func _init():
    gender = 0

You might have a different path to your Actor.gd file.
Hint: you can drag a script file from FileSystem window into code editor and it will insert its path.
If both files are in the same folder you can use relative file path like this:

extends "./Actor.gd"
answered Apr 9 by hilfazer (2,112 points)
selected Apr 9 by imekon

Oh wow, it's that simple!

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.