I am porting an existing project from Python, which was using multiple inheritance. I am now looking for ways to port this to Unity’s composition architecture. The game is a space ship game with ships roaming in a common environment.
This is the rough structure in my Python project:
There’s a base class for all ships, which handles the model, weapon firing etc.:
class Ship:
position_ship()
There’s a general Finite State Machine:
class FSM:
request()
There’s a common FSM that serves as a base for all AI-driven ships:
class AIShipFSM (Ship, FSM):
Update()
enterState()
There are classes for different AI types, like traders, warships … and the player, overriding some state methods of the common AIShipFSM, if needed:
class WarshipFSM (Ship, AIShipFSM):
enterState()
class TraderFSM (Ship, AIShipFSM):
enterState()
class PlayerFSM (Ship, FSM):
enterState()
Here’s the environment, instantiating the ship FSM classes:
class Environment:
add_ships():
warship = WarshipFSM()
trader = TraderFSM()
player = PlayerFSM()
(...)
So how do I translate this to a component model? Thing is, I had this in my Python code at an earlier state and it didnt go too well. Something like:
class AIShip (Ship):
brain = WarshipFSM
In Python you than had the problem that you couldnt access the AIShip class from within the WarshipFSM class, so you would do:
class AIShip (Ship):
brain = WarshipFSM(self)
class WarshipFSM ():
__init__(parent):
self.aiship = parent
do_something():
self.aiship.do_some_more()
But I assume that Unity won’t have that problem since it’s component-based anyway.
If I wanted to follow the component-based path, how would I do it syntactically?
Would I import WarshipFSM and bind it’s instance to a variable (like above). Or would I use RequireComponent? But my AI class is not a component, right?
What I do know is, that I could (and will, anyway) create multiple prefabs for the AI classes. The prefab would contain an empty GameObject (for the model) + the common AI script + the specific AI script. But how I do access them horizontally?