Asynchronous function in Cnds (conditions)

For developers using the Construct 2 Javascript SDK

Post » Wed Jan 09, 2013 3:02 pm

@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 :
[code]Cnds.prototype.isThingExist = function (thing)
{
    this.content(thing, function(boolean) {
                   if (condition == true)
                   return boolean;
                 });
};[/code]
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 guyssepteven2013-01-09 15:33:32
B
44
S
12
G
13
Posts: 628
Reputation: 10,471

Post » Wed Jan 09, 2013 7:07 pm

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").
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,528

Post » Wed Jan 09, 2013 7:28 pm

@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.
septeven2013-01-09 20:06:26
B
44
S
12
G
13
Posts: 628
Reputation: 10,471

Post » Wed Jan 09, 2013 11:36 pm

@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...septeven2013-01-09 23:36:59
B
44
S
12
G
13
Posts: 628
Reputation: 10,471

Post » Thu Jan 10, 2013 1:05 am

@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.
B
44
S
12
G
13
Posts: 628
Reputation: 10,471

Post » Thu Jan 10, 2013 2:15 pm

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

[code]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);
    });
};[/code]

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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,528

Post » Thu Jan 10, 2013 4:37 pm

@Ashley
Thanks, really.
I guess it's getting to help me, I'm doing some try - I misunderstood sth in your code though:
[code]self.thingResults[thing] = result;[/code]
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?
B
44
S
12
G
13
Posts: 628
Reputation: 10,471

Post » Thu Jan 10, 2013 4:47 pm

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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,528

Post » Thu Jan 10, 2013 5:21 pm

@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 ;)septeven2013-01-10 17:22:08
B
44
S
12
G
13
Posts: 628
Reputation: 10,471

Post » Thu Jan 10, 2013 5:51 pm

@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.
B
44
S
12
G
13
Posts: 628
Reputation: 10,471

Next

Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests