PlugIn Development - Javascript Functions Scope

0 favourites
  • 14 posts
From the Asset Store
You can become a REAL game developer. Learn the basics and get resources that will let you get profits from your game!
  • Hello all, I am new to construct and I am developing a plugin to solve a problem I have.

    I am implementing a Push Notifications Plugin for android HTML5 games. In fact, I have it working using google push notifications service. But not the way I think a plugIn must be done.

    I wrote the plugin using the JS SDK provided by scirra. But I hit a wall when I needed a javascript function that can be call from outside the application, via the Cordova Push Notifications Plugin (https://github.com/phonegap-build/PushPlugin.git).

    That (and all push services) requires that the applications needs to be accessible (via a JS function) from the Device OS (Android).

    In my implementation I had to include an external JS file into the index.html, where the "onNotification" function is defined. If I define that same function inside the plugin, it does not work.

    In order to be a proper plugin, it must be autonomous and self contained. Or at least it must be Construct 2 itself who includes that file.

    So my question is:

    Plan A - How or where (inside the plugin) do I define a javascript function that has the same scope that the ones defined in the index.html file?

    Plan B - How do I define a plugin that once the game is exported to Cordova or Phonegap, Construct 2 itself imports the needed JS files and writes the <script> tag including it in the index.html file.

    Thank you very much for your help!

  • To add a global function that won't be renamed by closure compiler, just use:

    window["myGlobalFunction"] = function () { ... };

  • If you need to use "Plan B" you can add the JS file name in the "function GetPluginSettings()" in edittime.js

    This is typically needed for JS-API based libraries when you want to keep it in a local file. My setting for ParseFE is:

    "dependency":	"parse-1.3.1_fe_1.js",
    [/code:1gdvujte]
    For the Push functions in ParseFE I listen for events from the Cordova plugin by using a construct like this:
    [code:1gdvujte]document.addEventListener('onParsePushReceived', function (pushBundle) {
    }
    [/code:1gdvujte]
  • Thank you very much for your quick answers.

    I will test both options as soon as possible.

    Thanks!!

  • [attachment=0:1wx0ps4l][/attachment:1wx0ps4l]

    To add a global function that won't be renamed by closure compiler, just use:

    window["myGlobalFunction"] = function () { ... };

    Hello Ashley, thanks for your reply.

    I am testing your solution, and it seems to work. the problem I have now is to access the plugin properties from within the global function.

    I mean, how can I do this:

    window["onNotification"] = function (e) {

    this.lastMessageReceived = e.payload.message;

    this.runtime.trigger(cr.plugins_.pushNotifications.prototype.cnds.OnPushMessageReceived, this);

    }

    Of course I have defines lastMessageReceived and OnPushMessageReceived properly. But the error says "this is not defined".

    I guess that this is related to the global status of the function. But how can I get the "self" object or access its properties from inside that function.

    The process in general works fine. But I want to be able to catch the Push Messages from construct itself, and so stuff with it.

    Thanks!!

  • This is really just a normal JS programming question, and this isn't really the place to ask that kind of thing. You just need to define the function in a scope where you can access the variables you want via a closure. Typically you have something like "var self = this" in the enclosing scope and then you refer to "self" instead of "this" in the function body. If you have other general JS programming questions best head to a site like StackOverflow.

  • This is really just a normal JS programming question, and this isn't really the place to ask that kind of thing. You just need to define the function in a scope where you can access the variables you want via a closure. Typically you have something like "var self = this" in the enclosing scope and then you refer to "self" instead of "this" in the function body. If you have other general JS programming questions best head to a site like StackOverflow.

    Thank you for your reply Ashley.

    I asked that, because I already try what you mentioned. I am not familiarized on how Construct 2 compiles the JS, so I am finding hard to debug the code. But I am indeed familiarized with JS.

    If you say that should work, then I will look for another error. Because I defined "self" as a global variable, and asigned self = this inside instanceProto.onCreate. And inside window["onNotification"] = function (e) {} "self" is null.

    I apologize if my question was misplaced. I will try to find my answer elsewhere.

    Kind regards

  • No, you should not define self as a global variable.

    Construct 2 uses Google Closure Compiler to compile JS code, which is separately documented by Google.

  • No, you should not define self as a global variable.

    Construct 2 uses Google Closure Compiler to compile JS code, which is separately documented by Google.

    Thank you very much for your help Ashley. Your information indeed solved my issue.

    Now I have a proper self contained and fully functional C2 plugin for push notifications.

    Thank you very much!

  • vairen

    Hi, are you willing to share your latest version?

    tnx!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • vairen

    Hi, are you willing to share your latest version?

    tnx!

    Of course Inina, I have no problem sharing it.

    The thing is that I updated C2 to r200 and my plugin stop working!!!

    It is driving me crazy, I can not fire conditions with: this.runtime.trigger(cr.plugins_.pushNotifications.prototype.cnds.OnPushMessageReceived, this);

    My condition is defined in edittime.js like this:

    AddCondition(0, cf_trigger, "On Push Message received", "Events API", "On push notification received", "Triggered when a push notification message is received.", "OnPushMessageReceived");

    And in runtime.js like this:

    Cnds.prototype.OnPushMessageReceived = function()

    {

    return true;

    };

    I do not know if there is something wrong with my code (I haven't change anything), or if there is some compatibility issue with the latest version.

    I just put the trigger code line at the top of "instanceProto.onCreate" function and nothing happen. I even commented all the rest of my code.

    Maybe Ashley could help on clarify if there was some change on this.

  • vairen

    Looking forward for that plugin...

    I have one question... how does app handles recieved notifications if its minimised?

    thanx!

  • vairen

    Looking forward for that plugin...

    I have one question... how does app handles recieved notifications if its minimised?

    thanx!

    I have tested only on Android OS, and is the OS itself that gets the notification from Google AGCM Service.

    You need to register with Google in order to be authorized to send AGCM messages, and you need to implement your own server side application that gets tue registration and using the sender ID that google provides you, sneds the message trough google AGCM service.

    For that you need to have apache cordova with this plugin installed: https://github.com/phonegap-build/PushPlugin

    And then my plugin register an event listener that talks with it. Once the device gets a push notification message, the OS sends it to the app and my plugin captures that and fire a C2 condition. And then you can do whatever you want with it.

    I can also provide you with my PHP code, so the only thing you need to do is register with google and put all pieces in place.

    I hope I can solve this soon and I will share it with you. I think there is a timing problem. when the plugin initializes. If I wait a few seconds to fire the conditions it works fine.

    I hope Ashley could be able to clarify that.

    Cheers

  • Any updates on this?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)