question

anudeep avatar image
anudeep asked

How do I access my player currency through a cloud script function? I want to display the currency my player has on the screen all the time.

As the title suggests, my game in UNITY is a choice based decision game. I have made cloud function for adding currency and reducing currency by 1 and run a for loop to "spend the currency" for example for(i=0;i<20;i++)

{

reduceCurrency();

}

This doesn't seem to work though, sometimes it only deducts 9, sometimes 10 from the playfab's player currency.

Maybe if i can actually just access the currency into my script, I can put conditions for it with the currency in my UI? which isn't the same as of now. I initialize my UI currency with the same value that i put into my player's through a cloudscript function.

How can I connect my UI to the playfab ? How can I pass in values to the cloudscript to add/subtract certain amount from the wallet?

How do I implement Game Currency into my cost based decision making game ?

CloudScriptIn-Game EconomyCharacter Data
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

·
Made Wang avatar image
Made Wang answered

Are you using Cloud Script Classic or Azure Function Cloud Script? Azure Function Cloud Script is recommended.

In Azure Function, you can use AddUserVirtualCurrency and SubtractUserVirtualCurrency to increase or decrease virtual currency, and use GetPlayerCombinedInfo to get virtual currency.

On the client side, you can use GetPlayerCombinedInfo to get virtual currency, use ExecuteFunction to call Azure Function Cloud Script, and pass in the type of virtual currency and the value of increase or decrease as parameters when calling.

13 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.

anudeep avatar image anudeep commented ·

Okay! So you recommend me to switch to Azure Function Cloud Script? Can't I achieve the same with the Cloud Script Classic ?

0 Likes 0 ·
Made Wang avatar image Made Wang anudeep commented ·

In fact, we recommend using Azure Function Cloud Script.It has many advantages, you can refer to Announcing CloudScript using Azure Functions is now GA (playfab.com) to learn more. Of course, you can also call the above APIs on Cloud Script Classic. Note that ExecuteCloudScript is used on the client side to call Cloud Script Classic.

0 Likes 0 ·
anudeep avatar image anudeep Made Wang commented ·

Okay! I have been looking up at azure functions for the last 3 days and I am tad bit confused about where I should be adding my currency? In the azureFunction.cs ? or in my azureFunctionHandler.cs which in turn calls the my azureFunction?

The video tutorial is quite bit confusing.

0 Likes 0 ·
Show more comments
anudeep avatar image anudeep commented ·

What I mean is, following the HelloWorld Function, I had come to written a small addCurrency function.

But using PlayFab.Plugins.Cloudscript isn't being detected, the error is stated below.

The type or
namespace name 'Plugins' does not exist in the namespace 'PlayFab' (are you missing an assembly reference?)

I have used Visual studio 2019 for most of my game code and VS code doesn't help me at all in coding or pointing out errors, which has been real difficult.

0 Likes 0 ·
anudeep avatar image anudeep commented ·
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using PlayFab.Plugins.CloudScript;
using PlayFab.DataModels;
using PlayFab.ServerModels;
using PlayFab.Json;
using System.Collections.Generic;

namespace Buffbaer.Economy
{
    public static class AddCurrency
    {
        [FunctionName("AddCurrency")]
        public static async Task<dynamic> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());

However I am now getting, CS0246: The type or namespace name 'FunctionExecutionContext<>' could not be found (are you missing a using directive or an assembly reference?)
0 Likes 0 ·
Made Wang avatar image Made Wang anudeep commented ·

Your code does not use the methods under the PlayFab.Plugins.CloudScript namespace, and if you use the PlayFabFunctionContexts, it is obsolete, we would recommend you not to use it.

FunctionExecutionContext is a method in CS2AFHelperClasses, you need to add reference: using PlayFab.Samples;

If you want to call the API in PlayFab, you should also add a reference to it: using PlayFab;

Other than that, all you need to do is to complete the necessary environment configuration by referring to PlayFab CloudScript using Azure Functions Quickstart Guide - PlayFab | Microsoft Docs.

0 Likes 0 ·
anudeep avatar image anudeep Made Wang commented ·

However I am unable to configure the environment as PlayFabAllSDK and PlayFabCloudScriptPlugin aren't being added by NuGet Package Manager. PlayFabAllSdk isn't compatible.

0 Likes 0 ·
allsdk.png (67.4 KiB)
Show more comments
anudeep avatar image anudeep anudeep commented ·

I have configured every thing I could, except the playfab sdk and plugin as per the document.

I have changed the directory for my Azure Function app as well, and still no change. What shall I be installing again ?

0 Likes 0 ·
Made Wang avatar image Made Wang anudeep commented ·

In fact, I can't reproduce your problem, dotnet add package should be like this under normal circumstances.

I suggest you to look for some professional support on .Net and Nuget.

0 Likes 0 ·
Show more comments

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.