Forum rules - please read before posting.

Link Variable to Animator - Sanity/HP mechanic

edited December 2022 in Technical Q&A

Hi. So, I'm back annoying everyone with the same old problem. Here's an outline of what I'd like to do.

I'd like to have a "sanity/hp" mechanic that works as follows:
The player has this UI/Portrait, which is linked to a animator (each animation is a state, stable, scared, etc). I'd like each state to be attributed to a variable integer value. For example, stable = 0, alert = 1, scared = 2, death = 3. I posted before "Link Variable to Animator" was implemented, and I waited for the feature to be out, but now I'm having difficulty finding a proper example of this beieng set-up. (https://adventure-creator.fandom.com/wiki/Link_an_Animator_parameter_to_a_Variable)

What I've done so far is create an empty object in the scene, added a animator to it with 3 test states (stable, alert, death), I've created the integer, but I'm unsure of how to put everything together.
As always, thank you in advance, and happy holidays everyone.

Comments

  • Happy holidays, @Alex877.

    The "Link Variable To Animator" component is included in AC - don't rely on the script on the wiki page, as the one included is more powerful. For details, see the Manual chapter of the same name.

    As of v1.76.1, you can now use this component to link an Animator's Integer parameter to a Global Integer variable (previously only Component variables could be linked).

    Create a new Global variable - either an Integer or PopUp will work - name it the same as the Integer parameter in your Animator, and set its Link to property to Custom Script.

    Next, attach the Link Variable To Animator component to the same GameObject as your Animator.

    In its Inspector, set its Shared Variable name to the shared name of your variable/parameter, set the Variable location to Global, and assign the Animator in the Animator field.

    At runtime, the parameter and variable should then be synced up.

  • edited December 2022

    https://drive.google.com/file/d/1zUUDjfspNLgFLeR27mtjvodaOByvZRWz/view?usp=share_link

    @ChrisIceBox
    Hi there! Thank you for taking the time and replying so quickly. I've set it up partially (and I've added an attachment), but I'm still unclear on a few things. This part "set the Variable location to Global" I think is referencing the "?" I noted in the image (Variables/Link Variable to Animator). Am I missing any component in this set up? Also, how will AC know how to cycle through the states if there's only one Parameter in the Animator? Or do I need to create an animator/parameter for each state?
    As always, thank you, and have a wonderful end of the week.

  • What is your AC version? The ability to rely on a Global variable, as opposed to only a Component variable, requires v1.76.1. If that's what you're using, make sure you're not accidentally adding an old script from the wiki.

    The correct script can be found in Assets/AdventureCreator/Scripts/Variables.

    Also, how will AC know how to cycle through the states if there's only one Parameter in the Animator?

    Link Variable To Animator merely syncs up the Variable and Animator parameter values - you'll still need to use Action logic to change the Variable's value, but that will in turn update the parameter.

    You can increase the Variable's value by using the Variable: Set Action. If you want it to return to zero after reaching a certain limit, you can precede this with a Variable: Check Action to check it beforehand.

    You won't need any more Animators or parameters, but you will need to create a Transition in your Animator for each possible value the parameter can take.

  • edited December 2022

    @ChrisIceBox Hello, hope you had a great holiday break. Yes, there was a problem where AC didn't update to 1.76.1. I experimented a bit and managed to get the animations to run when a variable is changed. I have only one question left regarding how to make them automatically play when the variable is changed. https://drive.google.com/file/d/1NYfRWxLB6Fj99fkG74lbaW9cfL29CulS/view? <-usp=share_link <- I've added a video of the test and how it works (for now), my problem is that I have to use two elements to trigger the animation change. For example, in this video I click on the fridge to decrease the variable by 1 each time, but in order to trigger the animation I need to go inside the trigger (on enter) where I've added multiple variable checks. I'd like to run these variable checks outside of the scene. I've tried adding them to a cutscene but they don't trigger, I've tried adding an ActionList (on the object) that runs an Asset File, still no luck. I'm sure at this point I'm just missing something obvious. As always, thanks so much for replying.

  • Can you share screenshots of the Actions involved, as well as the Animator / Link Variable To Animator components?

    It should be that the Variable: Set Action changes the parameter value in the Animator as well - keep the Animator window open and check its SanityCheck property value as the Global Variable value is changed. They should be in sync.

  • edited December 2022

    https://drive.google.com/file/d/1ZdBWDrNqUBW63TmUih6_YSNNYToy7mz5/view?usp=share_link <- here's an image of the entire set up. So when I set the variable (change the value) the animation should trigger automatically and not require what I called "trigger check to start animation" in the screenshot? This is the only way that I was able to get the animation to change when the variable value is modified. I also noticed that I get this "No variable component found for link variable to animator" message on play. even though everything is set-up properly, as far as I can tell.
    Thank you again for the time spent trying to unblock this.

  • So when I set the variable (change the value) the animation should trigger automatically and not require what I called "trigger check to start animation" in the screenshot?

    The "Trigger check to start animation" should not be necessary, no. That's the intention behind Link Variable To Animator - once the variable's value is changed, that should update the Animator.

    It'll only do so by updating the parameter value, however. Whether that results in an animation change will be down to your transitions.

    Again, check to see if the Animator's parameter value is updating at the same time as the variable. If it is, make sure that your transitions are set up to cater for it.

    Rather than transitioning to/from specific states, it might be easier to have your transitions come from the "Any State", so that the correct animation will play regardless of what it's playing at the time.

  • edited December 2022

    https://drive.google.com/file/d/1Sm1vBbVxjjxr-BX1b2KnED5hLK3YXDOW/view?usp=share_link <- Animator parameter value doesn't seem to update at the same time as the Variable. I also changed the animations to any state, but I don't think that's what's causing the issue, or maybe it is, I don't know. Unity Animator and synchronizing is the bane of my existence.

    Could the aforementioned error be the culprit?

    "No Variables component found for Link Variable To Animator on Sanity Check (UnityEngine.GameObject)

    -> AC debug logger
    UnityEngine.Debug:LogWarning (object,UnityEngine.Object)
    AC.ACDebug:LogWarning (object,UnityEngine.Object) (at Assets/AdventureCreator/Scripts/Static/ACDebug.cs:33)
    AC.LinkVariableToAnimator:OnEnable () (at Assets/AdventureCreator/Scripts/Variables/LinkVariableToAnimator.cs:56)"

  • Animator parameter value doesn't seem to update at the same time as the Variable.

    Can you double-check that the Animator in your video is synced properly with the Animator component in the scene? The Animator window's lack of a "state timer" running on a loop indicating the active state suggests you may be viewing the asset file, not the one running in the scene.

    To do this, select the Animator component for the object in the scene at runtime.

    Could the aforementioned error be the culprit?

    No - this should not be showing, and can be safely ignored. I'll see its removal in the next update.

  • Ah, recreated the trouble. Apologies, it is indeed a bug.

    I'll provide a robust fix in the next release, but in the meantime you should be able to get by by opening the LinkVariableToAnimator component, and renaming its OnEnable function to Start.

  • No issues, I'm kinda of happy it's a bug and that I'm actually not that bad at following instructions. Trying the fix now. thank you

  • edited December 2022

    @ChrisIceBox Changing OnEnable to Start didn't fix the issue, everything seems to operate as before. I'll wait for the update/fix.

    In the meantime I had a question about Variable Integers in general and their relationship to "Check Action". In all examples I only see Bool types being used so I don't know if they operate the same. I need to trigger specific actions when a integer value is reached, but I'm unable to. I've tried adding them to an Action List and running the Asset in a scene and it doesn't work. Also, unsure on how these types of Variables interact with On Start Game. I know that you need to Set Bools in On Start Game to their default, but I'm unsure how to treat Integers if the value is constantly changing.

    As usual, thank you in advance for your help, if you have a link to a thread or a video covering this, please just reference that, I've wasted enough of your time on the subject.

  • Bools and Integers are very similar in practice - think of a Bool as an Integer that only has two values, where False = 0, True = 1.

    You shouldn't typically need to set a Variable's value when your game starts, regardless of its type. A Variable's default value can be set within its properties in the Editor.

    If you're having a specific issue with them, share details so that I can recreate it. A tutorial on using an Integer variable to represent the game score, however, can be found here.

  • Hello, Happy New Year. Hope you had a wonderful holiday season. I saw the score tutorial, but it doesn't help. I need a method to check integer changes without interaction and to have them trigger specific events. For example: I need to enable a "bool" Variable when Integer Variable reaches a specific value. It's all related to the "sanity/hp". I could create a trigger to spawn inside each room, and to check that integer each time, but I wonder if there's a simpler way, something baked into AC.

  • edited January 2023

    https://drive.google.com/file/d/1MY3J_aIxui1oAYaOXJ5BPlf2NL1qAB2M/view?usp=share_link <- this is a simplified version of what I'm trying to achieve. If the Integer Var reaches specific values, conditions will change in the game ,represented by those placeholder text prompts. This is what I'm trying to achieve, but I can't seem to get the Var Checks to trigger any of it outside of an actual trigger. And if this is the only that this can be achieved without code, it's fine, I can make due with it and automate it. Just trying to make sure I'm not overly complicating things. With this approach I can also play the Animations I couldn't before.

  • Don't worry - you certainly don't need to rely on a scene wide Trigger to check values like that.

    The two Actions (1 and 2) in the top-right corner of your ActionList are along the right lines - if your Integer goes beyond a certain value, your Bool value changes. All you need to do is run these two Actions whenever you update the Integer value (i.e. following the Variable: Set Action that affects it).

    If you're updating this Integer in various ActionLists, you can create a new ActionList asset with those three Actions, and parameterise the amount that the Integer value is increased by so that you can set it dynamically. Then, each time you want to increase that variable, you can call that ActionList using the ActionList: Run Action to increase its value, check it, and then update the Bool value accordingly.

    This ActionList, for example, will increase integer Sanity by a dynamic amount, and set bool "Alert" to True if "Sanity" increases beyond 4:

    A tutorial on ActionList parameters can be found here.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.