Way to access other instances from an object's behavior?

  • Say I have a number of tilemaps with the same behavior.

    I want to be able to call an action from the behavior on one tilemap that will check all the other tilemap's tile data.

    Is this sort of thing possible? How do I access those other objects in the behavior?

  • I handled that in my isometric behavior by managing my own list of instances in the behavior. The list is in the behavior type so any instance with the behavior can access it. Then I add and remove the instances under the create/destroy functions of the behavior.

  • ah cool, that makes sense. I'll try that.

  • hm.. not sure what 'm doing wrong, R0J0hound

    It shows the array as empty in console.

    	behtypeProto.onCreate = function()
    	{
    		this.mapList = [];
    		
    	};
    
    	behinstProto.onCreate = function()
    	{
    		this.type.mapList.push(this);
    	};
    	
    	behinstProto.onDestroy = function ()
    	{
    		cr.arrayFindRemove(this.type.mapList, this);
    	};
    	[/code:dacqive9]
  • I looked at what I did and the behavior type is unique per object type you add the behavior too. So I ended up adding it to the behavior itself like so:

    behtypeProto.onCreate = function()
    {
    	if(!this.behavior.mylist)
    	{
    		this.behavior.mylist=[];
    	}
    	this.mylist= this.behavior.mylist; // this is just for convienience
    };[/code:18t9uomz]
  • Thanks, but that still doesn't seem to work, R0J0hound . The array still shows as empty. I'm not sure why.

  • I can add random numbers to the list, but I can't add 'this'

  • Hmm... it works with my behavior.

    Try logging the array to the console every step of the way. You can verify this is added to the array by logging it to the array before and after. If it's resetting to empty every time then something is amiss.

    The actual oncreate function in my plugin looks like this:

    behtypeProto.onCreate = function()
    	{
    		if(!this.behavior.state)
    		{
    			this.behavior.state={
    				objList:[],
    				enabled:true,
    				sortOnce:false
    			};
    		}
    		this.state = this.behavior.state;
    		this.isoList = this.state.objList;
    		
    	};[/code:p2uj6fho]
    The only difference is I put the array in an object first, but that shouldn't be necessary.
    Otherwise i'm adding and removing from it in the same way.
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • ah crap- my mistake. I was actually deleting the objects in my events, so they were being removed from the array.

    It's working, I just didn't realize it.

    Thanks R0J0hound , sorry for the confusion.

  • No worries, glad you got it working.

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