I can’t seem to find a lot of information about these 3 widget events.
I would like to know what the difference is between these. More specifically, I want to know if the major difference between ‘construct’ and ‘on initialized’ is that the latter is only called once and the former is called every time the widget is added to the viewport.
An example of something I want to do is store the game mode in my widget so I can access its functions. When I started learning unreal 6 months ago, 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?
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.
When the GameMode stays the same throughout the game and the widget itself also has only one instance that gets removed and added to the viewport, which is better to create the variable reference to my game mode or other const variables and references? Construct or OnInitialized?
The GameMode may not stay the same throughout. Each time a level loads, the current game mode gets destroyed and a new one is formed even if they are of the same type. It is the exact same for every other object (except the GameInstance).
widget itself also has only one
instance that gets removed and added
to the viewport
This line confuses me a little. If you remove your widget entirely from the viewport, it gets destroyed and reinitialized when it is readded so the constructor will run again which will be required to reset itself and cache any references it needs.
Having the widget store a reference to the GameMode on Construct is the best way to go about this.
I would discourage having the GameMode storing references to widget objects due to that is not how the architecture for a project should be set up (in most cases).
The GameMode class is used to define the rules for the level and the handling of controller objects. In short term, objects like widgets etc are irrelevant to the GameMode. UI should be handled by the HUD or PlayerController classes. Widgets may want to access the GameMode to retrieve certain information but the GameMode shouldn’t be accessing widgets to tell them about the information.