Search Unity

Is there a way to set the int hashcode of a trigger in Animator?

Discussion in 'Animation' started by laurentlavigne, Jan 15, 2021.

  1. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,363
    I'm using Animator as a state machine, convenient but the only way to trigger transition from a UnityEvent is via string or int, I'd rather use int given the cost of string.
    Is there a way to lock trigger name as an int so that I can input it in the inspector?
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    No, that's not possible. You could call Animator.StringToHash and log the result, but that's a bunch of effort and will obviously break if you rename the parameter. Though if you're using an Animator Controller as a state machine you probably don't care that much about safety anyway so you should just do whatever works.
     
  3. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,363
    uh oh, care to elaborate?
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Even if you just use a string, renaming the parameter will still break it. Looking at the Animator Controller won't tell you which scripts are depending on that parameter name. You'd need to read your whole script to find out which parameter names it depends on and even then there's no real indication of which Animator Controller that script is intended to interact with. Things need to change and evolve constantly throughout game development but Animator Controllers make it far too easy for even a tiny change to break things which you might not even notice right away and then have to waste time fixing later on. This page explains the dangers of magic strings, but there are many other practical problems with Animator Controllers as well.
     
  5. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,363
    Those are valid points especially because I won't be calling triggers from script but from unityevents.
    Perhaps I could call SO from the unityevents, those SO have the trigger name hard wired. With 3 or 4 FSM for to manage stage loading, win condition etc... maybe that won't be too bad.

    But I'm sure you've gone through all that so what is your suggestion?
     
  6. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    I use a fully script based FSM system which is included in Animancer (link in my signature, the Lite version includes the FSM source code which is entirely separate from the rest of Animancer so you can just use it on its own if you want). The State Machines examples demonstrate how I like to use it, but it's very flexible. You could easily give your script a TrySetState method that takes a state as a parameter so that you can call it with UnityEvents, that way you would be able to drag and drop the exact state you want into the event rather than needing to use something else (int/string/enum/SO) to identify your states.

    You might also be interested in my UltEvents plugin which is like UnityEvents, but much better (and is free).
     
  7. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,363
    Does UltEvents make use of reflection at runtime?
     
  8. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Yes, just like UnityEvents because there isn't really any other practical way it could be done.
     
  9. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,363
    your Animancer, does it allow to crossfade play a playable? like an anim clip
     
  10. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    I'm not sure exactly what you mean by that, but the Playing and Fading example demonstrates the basics of cross fading between animation clips.
     
  11. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,363
    not animation clips, playable assets
     
  12. bobadi

    bobadi

    Joined:
    Jan 3, 2019
    Posts:
    674
    think I don't use it for triggers, but bet it's same like for float animator parameter

    hashes.px.hash.gravity = Animator.StringToHash (hashes.px.s.gravity);
    animator.SetFloat (hashes.px.hash.gravity, 1);


    hash.gravity : int
    s.gravity : string
     
  13. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    The only thing I've used a Playable Asset for was the Introduction in the Platformer example which uses sprites so it doesn't have any fading. In theory, fading should work just like any other state for the first animation track, but you would need to try it and see because there are quite a lot of issues with Timeline that only come up when using the Playables API.
     
    laurentlavigne likes this.
  14. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,363
    thanks for the warning, yeah the entire editor is a minefield so i'm used to test things in isolation.
    it's very cool that animancer takes clips and playables, i'll try it