Using Player State?

Hi,

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:

https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/Types/Interface/UsingInterfaces/index.html

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:

https://sellfy.com/p/fo6a/

I don’t understand what you wanna mean with:

**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??

Here’s each step I described:

First right click in the content browser and create a class

Do a search for player state and select that as the parent class

Inside of any blueprint you can always see it’s parent class in the upper right corner

Add some variables to your player state, name, stats, score, etc. Make sure to click the eyeball so they’re remotely accessible from other blueprints.

109065-variables.png

Now inside your widget blueprint you can create a node to cast to the class you created.

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)

Then set the project’s Game Mode to Master_GameMode like so:

Then open up the game mode and set the player state on the righthand side to Master_PlayerState:

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.

Hi…i try all things that you say…but cast always fail in widget…

1 - I create all bleprints you say

2 - setup my project to work with this like you can see on screens below:

3 - Create interface functions

4 - Setup player state blueprint to implement the MMOInterface functions and create variables check “Editable”…lillte eyeball checked

5 - Setup level blueprint to show my interface
6 - Implemented the widget function that say what to do when i press button on widget

But…always fail the cast…need to put some delay?
If you wanna…I can share my screen to show the problem and what I alright doing.

Same error

Ahh, it looks like you might be forgetting to add the widget to the viewport

For the best structure, you should create a HUD blueprint and set your HUD in the game mode like this:

109235-gamemodeclasses.png

Then inside the code add your widget to the hud like this:

Then you should see it work.

Here’s the test project I set up [Download][3]

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.

You are awesome…It’s working^^! ^
Trying to learn how you do to work…you don’t use any interface…

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.