Inheritance and Arrays?

Godot Version

Godot 4.4

Question

If I put an Array in a class and fill it in the class, will my subclasses inherit the full array? , just the array?

Depends on how you fill it.

Extended classes will have the same variables as their inherited script, if default-initialized then the same defaults too.

1 Like

So lets say I fill the array with preloaded scenes?
Would all my scenes, if all the variables are previously stated in the base class, be inherited in the extended class?

Probably, again depends on what you mean by “fill”. If you are using defaults then yes, other methods like inside the _ready function or through @export may not hold true.

# a default initialized array
var my_array: Array[PackedScene] = [ preload("res://scene1.tscn"), preload("res://scene2.tscn") ]

okay so example would look like this

Extends Node
class_name MainClass

# pre loaded scenes
@onready var sc_1 = preload("res://scene1.tscn")
@onready var sc_2 = preload("res://scene2.tscn")

# array holding the scenes
@onready var filled_array: Array = [sc_1, sc_2,]

Now my next script would Extend MainClass will I be able to directly reference the filled_array

replace vars with const since you use preload(), and it would be inheritable

even available without instancing script (but resources will be loaded if script mentioned anywhere)

2 Likes

Thank you!