Michael Teasdale
Michael Teasdale

Reputation: 113

How do I create a base "class" in Godot?

Godot 4.0

I am creating a 2D game that includes different light sources: candle, candelabra, lantern, torch, etc. Each light source is a StaticBody2D with a CollisionShape2D, a Sprite2D and a PointLight2D. They differ in their sprite texturess and in the color, intensity and range of their light.

I intend to have a variety of these objects in my game, and life would be easier for me if I could create a base light and have each type of light (Candle, Torch, Lantern) as its own independent Scene built off of this base.

If I were doing this in OOP, I'd create a base Light class and derive Candle, Lantern and Torch from it, but I'm not sure how I'd do this in Godot since each Scene is a Node tree with child Nodes instead of a class with attributes and I want to derive from the whole tree, not just one node.

I've read through the docs, but either I'm missing something or I'm just not thinking about it right because I haven't been able to puzzle out a way to do this.

I'd rather not just copy my base scene into each light scene.

Any thoughts?

Upvotes: 2

Views: 4626

Answers (2)

Michael Teasdale
Michael Teasdale

Reputation: 113

@zett42's mention of @export got me thinking. Since I only have 3 variations of the light, and they differ only in the values of a few attributes, I decided to export an enum and set the value of my attributes based on that enum. This way, I need only 1 light object and maintain the simplicity of setting only one value for each instance.

enum Light_Type {CANDLE, CANDELABRA, LANTERN}
@export var type: Light_Type

@onready var sprite = $Sprite2D
@onready var light_source = $PointLight2D

const light_settings = [{"texture": "res://objects/lights/candle.png",
                        "color": Color8(255, 254, 176, 255), "size": 1},
                        {"texture": "res://objects/lights/candelabra.png", 
                        "color": Color8(255, 100, 100, 255), "size": 1.5},
                        {"texture": "res://objects/lights/lantern.png", 
                        "color": Color8(100, 255, 100, 255), "size": 2}]


func _ready():
    sprite.texture = load(light_settings[type]["texture"])
    light_source.texture_scale = light_settings[type]["size"]
    light_source.color = light_settings[type]["color"]

Upvotes: 1

Theraot
Theraot

Reputation: 40295

Godot supports good old OOP classes and inheritance. However, an scene is not a class. Thankfully, Godot also supports both scene inheritance and scene composition.

You are probably familiar with scene composition already: in an scene you can add an instance of another scene. If you enable "editable children" in the context menu of Scene, you can access and modify the children of the instantiated scene.

For scene inheritance, select the saved scene in FileSystem and from the context menu select "New Inherited Scene". Which will create a new scene that derives from the pre-existing one, and you add modifications on top.

Upvotes: 2

Related Questions