question

Paulius Raila avatar image
Paulius Raila asked

Can't get Catalog Items in cloud script.

So I'm trying to iterate through catalog items, but i'm unsuccessful, don't know what I'm doing wrong, it throws error as soon as I try to do something with catalogItems[i]. I think I found this example somewhere.

function getRandomAttributes(itemRarity, itemId) {
  
  	var request = server.GetCatalogItems({CatalogVersion: null});
	var catalogItems = request.Catalog; 
	var length = catalogItems.length; 
	var catalogItemInstance;
 
	for(var i = 0; i <= length; i++)
	{	
          if(catalogItems[i].ItemId == itemId){ // It throws error here!
             
          }
                            
	}

}
2 comments
10 |1200

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

Paulius Raila avatar image Paulius Raila commented ·

This is what error I get in client.

There was error in the Cloud Script function generateRandomAttributes:
 Error Code: JavascriptException
 Message: JavascriptException
UnityEngine.Debug:Log(Object)
<>c:<MakePurchase>b__16_1(ExecuteCloudScriptResult) (at Assets/Scripts/Inventory/selectedItemShopPanel.cs:72)
PlayFab.Internal.<>c__DisplayClass23_0`1:<MakeApiCall>b__1() (at Assets/PlayFabSdk/Shared/Internal/PlayFabHttp/PlayFabHTTP.cs:238)
PlayFab.Internal.PlayFabUnityHttp:OnResponse(String, CallRequestContainer) (at Assets/PlayFabSdk/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:249)
PlayFab.Internal.<Post>d__20:MoveNext() (at Assets/PlayFabSdk/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:191)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)



0 Likes 0 ·
Paulius Raila avatar image Paulius Raila commented ·

I execute cloud script after item is purchased in client to modify custom data of item instance. The script in the main post is a function called by another function in cloud script, it will return item attributes that i want to add to custom data, but I need ItemClass to know what attributes should I attach to the item and for that I need to iterate through item catalog to find the correct item and get it's item class.

0 Likes 0 ·

1 Answer

·
v-humcin avatar image
v-humcin answered

It seems that you are trying to access a list element that doesn't exist. You should change the for loop test expression to less than rather than less than or equal too.

for(var i = 0; i < length; i++)
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.