Own logic on anim state transition?

How to add logic when transitioning between states of an anim component? I am interested in the transition between states, not the events on the animations themselves, since I will change them in the process.

In fact, when I call the transition myself from script - I put a random animation from the set of animations for specific state, the same thing I want to do for automatic transitions, like ‘falling’ playing after ‘jump’ (or create it from script but for state, not every animTrack).

Is there a clean solution or will I have to constantly add events to the animations that I randomize?

Player.prototype.animTransition = function(state, time = 0, offset = null){
    this.skinEntity.anim.baseLayer.assignAnimation(state, getRandomElement(SOME_RANDOM_ANIMS).resource)
    this.skinEntity.anim.baseLayer.transition(state, time, offset);  
};

I think a state graph should be used then. You’d toggle one or another flag from the script to trigger the animation paths in the graph.

I found a solution that works for me – randomize all animations along the chain of transitions at once.

Player.prototype.postInitialize = function() {
    this.transitionsMap = new Map();
    /**@type []*/
    const transitions = this.skinEntity.anim.baseLayer._controller._transitions;
    for(var i = 1; i<transitions.length; i++){
        this.transitionsMap.set(transitions[i]._from, transitions[i]._to);
    }
    //console.log(this.transitionsMap);
};

Player.prototype.checkStateRandomAnim = function(state){
    //console.log('checkStateRandomAnim', state);
    const anims = this.skinData.getAnims(state);
    if(anims !== undefined){
        const randAnim = getRandomElement(anims).resource;
        this.skinEntity.anim.baseLayer.assignAnimation(state, randAnim);
    }
    const transtionState = this.transitionsMap.get(state);
    if(transtionState !== undefined){
        this.checkStateRandomAnim(transtionState);
    }
};

Player.prototype.animTransition = function(state, time = 0, offset = null){
    //console.warn('animTransition', state, time, offset);
    this.checkStateRandomAnim(state);
    this.skinEntity.anim.baseLayer.transition(state, time, offset);  
};

Note that to prevent eternal recursion in case your states are closed by transitions in a loop, checkStateRandomAnim method should be reworked. Example below.

Player.prototype.checkStateRandomAnim = function(state, usedStates = new Set()){
    //console.log('checkStateRandomAnim', state, usedStates);
    usedStates.add(state);
    const anims = this.skinData.getAnims(state);
    if(anims !== undefined){
        const randAnim = getRandomElement(anims).resource;
        this.skinEntity.anim.baseLayer.assignAnimation(state, randAnim);
    }
    const transtionState = this.transitionsMap.get(state);
    if(transtionState !== undefined && usedStates.has(transtionState) === false){
        this.checkStateRandomAnim(transtionState, usedStates);
    }
};