0 Favourites

Triggering condition on many instances of the same behavior

  • 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 ?

    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);
    };
    [/code:1u8s08wy]
    
    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.
  • Because all instances had been selected in event sheet. You might check the SOL when calling this action.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • 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.

  • 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;

    };

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

    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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
0 Favourites
[BEHAVIOR] Chipmunk Physics
673 82,511
nelostic's avatar
nelostic
Unread hot topic
0 Favourites
[Behavior] LiteTween
672 169,892
manujorgo's avatar
manujorgo
Unread hot topic
564 105,217
rexrainbow's avatar
rexrainbow