Download

Randomly select between states or animations

I have a state machine and want to randomly select between 2 states.

So from one state I want to drag two lines away, one to option 1 and another one to option 2. They both share the same condition, which is already bad design because if I want to change this condition I would have to edit both. But now how to randomly select between these two? My first thought was to have a variable like “RandomBool” which get’s set in the state before, but then I noticed I can’t set variables in states…

I also tried to select from 2 animations randomly in one state, which I also have not get working. Because they all should loop and there is no way to see when one animation finished (inside of one state) there is no way to select between 2 animations in one state, so I had to create 2 states which call each other in a loop like this:

But this is also bad design because the lines going away from them to the next state are having the same condition, but if I would want to change it I would have to edit it manually in both transition rules. If I would have 5 animations which should loop randomly I would have 5 same transition rules going to the next “real” state which I would have to edit manually - and I would need to have 5*(5-1)=20 internal transition rules between these states which would just all be “Animation time remaining == 0”. For 10 animations it would already be 90 transitions between the states to let every animation be playable after any other of the 10.

I think this is not really how it’s supposed to be used, so I’m probably doing something wrong :slight_smile:

Opinion wise not a big fan of state machines. The main problem is over time if you add all to it you can run into a bad case of argument nesting and I just don’t feel it necessary as far as next gen tech goes and was included as a legacy feature.

Also for it to all work slick you don’t want to be bumping and banging around in the event graph while stuck inside the state machine.

If you want to use state machines then Blend Poses by int is a good path and you can add as many states as you wish using a integer index/list and put the argument into the event graph where it belongs. :wink:

Blend Pose by Int has the nasty problem of not being able to reset animations, while state machines do. I would advice using a conduit as a shared entry point to your randomized states. The conduit would then branch out to all animations you want to randomize. Whenever you leave the conduit you randomize a number/bool for which animation to use. To exit the randomized state, just check for remaining animation time or another value that you control. As the state returns to the conduit, the value has already been randomized on the previous exit and you continue to the next randomized animation. Unless you’re returning to the other part of the graph.

What else could I use instead of a state machine? I thought they are the only way to handle animations in UE4? The problem with “Blend poses by int” is that if I generate the random int inside of the state, it changes every frame so every frame a different animation is used :frowning:

How can I set a bool from a conduit? I tried to do that, but since there are no execution wires I can’t plug anything into the execution input of the “set” node.

Can you explain by example? I’ve yet to see this problem myself and if a uncontrolled event change does occur then it’s a bug, a rather big one, that needs to be reported and fixed.

Used this bit of info as our starting point for our player control system.
https://youtube.com/watch?v=3NK97psTA8g
A state machine is tech that’s been used forever in some form or another where layered animations is NextGen tech where the input can be data drive much easier as pointed out in the video.

You can use a state machine anywhere with in a layered system if you wish but nesting arguments with in a state machine is not a good idea as you can land up with arguments arguing with arguments that can result in a character model not moving at all.

You would not use a random function directly with in a state or state machine but use it as a means to change the seed value by timer or delay. You could set a delay that every 10 seconds the random function generates a new value to “NewSeed”.

You can then change the state value and the argument would be “if NewSeed <> CurrentSeed then CurrentSeed = NewSeed” Be it using a state machine or layered the state will only change every ten seconds or not at all if the new value generated is equal to the current value.

If you select any state or conduit, you can define events in its properties. Type in an event name for leaving the state, compile the blueprint, then the event will appear as an anim notify in the right click menu in the event graph. Set your bool there.

Do the switch at any time and you’ll see the current animation stop at its current time and not reset to time 0 next time it becomes active. Only way to do it is by using a sync group, which then only syncs up with a dominant animation.

Good to know.

Blend by int probably works for me because the bulk is single frame poses to correct for additive positioning and to switch between using one blend space to another for the primary animation.