Hi,
I’ve been trying to create a basic implementation of the observer pattern, for events such as the player taking damage and the HUD needing to update as a result. While I am aware that I can do things like that straight from my player class, I would prefer to keep things tidy.
To that end, I’ve attempted to create the pub-sub pattern, and they seem to be OK on paper, but my publisher’s subscriber list keeps resetting during gameplay (I add the HUD as subscriber in my GameMode’s BeginPlay()). I assume this is because the player class which inherits it gets respawned, and the data in the TArray doesn’t persist.
Relevant bits of code:
- https://hastebin.com/ugovunaxap.cpp - Publisher interface (my Character class inherits this)
- https://hastebin.com/atodunaxas.cpp - Subscriber interface (my HUD class inherits this)
I can confirm that the IPublisher::Subscribe function does get called, and that the container size is indeed 1 after the call, but the IPublisher destructor gets called as well, afterwards, so my Update() calls will do nothing.
So, what I would like to know is: what am I doing wrong? What should I do to keep my list of subscribers even when the player respawns?
Thanks in advance!