Forum rules - please read before posting.

Copy or get the item value from an external script.

Hello everybody. I have a script that is part of a monetization package, does it have the item as coins that are included per payment? Is there a way to use AC to copy the currency value in that script to an inventory item or variable?

Comments

  • Variable values can be updated through script - see the Manual's "Variable scripting" chapter for details, as well as the front page of the Scripting Guide.

    A Global Integer, for example, can be update with:

    AC.GlobalVariables.SetIntegerValue (int id, int newValue);
    
  • AC.GlobalVariables.SetIntegerValue (int id, int newValue);

    https://uploaddeimagens.com.br/imagens/Gw3Z1-Y

    Hello. I don't know how to insert the code that copies the value of the coins without giving an error.

    public void RewardUser()
    {
    if (debugMode) Debug.Log("[MonetizationManager] RewardUser:" + selectedRewardType.ToString());

        switch (selectedRewardType)
        {
            case REWARD_TYPE.COINS_5:
                AddCoins(5);
                break;
    
            case REWARD_TYPE.COINS_17:
                AddCoins(17);
    
                break;
            case REWARD_TYPE.COINS_42:
                AddCoins(42);
                break;
        }
    }
    //-----------------------------------------------------------
    public void AddCoins(int coinsToAdd)
    {
        if (debugMode) Debug.Log("[MonetizationManager] AddCoins: " + coinsToAdd);
        int coins = PlayerPrefs.GetInt("COINS");
        coins += coinsToAdd;
        PlayerPrefs.SetInt("COINS", coins);
        PlayerPrefs.Save();
        OnCoinsChanged?.Invoke(coins);
    }
    
    //-----------------------------------------------------------
    public void OnPurchaseComplete(Product product)
    {
        if (debugMode) Debug.Log("[MonetizationManager] OnPurchaseComplete: " + product.ToString());
    
        if (product.definition.id.Equals("coinspack1"))
        {
            AddCoins(100);
        }
        else if (product.definition.id.Equals("coinspack2"))
        {
            AddCoins(500);
        }
        else if (product.definition.id.Equals("stage2"))
        {
            PlayerPrefs.SetInt("PURCHASED_STAGE2", 1);
            PlayerPrefs.Save();
        }
        else if (product.definition.id.Equals("removeads"))
        {
            PlayerPrefs.SetInt("PURCHASED_REMOVEADS", 1);
            PlayerPrefs.Save();
            HideBanner();
        }
    
        if (OnPurchased != null)
        {
            OnPurchased(product.definition.id);
        }
    }
    
  • There's no mention of AC in the code-block above, but your screenshot has a conflict between UnityEngine and AC for the Button class.

    Remove the using AC; line from the top of your script - the "AC." at the start of my example line will be equivalent.

  • edited June 2023

    public void RewardUser()
    {
    if (debugMode) Debug.Log("[MonetizationManager] RewardUser:" + selectedRewardType.ToString());

        switch (selectedRewardType)
        {
            case REWARD_TYPE.COINS_5:
                AddCoins(5);
                break;
                AC.GlobalVariables.SetIntegerValue(int id, int newValue);
            case REWARD_TYPE.COINS_17:
                AddCoins(17);
                break;
                AC.GlobalVariables.SetIntegerValue(int id, int newValue);
            case REWARD_TYPE.COINS_42:
                AddCoins(42);
                break;
                AC.GlobalVariables.SetIntegerValue(int id, int newValue);
        }
    }
    //----
    
  • Remove the using AC; line from the top of your script - the "AC." at the start of my example line will be equivalent

    I removed and added the code but it shows about 12 errors.

  • two mistakes that repeat

    Assets\TripliceReal2.0\ScriptsMonetization\MonetizationManager.cs(327,60): error CS1525: Invalid expression term 'int'

    Assets\TripliceReal2.0\ScriptsMonetization\MonetizationManager.cs(327,64): error CS1003: Syntax error, ',' expected

  • Don't copy the code verbatim - you need to replace int id and int newValue with the ID number of the Variable and its new value respectively. It also needs to go above the break; statement.

    Since you're already running an AddCoins function for each case, though, it's best to place it in there instead. Add this to the end of your AddCoins function:

    AC.GlobalVariables.SetIntegerValue(10, coins);
    

    This time, replace "10" with the ID of the Global Integer variable to sync the "coins" value with.

  • It worked, however the variable line only sets the coin value not increasing the value with each click. I would need to increase the value on every click.

  • AC.GlobalVariables.GetVariable (10).IntegerValue += coins;
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.