In this post, we’re going to create and fetch the characters for the logged in user.
This is extension of previous post which integrated with PlayFab to Login and Register.
There are two specific documents that may be useful to check for this:
The pre-requisites for this post are:
- Dealing with complex nested options
- Applying the options to skeletal mesh (using n-hance assets)
- Login and register with PlayFab
Select character widget
We will not look to focus into the design (that’s been covered in other posts) but specifically the blueprint logic relevant to PlayFab.
We’ll get started with Select Character Widget as that’s the one that will be brought up after you login.
When you construct this widget, you will want to fetch the character data from PlayFab.
In order to keep things neater, I created a new Graph to separate the PlayFab logic.
The functionality of this PlayFabApiComms is similar to one I setup in custom server tutorial, but obviously making calls to PlayFab instead.
Ok so as you can see, inside the PlayFabApiComms graph, I created a custom event: FetchCharactersFromPlayfab.
This first configures (or simply sets local) auth related variables – this will be shown in next image.
It then makes the call to Get All Users Characters – this is a PlayFab api call.
We’ll look at the success scenario shortly, but for failure scenario we can just create the error widget with message as content. This error widget was created in the previous post, when handling login/register.
Configure Auth Vars is a simple function which looks into our Game Instance to find the Login Info variable and extracts the PlayFab ID and Authentication Context and adds those as variables within this blueprint for easier access. This was introduced part of Login/Register flow.
Next lets look into the success scenario for when we get our characters.
Here we get our character response, which contains Character Name and Character ID.
It does not contain Character Data which we need – this will contain things like appearance information for the character.
We fetch the character data using Get Character Data API call from PlayFab, but for that we need to prepare the request.
Part of this request, I prepare a Map<String, String> which will contain Character ID -> Character Name.
This is because the response from Get Character Data only contains the ID, not the character name so I will need a mechanism to resolve this.
If we fail to get the character data, again we simply add the error widget to the screen with the error message.
On success, we need to process the response.
In order to align this with the custom server I’ve built, to reuse the components that are there, I essentially convert the data to Account Characters which has the following definition:
And Character Appearance:
The blueprint function to Process Character Data Response is as follows:
And the Play Fab Json To Appearance Info function is made part of a PlayFabHelper blueprint as a Blueprint Function Library to help us reuse this in future if required.
This is very similar to how we processed json data using VaRest plugin with the custom server. Essentially the response contains a JSON response and some documentation can be found here:
Essentially, in the json we expect to have json values encoded for all the parameters that we have in our Appearance Info structure – we have not added this yet, this will be done when we look into create character flow.
The main purpose is to generate the Appearance Info struct out of the JSON response and return it
The ‘Get String Value Of’ function definition is:
Feel free to ignore the debugging logs, but it can be useful to identify any issues.
Once we do this, we now possess all the data necessary to render our desired character on the screen. This means we have:
- Character ID
- Character Name
- Appearance Information
- any other custom data that’s requested
With that, we can move to Create Character widget.
Create character widget
Again, we will be focusing more on the PlayFab blueprints rather than the design of this screen.
In order to create a character, we must do the following:
- Create desired character
- Create custom params for the character (appearance information)
- Rollback changes on failure
My blueprints for this is as follows:
The Update auth vars is the same as the one in Select Character Screen.
The function Get Tag Value is not too important here, this is just my implementation for getting class name which I’ve fed to character type. You can change this to something else or make it static.
Next, I generate the Make ServerGrantCharacterToUserRequest, which importantly requires 4 values:
- character name
- character type
- play fab id
- authentication context
This request will not take in custom params and they will need to be created separately.
On error, we simply display the error message to user, this is useful as it will provide additional validation, such as duplicate name error and more.
If the call is successful, we want to now assign the custom data to the created character.
I do this using another function to refactor it, Assign Appearance Info to Character.
Update Auth Vars is not required here as we already updated them earlier, but its ok to be explicit.
Appearance Info To Play Fab JSON is now the inverse of the function we created in Select Character screen. i.e. we want to take our Appearance Info custom data and serialize it into JSON.
Therefore you can see this function takes the appearance info struct and extracts field by field each string parameter and sets it into the JSON object.
We now make the request to update the character data. On success we can simply navigate back to character selection screen so its simple – you can provide an additional success message to the user if you like.
On Fail however, we need to rollback the changes, because you will have a malformed character in the database.
Here we can see we try to DELETE the character if we fail to create the custom parameters for the character.
First you may want to log the reason, because this is unexpected and the user does not need to know this – it will rarely be a users’ fault if this fails, but more of technical issue.
Generate the request to delete the character, which mainly just needs the character ID.
If the request succeeds – this is good, we handled the edge case and user can try again.
If this request failed too, this could be a bigger issue, you may now have a malformed character that belongs to the user. You may need background jobs which try periodically clean these, but these should be unlikely.
For example it can happen with a network glitch.
And that’s it!
We now have a working Create character and Select character screens integrated with Play Fab.