Import PlayerIO API-> _pio is not defined; pure JS file works

0 favourites
  • 5 posts
From the Asset Store
2D fighting template based in the game that defined the fighting games genre.
  • I try to import PlayerIO.com API into C3 but if I import the script and run the game it throws an error: _pio is not defined.

    But _pio is defined in their script as:

    if (typeof (_pio) == 'undefined') { _pio = {} }
    

    If I put var _pio={} It gives another error in the file.

    I don't understand why it gives this error.

    The sample file from playerio in pure JS works with no problems.

    Full file:https://pastebin.com/2RKVZpta

  • Does it rely on a library being loaded first or is that the script entirely?

    Scripts in construct run in “strict mode” which is more restrictive with what you can do in js. Presumably without strict mode assigning a value to an undefined identifier will just create a new variable, but I haven’t tested.

    construct.net/en/make-games/manuals/construct-3/scripting/using-scripting/javascript-construct

    Some random thoughts at least. I don’t mind js but haven’t used it much within construct because it adds an extra layer of complexity sometimes.

  • From what I see in the js file, it tries to load an external file. Would that be a problem?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It must be an ancient library. It relies on "sloppy mode" global assignments, where you can merely write myvar = {} and have it create a global variable, as opposed to var myvar = {} or globalThis.myvar = {}. "Sloppy mode" was essentially retired with "strict mode" JavaScript which was introduced around 2011 if I remember right. JavaScript Modules always run in strict mode, so you have to adapt such old scripts to work correctly in strict mode.

    The easiest fix is to replace the line if (typeof (_pio) == 'undefined') { _pio = {} } with var _pio = globalThis._pio = {};. This explicitly creates a global variable named _pio, and also creates a variable scoped to the file named _pio, which the rest of the script expects. Then you need to fix a few other cases where it just assigns to something without declaring it as a variable - putting var before those appears to then fix it and have it load successfully.

  • Thanks Ashley.

    I managed to modify the files to make the connection work.

    If anyone is interested here are the changes:

    1. line 1

    2. line 172:

    //PlayerIO={

    var PlayerIO = globalThis.PlayerIO={

    3. line 556: //__pio_flashfallback_callback__ = function () {

    var __pio_flashfallback_callback__=globalThis.__pio_flashfallback_callback__ = function () {

    4. line 861:

    //PlayerIOError = function (code, message, stack) {

    var PlayerIOError =globalThis.PlayerIOError= function (code, message, stack) {

    5. line 913

    //PlayerIOErrorCode = {

    var PlayerIOErrorCode= globalThis.PlayerIOErrorCode={

    6 line 188:

    //authenticate: function (gameId, connectionId, authenticationArguments, playerInsightSegments, successCallback, errorCallback) {

    authenticate: window.myGlobalFunction = function (gameId, connectionId, authenticationArguments, playerInsightSegments, successCallback, errorCallback) {

    7 line 2649:

    //this.useSecureConnections = false;

    this.useSecureConnections = true;

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