pythonpython-3.xoopkivyrewardedvideoad

How to reward user points in kivy from rewarded video ads


I am trying to solve a problems from Weeks and I have to finally ask question on this website.

Problem :-

When the user plays the video Ad the points are increased and user is rewarded. But when user purchase something from points then the points are not decreasing.

From My Understanding it is a Object oriented problem.

See This code Snippet :

Python Code:

class PlayScreen(Screen):

# Function for display sneak peak (Ad Advantage)
    def Purchase_For_points(self):       #This FUnction will be called when user purchases some hint
        // Irrelevant Code
        app = MDApp.get_running_app()
        app.Num -= 1                     #After Purchase Deducted 1 Point

    def VideoAdPopup(self):
        video_dialog = MDDialog(title = "Watch an Ad",text = "Watch a small video Ad In Exchange of Reward",
                       size_hint = [1,0.3],auto_dismiss = False,text_button_ok = "Ok",text_button_cancel = 
                       "Cancel",events_callback = self.videocallback)

        video_dialog.open()

    def videocallback(self,text_of_Selection,popup_widget):  #Popup Function for rewarded ads
        if(text_of_Selection == "Ok"):
            SlipsApp.ads.show_rewarded_ad()     #Ad is Shown and rewards will be handled accordingly
        else:
            print("User did not want to watch ad")


class SlipsApp(MDApp):

    # These are our Admob Ad IDs
    APP = "ca-app-pub-XXXXXXXXXXXXXXXXXXXX"
    BANNER = "ca-app-pub-XXXXXXXXXXXXXXXXXXXX"
    INTERSTITIAL = "ca-app-pub-XXXXXXXXXXXXXXX"
    REWARDED_VIDEO = "ca-app-pub-XXXXXXXXXXXXXXXXX"
    TEST_DEVICE_ID = "3C91BAC5C088815B62389497AC1E309D"
    # Creating Ad Instance
    ads = KivMob(APP)

    #Number oF Rewards(Points)
    Num = NumericProperty(5)       #Variable for points

    def __init__(self, *args,**kwargs):
        self.theme_cls.theme_style = "Dark"
        super().__init__(*args,**kwargs)
        self.reward = Rewards_Handler(self)

    #Build Function
    def build(self):
        # Loading Ads
        self.ads.add_test_device(self.TEST_DEVICE_ID)
        self.ads.new_banner(self.BANNER, False)
        self.ads.new_interstitial(self.INTERSTITIAL)
        self.ads.request_banner()
        self.ads.set_rewarded_ad_listener(self.reward)
        self.ads.load_rewarded_ad(self.REWARDED_VIDEO)
        self.ads.show_banner()
        return intro()

#Class For Handling Rewards Callback Functions
class Rewards_Handler(RewardedListenerInterface):
    def __init__(self,other):
        self.game = other

    #Overriding Rewards Callback Functions
    def on_rewarded(self, reward_name, reward_amount):
        self.game.Num += 1                  #Reward given 1 Point
        print("User Given 1 reward")

Kivy Code: -

<PlayScreen>:
    name: 'PlayScreen'

    MDLabel:
        text:"  Points : "+str(app.Num)    #Showing Points On Screen
        pos_hint:{"top":1.35}

Solution

  • Yes, you are right this is a Object Problem. In the function Purchase_For_points() in PlayScreen class. You are again creating the object for accessing App class.This will copy all the attributes in the new object and decrease the Num each time in a new object of Running App.

    And, What is shown on the screen is the Num variable of main app object that is created on app startup. So, For decreasing and increasing the Num variable you must have access to the main app object.

    In your code I see you have given the Access of App Class to Rewards_Handler class by init method . So in the Rewards_Handler class game object is same as Main app object.

    create a different function in Rewards_Handler class to decrement points as this is the class which handles the reward related things

    class Rewards_Handler(RewardedListenerInterface):
        def decrementReward(self):
            self.game.Num -= 1
    

    For accesing this function from different classes you can create a function in App Class It must be static method so that you will not create another object and trap in objects

    class SlipsApp(MDApp):
        @staticmethod
        def decrement(element): #The element will be the main app object.
            element.reward.decrementReward()
    

    Now for getting access to class in which you want to use this function to decrement. Simply add a init() Method in class

    class PlayScreen(Screen):
        def __init__(self, **kw):
            super().__init__(**kw)
            self.app = MDApp.get_running_app() #this is main app object
    
        def Purchase_For_points(self):
            SlipsApp.decrement(self.app) #passing main App object.