0

I am using rewarded ads of admob and using Action<bool> parameter to return true or false but the given reward increases 1 every time.

public void showRewarded(Action<bool> giveReward)
{
    if (rewardedAd.IsLoaded())
    {
        rewardedAd.Show();
        rewardedAd.OnUserEarnedReward += delegate(object sender, Reward reward)
        {
            giveReward(true);
        };
    }
    else
    {
        giveReward(false);
    }

    requestRewarded();
}

I call this method in different places :

        public void watchAdUpgradeFactory()
    {
        AdsController.Instance.showRewarded(delegate(bool b)
        {
            if (b)
            {
                SoundManager.PlaySomeSoundOnce?.Invoke(SoundType.ButtonClick);
                playerStorageSO.ConcretePlayer.GetResource(ResourceType.Coin, 0);
                concreteFactory.Upgrade();
                ConfigureButton();
                checkdAdButton();
            }
        });
    }
        public void watchAdUpgradeTower()
    {
        AdsController.Instance.showRewarded(delegate(bool b)
        {
            if (b)
            {
               
                upgradeTower();
                ConfigureButton();
                checkdAdButton();
            }
        });
    }

This works as intended for the first time but the giveReward(true) stacks up after every time and player gets 1,2,3,4,5... rewards after consequent claims of reward. How can I fix this or is there a better method of achieving the same goal?

  • 1
    I can suppose, that on each successful IsLoaded call you are subscribing one more event handler... Try to move event subscription outside of method (make `+= delegate` once outside of method, somewhere in the class or constructor/init method) – Jeidoz Dec 22 '22 at 18:27
  • showRewarded is called from different objects with different rewards and giveReward returns to the caller with true or false depending on they earned the reward. I cannot move the adding delegate function to anywhere else because it is using the parameter of the method – SengunSipahi Dec 22 '22 at 19:09
  • But you need only add it once. Which is the important bit. So you need to ensure you only subscribe when you need to – BugFinder Dec 22 '22 at 19:11
  • there are more than 10 possible rewards that user requests through watching an ad how do you suggest me to subscribe them seperately and only 1 time. – SengunSipahi Dec 22 '22 at 20:35
  • 1
    Then you need to remove the last reward choice and replace it with a new one. – BugFinder Dec 22 '22 at 21:07
  • I wanted to do that but i dont know how i can clear all events from the OnUserEarnedReward. – SengunSipahi Dec 22 '22 at 21:25
  • @SengunSipahi First google search gives several ways how to clear all event subscribers: https://stackoverflow.com/questions/153573/how-can-i-clear-event-subscriptions-in-c – Jeidoz Dec 23 '22 at 05:57
  • It doesnt work for admob's events. `The event 'OnUserEarnedReward' can only appear on the left hand side of += or -= (except when used from within the class 'GoogleMobileAds.Api.RewardedAd')` is the error message. – SengunSipahi Dec 23 '22 at 07:35

2 Answers2

0

I'm assuming, based on your post and your comments below it, that you want to clear out the "OnUserEarnedReward" event each time before you subscribe to it again.

You can't clear out an event outside of its owning class. Events are simply made this way: Outside of their owning class, events only let you "subscribe" and "unsubscribe" to them, that's it.

Theoretically speaking, you can forcefully clear out an event through reflection, like in this example. However, that's probably not a very safe solution, and I don't think it's the best way to go about this.

Your best bet in this case would be manually "unsubscribing" the last reward from the event. You can't do this if you subscribe to the event with an anonymous function / lambda expression, however. Even when two anonymous functions look identical, they're still considered different functions.

You could keep a variable holding that delegate, though, and use that variable to subscribe and then unsubscribe from the event.

0

I have added a variable of Action to my ad manager script to keep track of the current reward request and added it to the event handler once at the initialization state. Here is the code snippets if anyone comes accross a similar issue.

    private Action<bool> currReward;
    private void initializeAds(){
        ///initialize ads
        rewardedAd.OnUserEarnedReward += delegate(object sender, Reward reward)
        {
            if (currReward != null)
            {
                currReward(true);
                currReward = null;
            }
       };
    }
    public void showRewarded(Action<bool> rew)
    {
        currReward = rew;
        rewardedAd.Show();
        requestRewarded();
    }