Topic was automatically imported from the old Question2Answer platform.
Asked By
AaronWizard
I have an Actor class and a Controller class. Each actor has a controller which may either be the player or an AI.
I’ve started with this:
# actor.gd
# Scene has a child Controller node
extends Node2D
class_name Actor
onready var controller : Controller = $Controller
func _process(delta: float) -> void:
controller.run(self, delta)
----
# controller.gd
# Subclassed for different behaviour. i.e. Player control and different AI types
extends Node
class_name Controller
func run(actor: Actor, delta: float) -> void:
...
But this causes a cyclic dependency error.
How else should I arrange this?
(I’ve considered that making controllers the parents of actors is an option, but that wouldn’t be feasible for me. It’s the actors that get placed in the level, and I’d want to be able to take an actor and change what controller it has.)
After discussing this elsewhere, the conclusion is to not specify types where they’d cause cyclic dependencies.
# controller.gd
# Same as before
class_name Controller
func run(actor: Actor, delta: float) -> void:
...
---
# actor.gd
extends Node2D
class_name Actor
onready var controller = $Controller # Type not specified
func _process(delta: float) -> void:
controller.run(self, delta)
This is actually similar to what’s done in the GDQuest OpenRPG project. In combat scenes there are Battlers that have BattlerAIs. Battlers have a variable for their BattlerAI but they don’t specify its type.