How to access behaviors properties in js?

// what’s a proper way to access the behaviors properties?

const obj = eventsFunctionContext.getObjects('Object');

// this works, but does not look right:

const behaviorProperties = obj[0].getBehavior('myBehavior')._behaviorData;
console.log('behaviorProperties', behaviorProperties);

// also, getBehaviorName returns undefined, when called from a behavior, why? I would have expected it to be ‘myBehavior’

const behaviorName = eventsFunctionContext.getBehaviorName();
1 Like

In case anyone got here with the same question, I found the correct way to do this.

I created a sprite object, named MenuBackground, and added a custom behavior I named TestJS. It has a single property, named MyStrProperty, which is a string with default value “Test”.

To access this property from javascript code, I first needed to create a behavior function, getMyStrProperty, which sole purpose is to return the property value.

Then, from JS, we need to first get the object containing the behavior, passing “Object” as its name. Why? Because this is the name of the parameter containing information about the current object.


Next step, we get from the function also the name of the behavior which came from the parameter “Behavior”. We will use its name to get the exact behavior from current object. As soon as we get the behavior, we can call the proper created function to get the param value.