The time has come for me to implement cutscenes (or scripted sequences) in my game. Is there a Node that can handle sequential events? If not, I have been developing a method of dealing with them. Let's have a look...
Firstly, I have a Game controller singleton that deals with pausing, the current loaded scene, HUD, and scene changing. What cutscenes are, are Control nodes with a GDscript attached that get added to the Game node as a child. For example, in my main menu when the player selects the New Game button, this happens:
func _on_NewGame_Button_pressed():
var script = test_scn.instance()
Game.add_child(script)
A preloaded Control node is attached the Game. Here's its script:
extends Control
onready var trans_scn = preload("res://Controllers/Transition.tscn")
onready var stage = 0
var trans
func _ready():
set_process(true)
func _process(delta):
if stage == 0:
trans = trans_scn.instance()
trans.fade = 0.01
Game.add_child(trans)
stage = 1
elif stage == 1:
if trans.alpha == 1:
trans.fade = -0.01
Game.setScene("res://Levels/Entryway.tscn")
get_tree().set_pause(true)
stage = 2
elif stage == 2:
if trans.alpha == 0:
get_tree().set_pause(false)
trans.queue_free()
queue_free()
It preloads a Transition node that handles the game's viewpoint fading in and out. In process we have the key part of how my game handles cutscenes. The process is perpetually checking which stage the script is at and then carrying out bits of script depending. In stage 0 the Transition node is made, giving a fade value so it fades to black, is then attached to the game, and the stage is set to 1.
As the Transition node iterates, it slowly increases its alpha value. Now that the cutscene script is at stage 1 it's checking to see if the Transition node's alpha value is at 1. Once it is, it reverses the fade value so it fades into the Entryway map that the game is set to load. The script also pauses the game (nothing and sets the stage value to 2.
Now once the Transition node's alpha is back at 0 then the pause is lifted, the Transition node is deleted and the plotscript itself is deleted.
This is a very basic script and I could have done a transition with a simple Transition node (in fact I was doing that before) but I will likely expand this New Game script to include more story including textboxes and the like. I will also try to "compress" it more so these cutscenes script require less lines, maybe take out the necessity of making a Control node and then attaching a script. There's more work to be done.
To reiterate, a node that can handle cutscenes or any kind of sequential eventing would be useful.