Learning extending and inherieting a node?

Godot Version

Godot_v4.2.1-stable

Question

Simple question at first~
I was doing my first gamejam, and I hit a concept of trying to build a base level to inherit or extend from.
Every level I create or generate from should have a base ruleset.

This made two questions for me:
Right now, we’re building our levels by hand. I feel like I should have a way to extend or build from a base saved scene/base node? Every level scene we create bases off of some BaseLevel.

Second part, I’d assume programmatically, we can do something similar with some LevelBuilder object? Instance a base level, modify some params, and spawn in?

Apologies if this is a basic question, I’m 101 to GODOT right now, first time using a game engine since Unity about 9 years ago.

depending how you will extends from it
if it’s from extends Node
then you can put something like

base_level.gd :

class_name BaseLevel
extends Node

#put base level's parameters and functions here
var level_name:String=""
var level_id:int=-1
func set_default_params(_level_name,_level_id): 
	level_name=_level_name
	level_id=_level_id

you can extend this “BaseLevel” on a new script you wanted to use with

example:
level.gd :
extends BaseLevel
Instead of extending Node. Also in Editor when adding new node, there should be a new “BaseLevel” node type to be use to create new node now.

You can use the extended function with super syntax
example:
super.set_default_params("LEVEL ONE",1)

when you spawn/instantiate the node who extends this Base Level script, it basically it already have exact same variables and methods can be used as an inherited

if you mean you just want it to be a script of object, use extends RefCounted when creating the BaseLevel script

class_name BaseLevel
extends RefCounted

var level_name:String=""
var level_id:int=-1
func _init(_level_name,_level_id): 
	level_name=_level_name
	level_id=_level_id

when a level builder trying to buid an object of BaseLevel, you can just call

Declare the variable where it will store the object and accessible to whole script:

var the_base_level:BaseLevel

to create the object use:

the_base_level=BaseLevel.new("LEVEL ONE", 1)

then you can use the created “object” of base level like:

var level_name=the_base_level.level_name
2 Likes

Heartbeast has a current tutorial series on YouTube where he shows composition and inheritance, mainly with different types of enemies.