[Solved] Issue withvalue in Expression from promise

For developers using the Construct 2 Javascript SDK

Post » Sun Jan 07, 2018 2:29 am

I currently have this code that sends everything to the console.log just fine, but when it comes back into Construct it always displays the textbox(messagebox) name or C2function name(bothlogs), depending if I'm calling the temp variable or the expression within the C2Function
Code: Select all
   Exps.prototype.getText = function (ret, stuff_)
   {
      var varself = this;
      function.get(stuff_)
      .then(function(content) {
         // .replace to remove control characters from the string
         varself.msgContent = ((content).toString()).replace(/[\x00-\x09\x0B-\x1F\x7F-\x9F]/g, "");
         console.log("Retrieved: " + varself.msgContent);
         return varself.msgContent
      })
      .then(function(text) {
         console.log("Returned: " + text);
         ret.set_string(text);
      })
      .catch(function(err) {
         if(err){
         varself.dataError = (err).toString();
         console.log(varself.dataError);
         }
         varself.msgContent = ("error " + varself.dataError).toString();
         ret.set_string(err);
      });

   };

Image
Image

Theoretically it should return with the same thing the console.log functions produce but I can't seem to get it working. I'm assuming it's something to do with it being a promise rather than a linear function.

My goal is to have the plugin.getText() expression be able to immediately get the text, as currently I have it 'working' with an action/triggered condition combo but that's not ideal for directly applying the text it to object instances on demand. If someone could help me
Last edited by Thndr on Mon Jan 08, 2018 1:57 pm, edited 1 time in total.
B
23
S
10
G
6
Posts: 351
Reputation: 5,151

Post » Mon Jan 08, 2018 12:02 am

After spending quite a few hours trying to work things every which way I've just decided to mimic how AJAX calls are tagged and triggered to ensure I can direct the data where I need to as the asynchronous stuff works fine with condition triggers
B
23
S
10
G
6
Posts: 351
Reputation: 5,151

Post » Mon Jan 08, 2018 11:25 am

Events run synchronously. You cannot use promises in expressions since Construct checks the return value immediately after the function returns. Your expression kicks off a promise which runs asynchronously but the resolve value is never used by anything.

You've already found the solution - use the same model as AJAX where actions kick off asynchronous work and then fires triggers upon completion.
Scirra Founder
B
415
S
247
G
92
Posts: 25,284
Reputation: 200,852


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests