[Solved] Return a value from a triggered promise?

0 favourites
  • 3 posts
From the Asset Store
Game Triggers is as versatile as a Swiss army knife, and will be a great resource for any type of game you're creating.
  • 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.

    [quote:3vw215xn]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:

    self.runtime.trigger(cr.plugins_.PluginName.prototype.cnds.OnProvMessage, self);[/code:3vw215xn]
    
    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:3vw215xn]
    	// 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();
    [/code:3vw215xn]
  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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

    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);
    	});
    };[/code:1a86eokk]
    
    With this code it's returning the JSON info I need .
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)