Undefined objects, trigger and self vs. this

0 favourites
  • 4 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • I am working on a plugin that should handle communication to and from a server. My code works in a regular html file but I am having problems getting it to work in a plugin. The errors I get are all related to some kind of ‘undefined’ objects. I have changed self to this and vice versa and moved things around but it will not work. I hope that some of you experienced plugin-developers or javascript developers can see the problem:

    	instanceProto.onCreate = function()
    	{
    	    
    	    this.groupNo = "";
    
    	    // Declare a proxy to reference the hub.
    	    this.connection = $.hubConnection('http://myserver.com/');
    	    this.myHubProxy = this.connection.createHubProxy('myGameHub');
    
    	    this.myHubProxy.on('PrivateGameCreated', function (groupNoString) {
    	        self.groupNo = groupNoString;
    	        this.runtime.trigger(cr.plugins_.MyGame.prototype.cnds.PrivateGameCreated, self);	        
    	    });
    		...
    	};
    	
    	Cnds.prototype.PrivateGameCreated = function () 
    	{
    	    return groupNo;
    	};
    	
    	Acts.prototype.StartPrivateGame = function ()
    	{
    	    this.connection.start().done(function ()
    	    {
                this.myHubProxy.invoke('createPrivateGame');
    	    });
    	};[/code:2hfbp2t5]
  • Maybe add

    var self = this;[/code:2wrt9k4j]
    before use "self" object.
  • I have tried to add “var self = this” in top of onCreate. The result is “cannot read property ‘invoke’ of undefined" (the same result as without "var self = this"). I get the error in this line:

    this.myHubProxy.invoke('createPrivateGame');[/code:3no96rp5]
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Google for some articles about JS "closures" and referencing vars outside of a "callback"

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)