Is there any reason you shouldn't make the player controller a singleton in a single player game?

Question

A common pattern I’ve seen in godot when you need to access global data is to just make a simple singleton. e.g.

public partial class SingletonExample : Node
{
    public static SingletonExample Instance { get; private set; }
    public override void _Ready() { Instance = this; }
}

However, I’ve seen in other engines that getting the player controller is a bit different. For example in Unreal you use UGameplayStatics::GetPlayerController(WorldContext, 0). But inspecting the source code, it seems like this is mostly to handle multiple player controllers in a multiplayer game.

There still is some interesting differences here though - the player controller isn’t necessarily a singleton, but rather an object you can find from a few other different singletons (namely the GameInstance or the GameState). At the end of the day, the player controller is still accessed through a singleton though.

So I guess my question is - would it be unwise to make the player controller itself directly a singleton? What are the advantages and disadvantages of doing this over making it an object you can query from a different singleton object - e.g. maybe a PlayerManager or a GameState object. How do other people handle finding a reference to the player controller from a global context?

Anything global has the advantage that anything can access it.
Anything global also has the disadvantage that anything can access it.

It will very quickly become incredibly difficult to manage state changes and make sure your single instance is updated / reset accordingly.

For example the player loads a save file. Instead of just creating a brand new, clean instance and giving it a few parameters from the save file, you have to make sure every single part of it is reset back to a neutral state every time. And unless your player is incredibly small, that’s going to be a huge headache. Also you really shouldn’t have anything loaded in regard to the player OUTSIDE the actual gameplay. You shouldn’t even have the player loaded in the main menu.

I think the way I have it set up, the player controller isn’t loaded until the scene I have it placed in is loaded. I guess now that I think about it, that might cause some issues with the instance being set to an invalid object unless I’m careful about cleaning up. In this case a “player manager” class might make more sense just in terms of creating a more robust interface for querying the player controller when it might not be available.

You can get the best of both worlds if you provide a static API for the instance, but it is still an instance you can drop internally. Don’t store state in any statics, only provide get/set/functions that manipulate internal instance. It still will have the same issues any singletons have tho, cant substitute, no interfaces, but in practical terms you would need to do massive refactor of some singleton only once in a while, not enough to justify dropping convenience in an indie project.

The idea of offering a static accessor while keeping no state in statics, as ansihex described, looks like a reasonable middle ground. Since the player scene only loads together with its level, would it help to clear the static reference in _ExitTree so it never points at a freed node?

Singletons outside of global systems such as a signal bus, an audio manager or a save manager cause more trouble than it is worth. Detecting the player when needed by using a function like get_tree().current_scene.find_child("PlayerName"), directly using @exportexportexportexport references to the player, or using Area2D/3D’s is a much safer way to reference the player. With singletons, even reloading the current level becomes hard because the player singleton is not reloaded once you reload the scene so you’ve gotta manually place the player back to the initial position etc. There is also the nightmare of figuring out bugs if it is a singleton because every node can access it and a node you forgot about writing months ago may still be setting the player’s properties, figuring out which script is causing the unintended bug and behaviour may become a tedious task.

You can always get creative with how you get references to the player node. But a singleton is a bit of a shortcut that may cause a headache if not handled carefully.

same thing happens if you dont use the singleton. the nodes will still change player properties, but now they will access it in a different way, you will still have to refactor every class that directly references the player, but player reference is acquired in a different way, and now THAT way to acquire the player is to blame? Those are just issues that happen, singleton or not, you have to refactor, you have to hunt down bugs, you have to keep track of usage.

Yeah you are right, that can happen whether you are using a singleton or not.

Clearing the static reference in _ExitTree is actually exactly what I started to implement after writing this question. I think having a singleton global reference to the player controller makes sense for the scope of this project.

You are right on other issues, like reloading a level, you need high level code, events, communication between top systems about state etc, but, without singletons, you need even more localized boilerplate code to glue things together, now you have to pipe events/signals from the bottom up and in whatever direction along the hierarchy, there is no overview of what is going on. Both approaches have pain points.