Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Actions and events that get consumed on use

Discussion in 'Scripting' started by Suduckgames, Sep 14, 2018.

  1. Suduckgames

    Suduckgames

    Joined:
    Nov 28, 2016
    Posts:
    218
    Hi!

    Sometimes when I am designing my system, I want that some actions and events get consumed on use but I not finding a good solution, for example:

    My game controller wants to listener in the ads Manager if the ad has been OK or KO, in order to ressurrect the player or continue with the "final score". I do it like that
    Code (CSharp):
    1.  
    2.     //This is on the AdsManager
    3.     public event System.Action OnAdFinishedOk;
    4.     public event System.Action OnAdKo;
    5.    //
    6. // This is on GameController
    7. adsManager.OnAdFinishedOk += Revive;
    8. adsManager.OnAdKo += EndGame;
    however I need to manually unsucribe for both events in the Revive and EndGame functions, but I also need to take into account if the Ads has been loaded( among others) in order to unsuscribe.

    That can lead to strange bugs where I forget to unsucribe from a listener and I will like to have something that unsuscribe both actions when something consume one of the actions

    Currently I came with

    Code (CSharp):
    1. public void Register(Action action) {
    2.     Action _action = null;
    3.     _action = ()=> {
    4.         action();
    5.         OnAdFinishedOk -= _action;
    6.     }
    7.     OnAdFinishedOk += _action;
    8. }
    But this is only valid if you want to consume 1 action, and make the register unsuscribe for two actions will break the flexibility.

    Any ideas are apreciate
     
  2. SkaredCreations

    SkaredCreations

    Joined:
    Sep 29, 2010
    Posts:
    296
    I wouldn't unregister automatically in the Register method, since as you said this would break flexibility.

    Instead you could create methods for RegisterHandlers and UnregisterHandlers to be called properly as you need, for example:

    Code (CSharp):
    1. void RegisterHandlers() {
    2.     adsManager.OnAdFinishedOk += Revive;
    3.     adsManager.OnAdKo += EndGame;
    4. }
    5.  
    6. void UnregisterHandlers() {
    7.     adsManager.OnAdFinishedOk -= Revive;
    8.     adsManager.OnAdKo -= EndGame;
    9. }
    10.  
    11. void Revive() {
    12.     UnregisterHandlers();
    13.     // ...do stuff...
    14. }
    15.  
    16. void EndGame() {
    17.     UnregisterHandlers();
    18.     // ...do stuff...
    19. }
    20.  
     
    Suduckgames likes this.
  3. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Is this actually how one would implement commercial ads in the video game?
     
  4. Suduckgames

    Suduckgames

    Joined:
    Nov 28, 2016
    Posts:
    218
    Interesting, I just tested to Unregister events that wasn't added and It just don't crash, so I guess that is the best choice

    It depens on your architecture, In my case, since I have differents ads types and I want to test differents ads , I use suscribe/unsuscribe so when I add a new ad type, I don't need to change anything in the ad part that can lead to bugs. Just sucribe to that events . It increase the complexity but give you more flexibility
     
    MisterSkitz likes this.
  5. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Have any good links where I can read up more on implementation of ads? I'd like to do a user-initiative where they earn a perk for watching an ad but don't have to if they chose not to system. My website I'm getting ready to design later this month, I wouldn't mind placing some ads besides google ads. I don't know where to look for resources though.
     
  6. Suduckgames

    Suduckgames

    Joined:
    Nov 28, 2016
    Posts:
    218
    I had made the system by myself so I don't have any link to help you sorry, however. Every ad company have a "best practices" page, you can check the unity ads and google admob best practices web