Godot Version
v4.2.1 stable
Question
I have a general questions about code architecture in gdscript as it’s quite different than other languages I’ve used.
As a learning exercise, I want to write code that lets me get the amount of light at a given point. Here’s what I came up with
Base class LightProducer
class_name LightProducer
extends Node
static var group_name = "light_producer"
func _ready():
add_to_group(group_name)
func get_light_level_at_point(position: Vector3) -> float:
return 0.0
Specific child for OmniLightProducer
class_name OmniLightProducer
extends LightProducer
@export var omni_light: OmniLight3D
// other fields to impact light
func get_light_level_at_point(position: Vector3) -> float:
// Calculate the light level on the point based on the omni_range, etc
return result
Specific child for SpotLightProducer
class_name SpotLightProducer
extends LightProducer
@export var omni_light: OmniLight3D
// other fields to impact light
func get_light_level_at_point(position: Vector3) -> float:
// Calculate the light level on the point based on the spot_angle, etc
return result
Which I later intend to use as:
func get_light_level():
var total_light_amount = 0.0
// Ideally I reference the static group name to avoid spelling issues
for light_projector in get_tree().get_nodes_in_group(LightProjector.group_name):
total_light_amount += light_projector.get_light_level_at_point(self.global_transform.origin)
return total_light_amount
Performance concerns aside, this works. However:
- LightProducer, which should never be instantiated, appers in the node tree because it has class_name.
- On LightProducer, get_light_level_at_point shouldn’t really be called, but can’t be marked abstract/interface
- I’m unclear if making these nodes siblings of the respective lights and connecting with with
@export var omni_light: OmniLight3D
is the right approach
Finally I’m not sure this is the best approach overall give then above limitations and wondering how else I might approach this problem.