Use case: Inventory
This can be useful for displaying the NFTs owned by a player in the game, or in your game's marketplace.
Overview
Displaying a player's inventory is a common feature in games that involve digital assets. To do this, you can simply send a request to the Immutable zkEVM API to fetch the NFTs owned by a player and then processing the request to display the NFTs in the game's inventory.
Displaying NFTs in the game inventory
To display a gamer NFT inventory, you can use the Search NFTs endpoint.
In order to obtain a list of NFTs owned by a player, you need to send a search request to the Immutable zkEVM API. The request parameters are populated based on the current state of the UI Marketplace policy settings.
Obtain NFTs owned by a player
ImmutableOpenAPI::OpenAPIStacksApi::SearchNFTsRequest SearchNFTsRequest;
SearchNFTsRequest.ChainName = Policy->GetChainName();
SearchNFTsRequest.PageSize = (ListPanel->GetNumberOfColumns() * ListPanel->GetNumberOfRows());
SearchNFTsRequest.PageCursor = PageCursor;
SearchNFTsRequest.AccountAddress = GetOwningCustomLocalPLayer()->GetPassportWalletAddress();
SearchNFTsRequest.ContractAddress = Policy->GetContracts();
SearchNFTsRequest.OnlyIncludeOwnerListings = true;
Policy->GetStacksAPI()->SearchNFTs(SearchNFTsRequest, ImmutableOpenAPI::OpenAPIStacksApi::FSearchNFTsDelegate::CreateUObject(this, &USearchNfTsWidget::OnSearchNFTsResponse));
Example of processing the response
void USearchNfTsWidget::OnSearchNFTsResponse(const ImmutableOpenAPI::OpenAPIStacksApi::SearchNFTsResponse& Response)
{
...
int32 NumberOfResults = Response.Content.Result.Num();
...
for (int32 ResultId = 0; ResultId < NumberOfResults; ResultId++)
{
ItemInterface->ProcessModel(ItemData);
ItemWidget->RegisterOnSelectionChange(UItemWidget::FOnSelectionChange::CreateUObject(this, &USearchNfTsWidget::OnItemSelection));
if (ItemData.Listings.Num())
{
ItemWidget->SetListForSellStatus(true);
}
}
}
Example in-game inventory
You can view a full example of an in-game marketplace in the sample game here, which also utilises the Search NFTs endpoint.