loicteixeira / gj-unity-api

[MOVED] Game Jolt API wrapper for Unity.
https://github.com/InfectedBytes/gj-unity-api/
MIT License
16 stars 13 forks source link

Notifications keeps comming up #68

Closed RCTGames closed 7 years ago

RCTGames commented 7 years ago

I don't know if it's my fault.. but when I tell the game to add a trophy (aka make the game add a trophy), it's just comes up, then goes away, comes back up again, gets removed and then comes again.

My code:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;

public class PizzaApi : MonoBehaviour {

    public int GiveTrophy1;
    // Use this for initialization
    void Start () {

        if (GameJolt.API.Manager.Instance.CurrentUser == null)
        {
            GameJolt.UI.Manager.Instance.ShowSignIn((bool success) =>
            {
                if (success)
                {
                    Debug.Log("The user signed in!");
                    GiveTrophy1 = 1;
                    Debug.Log("GiveTrophy1: " + GiveTrophy1);
                }
                else
                {
                    Debug.Log("The user failed to signed in or closed the window :(");
                }
            });

        }

    }

    void Update()
    {
        if (GiveTrophy1 == 1)
        {
            if (GameJolt.API.Manager.Instance.CurrentUser != null)
            {
                var trophyID = 69913;
                GameJolt.API.Trophies.Unlock(trophyID, (bool success) =>
                {
                    if (success)
                    {
                        Debug.Log("Gave Trophy to player!");
                        GiveTrophy1 = 0;
                        Debug.Log("GiveTrophy1: " + GiveTrophy1);
                    }
                    else
                    {
                        Debug.Log("Something went wrong");
                    }
                });
            }
        }
        else { }
    }

}

Video proof: https://www.youtube.com/watch?v=SrlDXq6d5VU

loicteixeira commented 7 years ago

TL;DR; You send GameJolt.API.Trophies.Unlock multiple times.

Longer version: In the Start method, you set GiveTrophy1 to 1. Then in Update, if GiveTrophy1 is equal to 1, you unlock the trophy with GameJolt.API.Trophies.Unlock. Finally, you set GiveTrophy1 back to 0 on the callback but because the callback is only called after the API has contacted the GameJolt servers, it might take a few seconds. During that time, GiveTrophy1 is still set to 1 so you're essentially executing GameJolt.API.Trophies.Unlock every frame.

You can solve it in a couple different ways:

Let me know if that fixes your problem.

FWIW, your video is private and I can't see it.

RCTGames commented 7 years ago

Ye, I thought if I linked the video it would be able to been viewed, but thanks :D

Update: I don't know how that worked, but thanks, tbh I didn't think you would respond. Thank you

loicteixeira commented 7 years ago

Which solution did you pick? What don't you understand? I'm happy to (try to) explain.

RCTGames commented 7 years ago

I choose the second one,

 if (GiveTrophy1 == 1)
    {
          GiveTrophy1 = 0;
          if (GameJolt.API.Manager.Instance.CurrentUser != null)
            { 
                 var trophyID = 69913;
                 GameJolt.API.Trophies.Unlock(trophyID, (bool success) =>
                 {
                     if (success)
                     {
                         Debug.Log("Gave Trophy to player!");

                         Debug.Log("GiveTrophy1: " + GiveTrophy1);
                    }
                    else
                     {
                         Debug.Log("Something went wrong");
                         GiveTrophy1 = 1;
                     }
                 });
             }
         }
         else { }

And I was just supprised that it worked

loicteixeira commented 7 years ago

So, the Start method is run once when the scene loads, the Update method is run on very frame and you scene starts with GiveTrophy1 set to 0.

In the original version of the script

In the Start method, you check if the user is signed in and if he isn't, you show the sign in window. At this point, nothing change. GiveTrophy1 is still set to 0, so nothing happens in the Update method (because of the if (GiveTrophy1 == 1) which is false).

Once the user press Sign-In, the API contacts the GameJolt servers (this might take a while) and then return. If successful, you set GiveTrophy1 to 1.

Now, because of that, the Update method will execute the content of the if statement (because GiveTrophy1 == 1 is now true). The content if this statement run GameJolt.API.Trophies.Unlock() which tries to unlock the trophy. The API contacts the GameJolt servers (again, this might take a while) and then return (success or failure), execute your callback and show a notification. During that whole time, the GiveTrophy1 == 1 condition is still true and the GameJolt.API.Trophies.Unlock() call gets executed on every frame, until the servers finally respond, execute your callback where you set GiveTrophy1 to 0 which stops the loop.

In the end, the GameJolt.API.Trophies.Unlock() call got executed multiple times, therefore it will show multiple notifications.

By adding GiveTrophy1 = 0; right after the if, you're essentially saying "Ok, I understand I should unlock the trophy, I'll take care of that, set GiveTrophy1 to 0 so on the next frame I don't run this code again (because I'm already doing it asynchronously)".

Later, after Debug.Log("Something went wrong"); by adding GiveTrophy1 = 1;, you're essentially saying "Bugger this didn't work, set GiveTrophy1 to 1 again so on the next frame, I'll retry`.

Hopefully that makes sense. Good luck with your game.