[Solved] Return a value from a triggered promise?

Get help using Construct 2

Post » Mon Jan 23, 2017 6:10 am

I ran into a slight issue trying to use an API that works fine on a normal page but is giving me some issues through C2.

c2runtime.js:16296 Uncaught (in promise) TypeError: Cannot read property 'trigger' of undefined
- at messageEvents.cb (C2runtime)
- at self.provider.getPost.then (API)


The line in question on the C2runtime was:
Code: Select all
self.runtime.trigger(cr.plugins_.PluginName.prototype.cnds.OnProvMessage, self);


Looks like the self modifier there might be applying to the API's listen promise/function, so there might be a better way for this to be executed. An earlier version of the API worked no problem with just BrowserExecJS but due to updates to it using promises, I'm having to delve into the SDK a little to try to make things work. I'm not used to working with Promises so there might be a simple solution I need.


Here's the runtime code for the plugin I'm trying to setup. Currently the only actions are Connect, Listen, Send, with an expression for the message and condition to trigger when a message is received.
Code: Select all
   // called whenever an instance is created
   instanceProto.onCreate = function()
   {
      
      this.msgContent = "";
      
      APIThing.Messages.setProvider('providername', {server: 'localhost', port: 5001});
      
      var self = this;
   };

...
   //////////////////////////////////////
   // Conditions
   function Cnds() {};
   
   Cnds.prototype.OnProvMessage = function ()
   {
      return true;
   };
   
   pluginProto.cnds = new Cnds();
   
   //////////////////////////////////////
   // Actions
   function Acts() {};
   
   Acts.prototype.listenTo = function (topic_)
   {
      APIThing.Messages.listenTo({topic: topic_}).then(function(message,msgInfo) {
         self.msgContent = JSON.stringify(msgInfo);
         self.runtime.trigger(cr.plugins_.PluginName.prototype.cnds.OnProvMessage, self);
      });
   };
   
   Acts.prototype.sendMsg = function (topic_, msg_)
   {
      APIThing.Messages.sendMessage({
         topic: topic_, data: {stuff: ((msg_).toString() + '').toString()}
      });
   };
   
   pluginProto.acts = new Acts();
   
   //////////////////////////////////////
   // Expressions
   function Exps() {};
   
   Exps.prototype.message = function (ret)
   {
       ret.set_string(this.msgContent);
   };
   
   // ... other expressions here ...
   
   pluginProto.exps = new Exps();
Last edited by Thndr on Mon Jan 23, 2017 10:16 pm, edited 1 time in total.
B
21
S
8
G
6
Posts: 346
Reputation: 4,891

Post » Mon Jan 23, 2017 8:00 am

Check the value of "self". It's saying that it can't get "trigger" from undefined.
So my guess is "self" isn't the right value.
self.runtime is probably undefined so you can't get further values.
You can even use the browser debugger to stop at that line so you can inspect the values.
B
91
S
31
G
103
Posts: 5,234
Reputation: 67,754

Post » Mon Jan 23, 2017 2:16 pm

R0J0hound wrote:Check the value of "self". It's saying that it can't get "trigger" from undefined.
So my guess is "self" isn't the right value.
self.runtime is probably undefined so you can't get further values.
You can even use the browser debugger to stop at that line so you can inspect the values.

I think I figured out how to deal with it.

Since the function I'm calling in the API has it's own THIS and SELF tags I had to specify the it outside the trigger call

Code: Select all
Acts.prototype.listenTo = function (topic_)
{
   var c2self = this;
   APIThing.Messages.listenTo({topic: topic_}).then(function(message,msgInfo) {
      c2self.msgContent = JSON.stringify(msgInfo);
      c2self.runtime.trigger(cr.plugins_.PluginName.prototype.cnds.OnProvMessage, c2self);
   });
};


With this code it's returning the JSON info I need :).
B
21
S
8
G
6
Posts: 346
Reputation: 4,891


Return to How do I....?

Who is online

Users browsing this forum: anty21ro and 6 guests