Hello,
First of all, I am an experienced solo unity dev but new to Godot. I am trying to make my player (a state machine based player ) get followed by NPC then if the player dies, NPC becomes controllable via keyboard/controller. I have been struggling to figure out which is the most scalable way to implement this architecture, as I am also intending to make the player state machine grow by adding more states later ,(and by consequence, NPC should also have access to the same state machine). I was planning to use composition, but godot's 's way with aggregation makes it a bit difficult with only one script allowed per Node. In Unity, I believe this would be solved by making the NPC composable and implement an interface that adds the player functionality when the player(or array leader) dies and observer pattern (signals in godot).
The problem is I am new to Godot and still learning my way around it, but I am still unsure how to make the entire architecture described in the title while accounting for possible scalability issues.
What I thought of on the top of my head.

  • make the npc and player share one parent class named Entity but different state machines then implement move /jump and rest of the states separately, the problem is once the player dies this becomes an issue to pass control from NPC to player, I am not sure how i'd implement it in this case...

  • use Resources? I am not sure, but as , per docs, that resources can accept a script at runtime, which made me think of slapping a Resource on all entities (same parent class) and in a manager class, manage the entities by giving the first element of the array the player script (with input) and the NPCs their AI script, once player dies, the resource on the next element of the array gets the player script while freeing the dead player. is this possible?? I am not familiar with Resources but this sounds like godot's version of Scriptable Objects.

  • I read somewhere about using virtual Input architecture? which would be nice to keep changing between a player/npc, but not really my use case. I am not familiar with this either but I wonder if anyone knows anything about this and if it applies to my use case.
    This is the first time I am encountering this use case and if it were just player + npc follow i'd do it, but passing the control is where I am a bit hazy about it. In unity composition is possible by adding multiple scripts to the gameobject, in Unreal, it literally has a possess pawn class that helps with this, but in godot...I am not sure how to do it. I would love to have your input and advice.
    Thank you for your time and help!

you can attach script to an empty node and put that node as child, this having multiple scripts in one node.
i would try doing something like this.

World
   Enemy
   Player

So here enemy needs to be attached to player somehow. Like when player enters enemy area enemy gets a reference to player. Store it in a var and connect to a dead signal. When player dies he emits a dead signal with player reference var.
Enemy node would have all the input controls and camera controls like a player but disabled. On player death you just activate the enemy to start using the controls inputs, and re parent the player camera node to enemy node, Disable controls on player node and bobbie is your unkie.

    kuligs2
    Hi,
    I will try the empty script bearing nodes as children. I didn't know about this way.
    While I thought of obviously using signals to propagate the OnplayerDead() event, the issue comes with scalablity as the player himself grows (gains ability along the way thus gaining new states),maybe I should also make all followers gain the same ability as they go and activate it. the problem is adding a new state at runtime in a way like plug-and-play screams "use resources" to me.
    this will still need another layer of abstraction for an ability class.
    I will try your suggestion first , regardless of the scalability. thanks!