UiSliderBus values cannot change in the OnActivate

Hi

I am trying the “How to Interact with UI Using Lua” tutorial and found that UiSliderBus.Event.SetMaxValue is not working in the OnActivate function.

This is part of my UIEditorLuaSubTutorial.lua script to just print out the maxValue of the slider to the log file.

    local UIEditorLuaSubTutorial =
{
Properties =
{
Image = {default = EntityId()},
Slider = {default = EntityId()},
BackButton = {default = EntityId()},
},
}
function UIEditorLuaSubTutorial:OnActivate()
UiSliderBus.Event.SetMinValue(self.Properties.Slider, 0)
UiSliderBus.Event.SetMaxValue(self.Properties.Slider, 360)
UiSliderBus.Event.SetValue(self.Properties.Slider, 0)
local maxValue = UiSliderBus.Event.GetMaxValue(self.Properties.Slider)
Debug.Log("OnActivate: Slider max value = " .. tostring(maxValue)) -- maxValue is nil self.sliderHandler = UiSliderNotificationBus.Connect(self, self.Properties.Slider)
end
function UIEditorLuaSubTutorial:OnSliderValueChanged(value)
local maxValue = UiSliderBus.Event.GetMaxValue(self.Properties.Slider)
Debug.Log("OnSliderValueChanged: Slider max value = " .. tostring(maxValue)) -- maxValue is 100 instead of 360 end
return UIEditorLuaSubTutorial

My environment is

  • Lumberyard version 1.9.0.1

  • Windows 10 pro 64bit

  • Core i7-5820K / 64GB / GeForce 1080

Hey @Soeda — sorry for the troubles. Let me see if i can get some answers for you :slight_smile:

Hey @Soeda, just wanted to keep you posted. We’re still looking into this issue - we’ve bumped this issue with our team to get a quicker response. Sorry for the delays.

Hi @Soeda,

Sorry this is not working for you.

It looks like the slider entity is not listening on the UiSliderBus when you send the events. There are a couple of reasons that this might happen.

One reason would be if the properties on the LuaScriptComponent are not set correctly. The “Slider” property needs to be set to point to an entity with a UiSliderComponent on it.

Assuming that is all setup correctly it could be a problem with the order in which the UI element entities are activated. If the element with this script on it is activated BEFORE the slider element then the slider will not be listening on the bus yet.

There are ways to safeguard for this. The recommended way is to use a TickBus to wait a frame before sending those events. So the steps are:

  1. in the Activate register on the TickBus
  2. In the OnTick method disconnect from the TickBus and then call the events on the Slider
    This is a pattern that we use in a lot of our sample scripts to do something after all of the entities are activated. Since the script disconnects from the TickBus after the first frame there is no performance issue.

You can see an example of it in: SamplesProject\Scripts\UiMainMenuLuaSample\MainMenu.lua

Let us know if that helps.

Rob

Hi @RobP

Thank you for your response.

I will use the TickBus pattern to avoid timing issue.

But, I could not find the SamplesProject\Scripts\UiMainMenuLuaSample\MainMenu.lua file in the 1.9.0.1 install directory.

Do I need to download other sample data or version to get the MainMenu.lua ? If so which sample should I try?

Thank you.

Hi@RobP

I changed my script as below and working fine.

Thanks.

	function UIEditorLuaTutorial_Second:OnActivate()
self.tickBus = TickBus.Connect(self)
end
function UIEditorLuaTutorial_Second:OnTick()
self.tickBus:Disconnect()
UiSliderBus.Event.SetMinValue(self.Properties.Slider, 0)
UiSliderBus.Event.SetMaxValue(self.Properties.Slider, 360)
UiSliderBus.Event.SetValue(self.Properties.Slider, 0)

Glad you were able to get the issue resolved!