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

For developers using the Construct 2 Javascript SDK

Post » Sun Sep 25, 2016 11:24 pm

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?
B
43
S
19
G
65
Posts: 1,101
Reputation: 37,939

Post » Sun Sep 25, 2016 11:43 pm

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.
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455

Post » Sun Sep 25, 2016 11:47 pm

ah cool, that makes sense. I'll try that.
B
43
S
19
G
65
Posts: 1,101
Reputation: 37,939

Post » Mon Sep 26, 2016 12:46 am

hm.. not sure what 'm doing wrong, @R0J0hound
It shows the array as empty in console.
Code: Select all

   behtypeProto.onCreate = function()
   {
      this.mapList = [];
      
   };

   behinstProto.onCreate = function()
   {
      this.type.mapList.push(this);
   };
   
   behinstProto.onDestroy = function ()
   {
      cr.arrayFindRemove(this.type.mapList, this);
   };
   
B
43
S
19
G
65
Posts: 1,101
Reputation: 37,939

Post » Mon Sep 26, 2016 1:08 am

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:
Code: Select all
behtypeProto.onCreate = function()
{
   if(!this.behavior.mylist)
   {
      this.behavior.mylist=[];
   }
   this.mylist= this.behavior.mylist; // this is just for convienience
};
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455

Post » Mon Sep 26, 2016 1:15 am

Thanks, but that still doesn't seem to work, @R0J0hound . The array still shows as empty. I'm not sure why.
B
43
S
19
G
65
Posts: 1,101
Reputation: 37,939

Post » Mon Sep 26, 2016 1:20 am

I can add random numbers to the list, but I can't add 'this'
B
43
S
19
G
65
Posts: 1,101
Reputation: 37,939

Post » Mon Sep 26, 2016 1:35 am

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:
Code: Select all
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;
      
   };

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.
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455

Post » Mon Sep 26, 2016 1:52 am

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.
B
43
S
19
G
65
Posts: 1,101
Reputation: 37,939

Post » Mon Sep 26, 2016 4:26 am

No worries, glad you got it working.
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests