Hi, I’m looking for advice on the right / best / a good way to approach in-level cutscenes / animations. I don’t want to change scene, or fade or anything that interrupts the game.
The player is a characterbody2D with all the usual nodes and a state machine, movement controlled by wasd inputs, and animation controlled by an animation player (animation chosen by what is happening in the state machine).
The sort of process I’d like to implement now would be something like
For example:
Player presses an elevator button - implemented as part of player interactions
Disable player input - implemented as part of player
Wait 2 seconds (for elevator to arrive and door to open)
Walk to center of elevator
Face front
Door closes
Change level - implemented in level manager
Activities 3-6 should be done by the game, not by the input. I’m not concerned about the elevator and player talking to each other during this process, I’ll time the animations for now so it just works.
I’m just struggling to find an approach that controls the player for a short time. I’ve considered:
An additional “game controlled player” state where I send data over on certain things, but this means I can’t use the animations based on the existing state machine, and would have to call all the relevant animations in that state, and match based on data. (This would be OK, but only if there’s not a better way)
Adding more animations to the animation player, but this could create a huge list and add more code to the player
I do have limboAI for enemy behaviour trees, but doesn’t feel right to attach one to the player unless that is actually a good way to do it.
Sounds like a nice fit for a Command Pattern implementation. There are some nice tutorials online about it, but I also posted about one I made before. It uses C# but you can easily adapt it for GDScript as well.
You basically just create a simple abstract class which has an execute function, which inherits Resource, then you create specific classes that execute specific actions. Then, you can create a “runner” that will go through each action you created in the inspector and execute them one by one. It’s incredibly nice and useful for cutscenes especially but even for general gameplay too. I shipped a whole game using mostly this pattern before.
My current game has most of the Characters spending most of their time as NPCs wandering about doing their own things and I choose 1 character to take control of it.
My state machine has evolved into states such as GoToLocation, ChaseCharacter, SearchFood, etc. and the ChangeState signal passes an optional dictionary of useful information such as a Vector2 location or a character node name to chase. I also have a general purpose WhatNext state that tries it’s best to determine what to do when each of these activities/states completes.
Some states such as SearchFood go straight to the Eat state if they find find, if they get interrupted by an enemy they may call WhatNext to decide about fighting or running away.
It’s still very early days for the animations, I just have basic Sprite2D faces, but so far game controlled states is working for me.
Thanks for laying out the elevator sequence step by step, it makes the question easy to follow. Since your player already has a state machine, would a dedicated scripted state that ignores input and just moves toward a target position be enough for steps 3 to 6, or do you want the AnimationPlayer to drive the movement as well? That choice seems to decide whether the command-style runner mentioned above or a simple extra state fits better.
Thanks Everyone - I’ve put in a basic command pattern for the player control, and (currently) put the player in a new “non-controlled” state in the state machine during the time they are being controlled by the game.