I have a Widget that displays a player character’s stats. What is the correct way of making a reference or casting so that the Widget will still display the correct stats even after destroying the Actor and possessing another Actor?
To get a reference to the player, you can simply use the GetPlayerCharacter node.
I assume you have some reason for wanting to display these stats after destroying it- other than just displaying them, I mean.
The common place all purposes align should be where you store it, but you can store the actual data inside a struct.
Just make sure this struct gets updated in the widget/common-place before destroying the actor
there are various ways. this generally fall in the game programming patterns category. you can search the web for articles or youtube. you need to decouple the UI from the actor in some way and that is what those patterns are for.
it depends of what you need in your project, but is important to not go crazy with these patterns as is a big rabbit hole, if you don’t need the full functionality.
it may be as simple as moving the UI initialization to the PlayerController instead of the pawn/actor.
So I should display the Struct’s values rather than the player character’s?
Since you’re destroying the player character, yes.
The player won’t exist anymore- you won’t be able to get the values from it.
Again, make sure this is updated before the character dies. Depending on the purpose of the widget, it might not be bad practice to just have the character itself do it.
I see, thank you