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.
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.