Asynchronous function in Cnds (conditions)

  • Ashley or other may help me on this problem.

    I'm aware that it could be a general javascript question (please don't forget that I've been developing in JS for only one year), but I stay totally stuck on this.

    I'm developing a new plugin for C2 and need to code a condition like this :

    Cnds.prototype.isThingExist = function (thing)
    {
        this.content(thing, function(boolean) {
                       if (condition == true) 
                       return boolean;
                     });
    };

    So I'm aware that I can't return the boolean since it's from an asynchronous function.

    So my question is, how to return the boolean value to isThingExist(); ?

    Thanks for helping guys

  • You can't use asynchronous functions like this in conditions, because they have to return the correct answer immediately.

    The solution is to move the asynchronous-ness to event triggers, like the AJAX object does. So instead of making a "Is thing true" condition, you'd make a "Check thing" action which then triggers "On thing result", where you can then use a "Is thing" condition (that just returns a variable returned from the result of "Check thing").

  • Ashley

    Ouch, this part in your Ajax plugin seems really difficult to understand for me ... :(

    But even if I'll understand it, I'm always wondering how I will be able to return this boolean in a "Check thing" action...

    If you have time, I could send you this part of my code in order to make you understand exactly what I want to do (I'm adapting an external library so I depend of this library)

    Edit. Ash, I sent you a personal email since my plugin isn't released yet, but I'll give the solution asa I'll be able to correctly understand this.

  • Ashley

    I'm looking for a solution, again and again...

    It seems your AJAX code uses the acts to fire the doRequest().

    But its conditions are linked to its actions (request - on complete/on error ...).

    For me, it has to check if a file exists, so when have I to fire the action which'll trigger the event...

  • rexrainbow Pode

    Sorry for the alert guys, but if you have some help about how to succeed with asynchronous functions and construct2 conditions/expressions, please don't hesitate :)

    Sorry for multipost, but I'm just stuck and it's really frustrating me.

  • septeven - the code template would be something like this:

    Cnds.prototype.onThingResult = function (thing)     // trigger
    {
         return this.triggeringThing === thing;
    };
    
    Cnds.prototype.isThing = function (thing)
    {
         return !!this.thingResults[thing];
    };
    
    // note this is now an action
    Acts.prototype.checkThingExists = function (thing)
    {
         var self = this;
         
        this.content(thing, function(result)
         {
              // need to associate result with 'thing' somehow
              // (could do this differently but this is just for example)
              self.thingResults[thing] = result;
              
              self.triggeringThing = thing;
              self.runtime.trigger(/*...*/ onThingResult, self);
        });
    };

    In events you would then use the 'Check thing exists' action, which then triggers 'On thing result' where you can use the 'Is thing' condition to test the result.

  • Ashley

    Thanks, really.

    I guess it's getting to help me, I'm doing some try - I misunderstood sth in your code though:

    self.thingResults[thing] = result;

    why is this for ?

    Other thing, in my mail, I talked you about the expression things too.

    Have to do the same kind of thing?

  • Can we keep it all in the forum so other developers can learn from the discussion?

    thingResults is just an ordinary javascript object, set it on startup like "this.thingResults = {};". Then you can use it like a map of thing to result by creating different properties on that object.

    You just have to do the same thing for expressions: use an action to request the data, then trigger a condition when the result is available, and in that event the user can use the expression to retrieve the result. So it's identical but instead of returning true/false in a condition you set the return value via ret.

  • Ashley

    About your first request, of course we can !

    About your answer, first of all, thanks to care about this. I will try with this and 'll tell you what happens ;)

  • Ashley

    Gotcha! for the conditions :)

    I have to work with expressions now - Thank you so much Ashley, without your help, the plugin won't never come !

    Thanks again.

  • Ashley

    I begin to understand correctly this way of working, I want to thank for that. My expressions work perfectly now. There is something like a SDK bug though, let me know what do you think about it.

    My expression checks (before returning something) if the file exists by checking some var. After that, either it returns the string, either it sends a log to the console.

    It works perfectly, BUT, if the file doesn't exist, instead of doing nothing it returns the 'path' variable O_o ....

    Here is my actual code:

    Exps.prototype.getFileContent = function (ret, path) 
    {
       if (this.triggeringFile == path && this.error_ret != 404) 
             { ret.set_any(this.fileContent); } 
       else 
             { cr.logexport ("The file may not exist."); return false; };
    };
  • septeven - your code is wrong. "return false" makes no sense in an expression - you must *always* return a value through 'ret'. In that case you should say ret.set_int(0) or some other value otherwise you'll leave undefined data in the return value.

  • Ashley, it works perfectly now :)

  • Try Construct 3

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

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

    Here is the dropbox plugin, thanks for your help !

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