question

laurentiumarianivan avatar image
laurentiumarianivan asked

How to use GetRandomResultTables

Hello,

I have 4 drop tables, one for each rarity (common, rare, epic, legendary), each drop table containing the items corresponding to that rarity. I have another 3 drop tables, each having a specific drop chance:
item_slot_one -> common_items (70%), epic_items (10%), legendary_items (5%), rare_items (15%)
item_slot_two -> rare_items (50%), epic_items (50%)
item_slot_three -> epic_items (33%), rare_items (33%), legendary_items (33%)

Every 24 hours, I want to generate 3 items (one from each item_slot, and all 3 items different from each other) and set them into the User Read Only Data. For now and for testing purposes, this method is called whenever an user logs in. Later on, I will probably use a Scheduled Task to run this cloudscript function (I know it is not very efficient, but this is an MVP and it won't have many players).

Here is my code:

handlers.getRandomDropTableItems = function (args, context)
{
    var first_item = server.EvaluateRandomResultTable({"TableId" : "item_slot_one"});
    var second_item = server.EvaluateRandomResultTable({"TableId" : "item_slot_two"});
    var third_item = server.EvaluateRandomResultTable({"TableId" : "item_slot_three"});
    
    while(first_item.ResultItemId == second_item.ResultItemId || first_item.ResultItemId == third_item.ResultItemId || second_item.ResultItemId == third_item.ResultItemId)
    {
        first_item = server.EvaluateRandomResultTable({"TableId" : "item_slot_one"});
        second_item = server.EvaluateRandomResultTable({"TableId" : "item_slot_two"});
        third_item = server.EvaluateRandomResultTable({"TableId" : "item_slot_three"});
    }
    
    var data = {};
    data["item_one"] = first_item.ResultItemId;
    data["item_two"] = second_item.ResultItemId;
    data["item_three"] = third_item.ResultItemId;
    
    server.UpdateUserReadOnlyData({"PlayFabId" : currentPlayerId, "Data" : {data: JSON.stringify(data)}});
    
    return { messageValue: data};
    
};

As you can see in my snippet, I am getting each item from those item_slot tables and setting them in User Read Only Data. The problem is that the server API calls exceed my limit (which is 5, and the calls made are usually 6). I want all those 3 items to be different from each other, and that is why the while is there.

From a previous post, I understand that I need to use GetRandomResultTables. But I am not exactly sure how (I looked through the API documentation and a few examples, but it is not exactly clear for my case). Could you provide me a code snippet or some hints/ more details about using GetRandomResultTables in my case?


Thanks again for all the help!
apisCloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Citrus Yan avatar image
Citrus Yan answered

GetRandomResultTables retrieves the configuration info for the specified drop tables, which includes all ItemId values and weights for each table, you can use such info to determine which items should be given to the players, basically you’d need to simulate the “dropping” process on your own. Here is the basic flow:

1. Use GetRandomResultTables to retrieve the config info of all the drop tables that'll be used : common_items, epic_items, legendary_items, rare_items, item_slot_one/two/three.

2. Based on the the ItemId values and their weights, determine the items that should be given to the player, and, instead of using the while loop to check duplicate items after all three tables are opened, you might as well generate items one by one, and add generated items into a Set:Set - JavaScript | MDN (mozilla.org), when the newly generated item is already in the set, regenerate the item again.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.