Triggering condition on many instances of the same behavior

For developers using the Construct 2 Javascript SDK

Post » Wed Aug 27, 2014 1:18 pm

I created a behavior that can be added to a given object more than once. (Of course, each instance of my behavior will have a different name in the C2 editor.)

When triggering a condition from an action (using Runtime.trigger), it seems that the condition is triggered on all instances of my behavior. What I want to do is trigger the condition only on the behavior instance to which the action belongs. How can I do that ?

Code: Select all
Cnds.prototype.OnStuffChange = function (name)
{
   return cr.equals_nocase(this.currentStuff, name);
};

// ...

Acts.prototype.ChangeStuff = function(name)
{
        this.currentStuff = name;

        // this is triggered on all instances of "MyBehavior", not only on "this"
        this.runtime.trigger(cr.behaviors.MyBehavior.prototype.cnds.OnStuffChange , this.inst);
};


I'm just getting started creating behaviors. I thought about saving an "activeInstanceName" globally and compare it with this.type.name in conditions. But that seems to be too much work for something this simple.
ImageImage
B
44
S
14
G
10
Posts: 167
Reputation: 7,781

Post » Fri Aug 29, 2014 3:54 am

Because all instances had been selected in event sheet. You might check the SOL when calling this action.
B
110
S
28
G
280
Posts: 4,487
Reputation: 156,566

Post » Fri Aug 29, 2014 9:25 am

If we were talking about plugins, I would completely agree with you. But in my case I'm working with a behavior that has many instances in the same object.

Quite frankly I'm not very sure how SOL for behavior instances work, how can you "select" a behavior instance in the event sheet ? The manual is not very clear on this subject.
ImageImage
B
44
S
14
G
10
Posts: 167
Reputation: 7,781

Post » Fri Aug 29, 2014 9:41 am

@Zed2100

Uh, yes, all conditions of this behavior will be triggered. You could add a flag to check which behavior instance need to be executed.


this.my_call = true;
this.runtime.trigger(cr.behaviors.MyBehavior.prototype.cnds.OnStuffChange , this.inst);
this.my_call = false;


And

Cnds.prototype.OnStuffChange = function ()
{
return this.my_call;
};
B
110
S
28
G
280
Posts: 4,487
Reputation: 156,566

Post » Fri Aug 29, 2014 10:25 am

I was hoping for a built-in feature in the sdk that handles behavior instance picking. :D

My solution was to store a global variable "currentBehaviorInstanceName" inside the behavior's closure and set it to "this.type.name" before triggering a condition, then checking if the variable is equal to "this.type.name" inside the condition.

I think your solution is better, using a boolean is much simpler, plus, I try to avoid global variables as much as possible.

Thanks.
ImageImage
B
44
S
14
G
10
Posts: 167
Reputation: 7,781


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests