Mastering CryENGINE
上QQ阅读APP看书,第一时间看更新

User profiles

One of the distinct advantages of using Action Maps instead of input events is the ability to allow the player to customize the input controls of the game to their personal preferences. The modified Action Maps are saved and loaded using user profiles.

This section will explain how to modify an ActionMap at runtime as well as how to load and save them. You will need to decide whether adding this functionality makes sense for your type of project.

Tip

Apart from the Action mapping, user profiles also store a list of personal settings, such as brightness, volume, and difficulty level. As these are not input system relevant, they will not be covered in this chapter. If you want to learn more, please take a look at the attributes.xml file at Game\Libs\Config\Profiles\default.

Modifying user profiles

User profiles are managed inside the CryAction DLL, but they can be accessed and modified from anywhere in the codebase. Full source access is not required. If you want to allow players to change the controller mappings in your game, you would offer a menu of some kind. Once you know which Action you want to remap to which input events, you can access the user profile and modify it.

To access the user profile you need to use the PlayerProfileManager, which you can retrieve from the GameFramework. If you like, you can put your code into the CGame class inside Game.cpp, as it already includes all the necessary header file to work with profiles as seen in the following code snippet:

#include <IPlayerProfiles.h> 
IPlayerProfileManager* pProfileMgr = gEnv->pGame->GetIGameFramework()->GetIPlayerProfileManager();

The profile manager lets you access the currently active profile. For this, you will need the username of the user that is currently logged into the system on which your game is running. On a Windows machine, this will be the username that was used to log in; on a console such as the Xbox, this is the name of the user currently signed in to the system. Most platforms support multiple users, and CryENGINE manages separate profiles for each of them.

const char* userName = pProfileMgr->GetCurrentUser();
IPlayerProfile* pProfile = pProfileMgr->GetCurrentProfile(userName);

Once you have a pointer to the current profile, you can retrieve an ActionMap by its name and rebind an Action to a different input event. This is the function to change the mapping:

virtual bool ReBindActionInput(const ActionId& actionId,
      const char* szCurrentInput,
      const char* szNewInput);

The first parameter asks for the ActionId of the Action you wish to change. You will need to include the file GameActions.h to access the ActionIds as they were declared in the GameActions.actions file (see the section Setting up an Action event). Next, you will need the names of the input events this Action is currently bound to and the one to which you want to change the mapping.

#include "GameActions.h"

pProfile->GetActionMap("player")->ReBindActionInput(g_pGameActions->jump, "space", "k");

Since there can be more than one input event mapped to one Action event, you will need to specify which one you intend to change. The preceding example rebinds the jump Action from the Space bar to the letter k. The Action is also bound to the A button on the Xbox controller. This mapping, however, was unchanged.

After you changed a user profile, you need to save it again so that the changes are not lost once the game is quit. You can do so by telling the profile manager to save out the profile currently active for this user as follows:

IPlayerProfileManager::EProfileOperationResult profileResult;
pProfileMgr ->SaveProfile(username, profileResult, ePR_All);

User profiles are stored inside the PROFILENAME folder under USER\Profiles, which is created in the build's root folder when CryENGINE is started for the first time. You can navigate there in a file explorer and look at the content. The XML files are stored in binary format, however, and cannot be edited with a text editor.

All available profiles are automatically scanned at the start of the game, so you don't need to manually load them. To switch to a different profile, simply activate it in the profile manager as follows:

pProfileMgr ->ActivateProfile(userName, profileName);

DLCs and patches

Note that when you save the Action mapping in the user profile, only the mappings that differ from the default profile are being stored. If you release a patch for your game at a later time with some changed or added Action mappings, the defaultprofile.xml file will work as expected.

In case you want to force the player to use your updated mapping with a patch or DLC and discard previously modified Action Maps, you can increase the version number at the top of the defaultprofile.xml file. If the version number in there is larger than the one stored in the user profile, the player-modified map will be ignored.