Forum rules - please read before posting.

Slider on menu not updating

I have a slider on a menu (the Options menu) that controls the walk speed for the player.

The speed is set by a variable (a normal global variable float value). However, when the menu opens the slider is not reflecting the correct value.

When I open the menu I'd like the slider to reflect the current speed (which is set elsewhere). I created a custom action that is run from an actionlist that runs when the menu is turned on.

The actionlist finds the slider and sets the slider value.

This is a subset of the code in the custom action:

        Menu menu = PlayerMenus.GetMenuWithName("Options");
        GameObject menuGo = menu.RuntimeCanvas.gameObject;

        Slider[] sliders = menuGo.GetComponentsInChildren<Slider>();
        foreach (Slider slider in sliders)
        {
            if (slider.gameObject.name == "sldrSpeed") // looks for the speed slider
            {
                GVar walkSpeed = GlobalVariables.GetVariable("Environment/walkSpeed");
                slider.value = walkSpeed.FloatValue;
                break;
            }
        }

The slider does not get set correctly. It always appears as however it was last manually set.

One possible theory is that the game is paused, so the UI ignores the value.

Or, more likely, it is because the menu has already been drawn and what I need is some way to tell the slider that the value has been changed and the slider should be updated to reflect the new value.

Any ideas for me?

-Ken W

Comments

  • edited November 2021

    Is the Slider connected to the AC's Menu Manager as a Slider element?

    If not, updating the Slider's value won't involve AC - so the code snippet ought to work, unless perhaps you're assigning the value to something outside it's current min/max bounds. The game being paused shouldn't interfere with things, IIRC.

    If it is linked to AC as a Slider element though, its value is controlled on the AC side. The Slider can then be synced with a Global Float variable automatically - avoiding the need to update it through script - by setting its Slider affects property to Float Variable.

    Additionally, if the variable's value represents an option, you may wish to set its Link to field to Options Data if you're not already. This will cause its value to be synced with the current Profile Data, and not whichever save file the user last recorded.

  • I had forgot you had the ability to link a slider to a global variable. I made the change and it works perfectly now (plus I was able to delete my custom action). Thank you!

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.