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?
@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"]