I wanna create a little MMO game. To do this, I learn that need to use the PLAYER STATE BLUEPRINT to make replications of player name, stats, score, etc. I see in other packages it is used on player state being CAST from Widget Blueprints…but I can’t duplicate this on my project.
HOW DO AN CAST TO BLUEPRINT PLAYER STATE?
Anyone can help pls?
Creating a player state blueprint is the same as creating any other blueprint class. Right click in the content browser > New Asset > Blueprint Class > All Classes > Player State.
if you name it Master_PlayerSate then you’ll have an option in any blueprint interface to do a cast to Master_PlayerState.
But player state blueprints are empty by default, so you’ll need to add your own variables. If you add one inside the blueprint called MyVariable then drag a pin out from your cast Master_PlayerState in your widget blueprint you’ll see options like Set MyVariable and Get MyVariable.
Also for network purposes be sure to set your GameMode to use Master_PlayerState as its player state class, and set the replication option on each variable in your player state to “Replicate”.
Once you’ve got all that working, take a look at blueprint interfaces here:
That will allow you to avoid casting altogether, but it comes with the caveat that you have to create interface functions for each variable in your blueprint.
What I ended up doing after awhile to avoid creating all the extra functions is creating my own data table for my player state, and storing the data table as a single variable in the blueprint. For an MMO with many hundreds/thousands of players this would be pretty inefficient, and you’d want to use an SQL database instead.
For examples of an SQL implementation you should take a look here:
**Creating a player state blueprint is the same as creating any other blueprint class. Right click in the content browser > New Asset > Blueprint Class > All Classes > Player State.
if you name it Master_PlayerSate then you’ll have an option in any blueprint interface to do a cast to Master_PlayerState.
**
**
But player state blueprints are empty by default, so you’ll need to add your own variables. If you add one inside the blueprint called MyVariable then drag a pin out from your cast Master_PlayerState in your widget blueprint you’ll see options like Set MyVariable and Get MyVariable.
**
**
Also for network purposes be sure to set your GameMode to use Master_PlayerState as its player state class, and set the replication option on each variable in your player state to “Replicate”.**
I understand programming strategies, but don’t understand this in blueprint yet…
Can explain more about this??
But you still aren’t done because you haven’t told the game mode to use your player state. Go back and do the first two steps again, but this time create a Game Mode class called Master_GameMode.
(see continued answer because it won’t let me use more than 5 attachments)
Now the player controller that your game mode creates at runtime will have a playerstate that is of the class Master_PlayerState, the cast will succeed, and you can access the Player State variables you created from your widget blueprint.
I haven’t used the simulation features that much, I don’t think player states get spawned until run-time, so when you run the simulation It might not have anything to access.
Actually, once you start having too much code, blueprint interfaces are actually better because they can help keep things more oraganized.
Imagine if I have 3 different pawn classes that the player controller could be possessing at any moment, they all have their own Jump function. You want to call Jump on the player’s current pawn, but how do you know what pawn type to cast to? If you have a blueprint interface that all of your pawns implement, then you just call the interface function for jump (To differentiate it, I would call it IJump) against the current pawn, and you don’t have to figure out what type it is.
It’s important though to know how casting works, because that’s the basis of any typed language, and ultimately blueprints compile down to c++ objects which are typed.