cybereality
That's a good idea in general, and I've been considering using something like that for this project. Still I don't think an event or command queue quite solves my need here. I'll just go ahead and post my script I'm working on right now as an example (just a prototype while I'm exploring things)
class_name Player
extends CharacterBody2D
@export var _movement_buffer__path: NodePath
@onready var _movement_buffer:Object = get_node(_movement_buffer__path)
@export var speed := 300.0
@export var brake := 0.2
func _physics_process(delta: float) -> void:
var input_movement := Vector3.ZERO
var input_received := false
var received_input:int
if is_instance_valid(_movement_buffer) && _movement_buffer.has_method("front"):
received_input = _movement_buffer.front()
else:
push_error("_movement_buffer missing or missing function front")
if Config.INPUT_DIRECTIONS_MAP.has(received_input):
input_movement = Config.DIRECTION_VECTORS[Config.INPUT_DIRECTIONS_MAP[received_input]]
input_received = true
elif received_input != Config.Inputs.NONE:
push_error("Expected directional input. Received %s" % received_input)
if input_received:
velocity = Hex.cube_to_offset(input_movement * speed)
else:
velocity = velocity.move_toward(Vector2.ZERO, (speed / brake) * delta)
var _collided = move_and_slide()
So you see, all I'm really doing is calling a getter-type function, front
, on my child object. Storing that as a NodePath means I don't have to worry about renaming or moving the child, even out of the local hierarchy, and I can replace the script with whatever as long as it has front
which returns a Vector3. The unfortunate part is that I have to ensure that myself, both with the error-checking and with being careful to assign a valid Node.
I'm calling this in _physics_process, so I need a value right away. I unfortunately don't have time to wait for a response from a global event which might not have a listener, and throwing that event would require excessive data. I would need to do something like pass Self and a callback and have the listener check if it's a child before calling the callback and... it'd be messy.
If gdscript had interfaces like C# does, that would be ideal, but unfortunately it does not and will not. And I know I can work in C# with Godot, but even with 4.x C# just isn't quite there for me, plus I'd lose out on things that make Godot special with gdscript.
Edit: Also I don't think C# interface variables can be exported, can they?