I am trying to get the variable from player state in my multiplayer game at the event begin play but casting fails,
And if I try that casting on key event press that time I am able to access the value from player state. I don’t know what the hell is happening in this case.
Please help me.
Just curious: What does the GameMode look like, can you post screenshots of it? and do you have HubZero_PlayerState set in your GameMode as shown below?
Also is the GameMode where you have PlayerState set currently active in the level you are testing this BP out in?
The cast will fail if the PlayerState you are getting does not match the PlayerState you have set in the GameMode properties.
I wanted to add that you may want to use Get Controller inside of your character blueprint so that you can get the controller that is currently being used by that specific client.
The suggestions to use a delay or put the logic into Tick give me the heebie jeebies as a programmer. Instead, my solution for solving the issue where PlayerState was null/unavailable until a few frames after BeginPlay was to put my logic in an overridden version of ACharacter::OnRep_PlayerState(). This gets called as soon as the PlayerState is first assigned, or ever changes.
Instead of putting unnecessary stress on the Tick event, or calling Delay without knowing the exact amount of time needed, you can use lazy loading.
If you don’t need that value at instant, just create a GetValue function which checks if the variable has a value. If not, it gets the required data and sets the variable, then just return it.
The best solution would be if Unreal had events triggering when all the required classes are set and ready on the long term.