Hi BioMedKUL
Preconstruct = Works a bit like a class constructor. It is responsible for building the widget and will be called in the editor / game.
Construct = Works like BeginPlay. Called once the widget is loaded in the level / viewport
OnInitialised = Called on non-instanced implementations of the widget. It is used for internal setup such as delegate bindings/property initialization (things that only need to happen once). I haven’t really used OnInitialised much myself, and I could be wrong here; it seems to behave like a singleton for the UUserWidget.
I started setting the game mode
variable of my widget during the
‘construct’ event. But this means that
every time the widget spawns, the
variable gets overwritten with the
same value. Can I move the setting of
this variable to ‘on initialized’ and
be sure it only gets set once?
OnInitialised will not be called on instanced versions of the widget so you will not be able to access and store the GameMode here as the GameMode will not exist when it is called or the GameMode can be subject to change.
OnPreConstruct also is not an appropriate place to cache references to your game mode as it can be called in design time when the GameMode will not exist.
Providing you’re not working with networked applications (The GameMode cannot see widgets), then there is no reason for you not to call GetGameMode in your widget constructor. I would recommend that you always store a variable reference to the game mode etc OnConstruct as casting in blueprints is expensive so only calling it once when the widget is created is a good way to go.
I personally create a base class for my widgets with a function called “CacheReferences” in which I will store object references all my widgets require when OnConstruct is called.
Hope this helps
Alex