メインコンテンツまでスキップ

ゲームサーバーを使用してプレイヤーインベントリを管理する

Last updated on October 23, 2024
注記

The Inventory service is available upon request. To integrate this service into your environment, please open a request through the AccelByte Customer Support Portal.

Overview

The AccelByte Gaming Service (AGS) Inventory service allows you to manage players inventory and items directly from the game server, enabling inventory and items management, organization, or manipulation.

This article lists how you can enable your game server to manage the inventories owned by your players.

Prerequisites

  • Access to the AGS Admin Portal.

  • A publisher or a game namespace.

  • You have reviewed the key concepts of the Inventory service and the Inventory service API documentation.

  • You have integrated the AccelByte SDK into your game, including the following permissions:

    • Client ID

    • Client Secret

      注記

      Support for Unreal and Unity SDKs is currently in development and will be available at a later date. In the meantime, we encourage you to utilize our Extend SDK for all your game server-related actions.

Grant inventory to the player

You can use this action if you want to allow your game server to grant inventories to players after they reach an achievement or complete a specific action within the game, for example, granting new inventory after a player achieved level 20.

You can enable the game server to grant a new inventory to the player as long as the number of inventory with the same code does not exceed the "maxInstancesPerUser" set in the inventory configuration.

Use this function for reference:

AccelByteSDK sdk = AccelByteSDK.Builder
.UseDefaultHttpClient()
.SetConfigRepository()
.UseDefaultTokenRepository()
.EnableLog() // Optional
.Build();


string gameNamespace = "gameNamespace";
string inventoryConfigurationCode = "SomeConfigurationCode";
string userId = "userId";


ApimodelsCreateInventoryReq body = new ApimodelsCreateInventoryReq()
{
InventoryConfigurationCode = inventoryConfigurationCode,
UserId = userId
};

var response = sdk.Inventory.AdminInventories.AdminCreateInventoryOp
.Execute(body, gameNamespace);
if (response == null)
return "No response from server.";


// Do something with the response

Retrieve list of player inventories

Retrieving a list of player inventories can give you information about a player's inventory, such as the inventoryId and inventoryConfigurationId.

To retrieve a list of player inventories via your game server, use this function for reference:

AccelByteSDK sdk = AccelByteSDK.Builder
.UseDefaultHttpClient()
.SetConfigRepository()
.UseDefaultTokenRepository()
.EnableLog() // Optional
.Build();


string gameNamespace = "gameNamespace";
string inventoryConfigurationCode = "SomeConfigurationCode";
long limit = 10;
long offset = 0;
string userId = "userId";


var response = sdk.Inventory.AdminInventories.AdminListInventoriesOp
.SetLimit(limit)
.SetOffset(offset)
.SetSortBy(AdminListInventoriesSortBy.CreatedAt)
.SetInventoryConfigurationCode(inventoryConfigurationCode)
.SetUserId(userId)
.Execute(gameNamespace);
if (response == null)
return "No response from server.";


// Do something with the response

Update player inventory slot limit

Use this action to update a specific player's inventory maximum slot limit by providing the inventory ID and the new number of slots that will be implemented.

To update a player's inventory slot limit via your game server, use this function for reference:

AccelByteSDK sdk = AccelByteSDK.Builder
.UseDefaultHttpClient()
.SetConfigRepository()
.UseDefaultTokenRepository()
.EnableLog() // Optional
.Build();


string gameNamespace = "gameNamespace";
string inventoryId = "inventoryId";
int incMaxSlots = 10;


ApimodelsUpdateInventoryReq body = new ApimodelsUpdateInventoryReq()
{
IncMaxSlots = incMaxSlots
};


var response = sdk.Inventory.AdminInventories.AdminUpdateInventoryOp
.Execute(body, inventoryId, gameNamespace);
if (response == null)
return "No response from server.";


// Do something with the response

Delete player inventory

Player inventories can only be deleted if they're empty. Ensure that you first remove all the items from a player's inventory before deleting it.

To delete a player's inventory via your game server, use this function for reference:

AccelByteSDK sdk = AccelByteSDK.Builder
.UseDefaultHttpClient()
.SetConfigRepository()
.UseDefaultTokenRepository()
.EnableLog() // Optional
.Build();


string gameNamespace = "gameNamespace";
string inventoryId = "inventoryId";


ApimodelsDeleteInventoryReq body = new ApimodelsDeleteInventoryReq()
{
Message = "message"
};


sdk.Inventory.AdminInventories.DeleteInventoryOp
.Execute(body, inventoryId, gameNamespace);

Add item to player inventory using inventoryId

Use this action to add items to a player's inventory. This requires you to provide the player's inventoryId, which is information that you can get from retrieving a list of player inventories.

Use this function for reference:

AccelByteSDK sdk = AccelByteSDK.Builder
.UseDefaultHttpClient()
.SetConfigRepository()
.UseDefaultTokenRepository()
.EnableLog() // Optional
.Build();

string gameNamespace = "gameNamespace";
string inventoryId = "inventoryId";
string userId = "userId";
Dictionary<string, object> customAttributes = new Dictionary<string, object>();
Dictionary<string, object> serverCustomAttributes = new Dictionary<string, object>();


ApimodelsSaveItemToInventoryReq body = new ApimodelsSaveItemToInventoryReq()
{
CustomAttributes = customAttributes,
SlotId = "slot-1",
SlotUsed = 1,
Qty = 1,
ServerCustomAttributes = serverCustomAttributes,
SourceItemId = "source item id",
Tags = new List<string>() { "weapon" },
Type = "ingame"
};


var response = sdk.Inventory.AdminItems.AdminSaveItemToInventoryOp
.Execute(body, inventoryId, gameNamespace, userId);
if (response == null)
return "No response from server.";


// Do something with the response

Add item to player inventory by inventoryConfigurationCode

Use this action to add items to a player's specific inventory configuration. This action requires you to provide the inventoryConfigurationCode, which is information that you can get from retrieving a list of player inventories.

Use this function for reference:

AccelByteSDK sdk = AccelByteSDK.Builder
.UseDefaultHttpClient()
.SetConfigRepository()
.UseDefaultTokenRepository()
.EnableLog() // Optional
.Build();


string gameNamespace = "gameNamespace";
string userId = "userId";
Dictionary<string, object> customAttributes = new Dictionary<string, object>();
Dictionary<string, object> serverCustomAttributes = new Dictionary<string, object>()
ApimodelsSaveItemReq body = new ApimodelsSaveItemReq()
{
CustomAttributes = customAttributes,
InventoryConfigurationCode = "SomeConfigurationCode",
SlotId = "slot-1",
SlotUsed = 1,
Qty = 1,
ServerCustomAttributes = serverCustomAttributes,
SourceItemId = "source item id",
Tags = new List<string>() { "weapon" },
Type = "ingame"
};


var response = sdk.Inventory.AdminItems.AdminSaveItemOp
.Execute(body, gameNamespace, userId);
if (response == null)
return "No response from server.";


// Do something with the response

Retrieve list of items from a player's inventory

Use this function for reference:

AccelByteSDK sdk = AccelByteSDK.Builder
.UseDefaultHttpClient()
.SetConfigRepository()
.UseDefaultTokenRepository()
.EnableLog() // Optional
.Build();

string gameNamespace = "gameNamespace";
string inventoryId = "inventoryId";
long limit = 10;
long offset = 0;


var response = sdk.Inventory.AdminItems.AdminListItemsOp
.SetLimit(limit)
.SetOffset(offset)
.SetSortBy(AdminListItemsSortBy.CreatedAt)
.SetQtyGte(1)
.SetSourceItemId("sourceItemId")
.SetTags("weapon")
.Execute(inventoryId, gameNamespace);
if (response == null)
return "No response from server.";


// Do something with the response

Update item attributes and tags

Unlike the game client, the game server has more ability to add additional attributes and tags.

Update custom attributes

You can use your game server to update custom attributes written by players and attributes that can only be modified by the server with required permissions. You can use this action to update attributes that will impact gameplay experience, such as item stats modifier. This attribute will be stored in the serverCustomAttributes field, for example, items with additional damage, critical, durability, etc.

Using Server Custom attributes in Inventory item in AGS

Update tags

You can use your game server to add tags to the items for the purpose of tagging and grouping them. Game servers can add tags to the items and not allow players to update it.

The following image shows a sample item with a tag.

Using tags in Inventory item in AGS

Use this function for reference:

AccelByteSDK sdk = AccelByteSDK.Builder
.UseDefaultHttpClient()
.SetConfigRepository()
.UseDefaultTokenRepository()
.EnableLog() // Optional
.Build();


string gameNamespace = "gameNamespace";
string inventoryId = "inventoryId";
string userId = "userId";
Dictionary<string, object> customAttributes = new Dictionary<string, object>();
Dictionary<string, object> serverCustomAttributes = new Dictionary<string, object>();


List<ApimodelsAdminUpdateItemReq> body = new List<ApimodelsAdminUpdateItemReq>()
{
new ApimodelsAdminUpdateItemReq()
{
CustomAttributes = customAttributes,
SlotId = "slot id",
SourceItemId = "sourceItemId",
ServerCustomAttributes = serverCustomAttributes,
Tags = new List<string>() { "Axe" },
Type = "ingame"
}
};


var response = sdk.Inventory.AdminItems.AdminBulkUpdateMyItemsOp
.Execute(body, inventoryId, gameNamespace, userId);
if (response == null)
return "No response from server.";


// Do something with the response

Consume items

To consume items owned by the players via your game server, use this function for reference:

AccelByteSDK sdk = AccelByteSDK.Builder
.UseDefaultHttpClient()
.SetConfigRepository()
.UseDefaultTokenRepository()
.EnableLog() // Optional
.Build();


string gameNamespace = "gameNamespace";
string inventoryId = "inventoryId";
string userId = "userId";


ApimodelsConsumeItemReq body = new ApimodelsConsumeItemReq()
{
Qty = 1,
SlotId = "slotId",
SourceItemId = "source item id"
};


var response = sdk.Inventory.AdminItems.AdminConsumeUserItemOp
.Execute(body, inventoryId, gameNamespace, userId);
if (response == null)
return "No response from server.";


// Do something with the response

Remove items from player's inventory

To remove items owned by players from their inventory via your game server, use this function for reference:

AccelByteSDK sdk = AccelByteSDK.Builder
.UseDefaultHttpClient()
.SetConfigRepository()
.UseDefaultTokenRepository()
.EnableLog() // Optional
.Build();


string gameNamespace = "gameNamespace";
string inventoryId = "inventoryId";
string userId = "userId";


List<ApimodelsRemoveInventoryItemReq> body = new List<ApimodelsRemoveInventoryItemReq>()
{
new ApimodelsRemoveInventoryItemReq()
{
SlotId = "slot id",
SourceItemId = "source item id"
}
};


var response = sdk.Inventory.AdminItems.AdminBulkRemoveItemsOp
.Execute(body, inventoryId, gameNamespace, userId);
if (response == null)
return "No response from server.";


// Do something with the response