Why does this return 0.1? - Parse.com plugin

For developers using the Construct 2 Javascript SDK

Post » Mon Aug 25, 2014 7:36 am

Hello

I'm developing a Parse.com plugin for C2, and I get this annoying error.
When I set the value to a variable in C2 using an expressesion, it gets turned into 0.1 for any integer or float.
When I log to the console directly from runtime, it returns the correct result. When I do it using C2, it returns 0.1 in the console log.

This is my code in runtime:
Code: Select all
Exps.prototype.QueryHighest = function (ret,par0,par1,par2)
{
   var Object = Parse.Object.extend(par0);
   var query = new Parse.Query(Object);
   
   // Retrieve the values descending
   query.descending(par1);
   
   query.first({
      success: function(result) {
         var Return = result.get(par2);
         console.log(Return);
         if(isNaN(Return)){
            ret.set_string(Return);
            console.log("set_string");
         }else if(!isNaN(Return)){
            if(isInt(Return)){
               ret.set_int(Return);
               console.log("set_int");
            }else{
               ret.set_float(Return);
               console.log("set_float");
            };
         }else{
            ret.set_any(Return);
            console.log("set_any");
         };
      },
      error: function(error) {
         alert("Error: " + error.code + " " + error.message);
      }
   });
};


And for the edittime:
Code: Select all
AddStringParam("class","Which class does the object lie in?");
AddStringParam("search attribute","The attribute whose value you want to search for.");
AddStringParam("attribute","The attribute whose value you want to get.");
AddExpression(0, ef_return_any, "Parse query: highest result", "Search Query", "QueryHighest", "Returns the highest value from a specified attribute.");


And in C2:
Image
B
4
Posts: 1
Reputation: 202

Post » Mon Aug 25, 2014 12:34 pm

Values must be returned synchronously from expressions. If query.first() is asynchronous, then you return a value after the expression has already returned, which is not allowed. It is really a bad idea to use anything asynchronous in an expression anyway, it doesn't fit the model of how Construct 2 works. Instead you should have an action to start the asynchronous task, a trigger that fires when it completes, and then synchronous expressions that return the results of the task.
Scirra Founder
B
395
S
232
G
88
Posts: 24,371
Reputation: 193,772


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests