disconnect

Game development with Board Game Arena Studio
User avatar
garcia1968
Posts: 24
Joined: 03 April 2013, 16:03

disconnect

Post by garcia1968 »

Searched online, but having trouble finding a working way to do dojo.disconnect elements in an earlier connect statement like below. Say there are pieces that are available to select in the first state of the game, that in a later state are visible, but no longer selectable/clickable.

Code: Select all

dojo.query( '.square_piece' ).connect( 'onclick', this, 'changePiece' );

Code: Select all

dojo.query( '.square_piece' ).disconnect()
seems like the intuitive choice, but can't get that or variations using foreach and nodelist working correctly.

Thanks
User avatar
Rudolf
Posts: 566
Joined: 24 December 2011, 23:04

Re: disconnect

Post by Rudolf »

var connections = dojo.query($requete).map( function( zenode ) { return dojo.connect( zenode, "onclick", zefunction); });

And to disconnect:
dojo.forEach(connections, function(zeconn) { dojo.disconnect(zeconn); });

in your case:

var connections = dojo.query('.square_piece').map( function( zenode ) { return dojo.connect( zenode, "onclick", 'changePiece'); })
User avatar
garcia1968
Posts: 24
Joined: 03 April 2013, 16:03

Re: disconnect

Post by garcia1968 »

merci beaucoup for the help. I tried swapping my existing connect with the suggested line, but I am getting the following error.

Javascript error: during pageload undefined no_stack_avail
User avatar
Rudolf
Posts: 566
Joined: 24 December 2011, 23:04

Re: disconnect

Post by Rudolf »

Arf, for the stack, it's something that I had in the beginning of BGA dev,
did you declare the 'var connections' in right place?
lets try "var connections = new Array(); " in the globals,
then use 'this.connections = ' ... it should solve?
User avatar
garcia1968
Posts: 24
Joined: 03 April 2013, 16:03

Re: disconnect

Post by garcia1968 »

Tried moving the connections var instantiation to the globals section as suggested, but still same error.

Code: Select all

function (dojo, declare) {
    return declare("ebg.isaac", ebg.core.gamegui, {
        constructor: function(){
            console.log('isaac constructor');
              
            // Here, you can init the global variables of your user interface
            // Example:
            // this.myGlobalValue = 0;

	    var connections = new Array();
User avatar
Rudolf
Posts: 566
Joined: 24 December 2011, 23:04

Re: disconnect

Post by Rudolf »

Without var, and better put 'this.connections' instead 'connections'

and in later code, let's use 'this.connections'
User avatar
garcia1968
Posts: 24
Joined: 03 April 2013, 16:03

Re: disconnect

Post by garcia1968 »

same error-

I have also tried:

this.connections = [];

How is your code written where it is working on your end?
User avatar
Rudolf
Posts: 566
Joined: 24 December 2011, 23:04

Re: disconnect

Post by Rudolf »

I've tried, it's working with this example:
in constructor:

Code: Select all

                                                               this.param=new Array();
							       this.colors= new Array('WHITE','YELLOW','RED','GREEN','BLUE');
							connections =new Array();
in setup:

Code: Select all

// Setup game notifications to handle (see "setupNotifications" method below)
			            	this.setupNotifications();
					this.connections = dojo.query('_r_c').map( function( zenode ) { return dojo.connect( zenode, "onclick",this, 'OnClickManagement'); });
					dojo.forEach(connections, function(zeconn) { dojo.disconnect(zeconn); });
			            	console.log( "Ending game setup" );
User avatar
garcia1968
Posts: 24
Joined: 03 April 2013, 16:03

Re: disconnect

Post by garcia1968 »

Still same error(included the 'this' argument which wasn't in the previous examples), but I did notice that if I don't use class dot notation(like dojo.query('.piece') in the query like below, it won't work, but it also wont' raise an error(perhaps because it doesn't find any elements)

Code: Select all

this.globConnections = dojo.query('piece').map( function( zenode ) { return dojo.connect( zenode, "onclick", this, 'changePiece'); });
In your sample code, the setup connections are immediately disconnected - not sure that '_r_c' is finding anything - if you had an example where it found something that you could test-click successfully against and then disconnect afterwards that might be a better test.

Greg, Een - any working examples from existing games?

If not, I can brute-force a poorer solution by destroying the old elements/connections and swapping in a new set of elements/connections.

Thanks
User avatar
Een
Posts: 3861
Joined: 16 June 2010, 19:52

Re: disconnect

Post by Een »

Hi,

In the next version of the studio, the following methods will be available by default (contribution by Tirix included in the framework). In the meantime, you can copy them inside your code to use them.

In your js constructor :

Code: Select all

                // Array of current dojo connections
                this.connections = [];
In your js as functions :

Code: Select all

         /**
         * Utility to connect and disconnect a single element to/from an event.
         */ 
        connect: function(element, event, handler)
        {
        	if(element == null) return;
        	this.connections.push({
        		element: element,
        		event: event,
        		handle: dojo.connect(element, event, this, handler)
        	});
        },
        
        disconnect: function( element, event )
        {
        	dojo.forEach(this.connections, function(connection) {
        		if(connection.element == element && connection.event == event)
        			dojo.disconnect(connection.handle);
        	});
        },

        /**
         * Utility to connect an event to all elements of the same css class.
         */
        connectClass: function(className, event, handler)
        {
        	var list= dojo.query("."+className);
        	for(var i=0;i<list.length;i++) {
	        	var element = list[i];
        		this.connections.push({ 
	        		element: element, 
	        		event: event,
	        		handle: dojo.connect(element, event, this, handler)
	        	});
	        }        	
        },
        
        /**
         * Utility to remove all registered events.
         */
        disconnectAll: function()
        {
        	dojo.forEach(this.connections, function(connection) {        		
        		dojo.disconnect(connection.handle);
        	});
        	this.connections = [];
        },
Usage examples:

Code: Select all

// Add appropriate events
this.connectClass( "uctg_cell", "onclick", "onClickCell");

// Remove all existing events                    
this.disconnectAll();
Hope it helps!

Cheers,
Een
Post Reply

Return to “Developers”