Putting classes in separate files

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

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?

:bust_in_silhouette: Reply From: hilfazer

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"

Oh wow, it’s that simple!

imekon | 2020-04-09 08:58