UI Canvas Lua - Find Element By ID

I am following along with one of the Lua UI tutorials (which is out of date) but I have found the replacement function that searches for the UI element based on the id of the element, however the function call doesn’t work as expected - meaning it doesn’t work.

I call:

local entity = UiCanvasLuaBus.Event.FindElementById(self.canvasEntityId, 2);

which should return an entity with id = 2, but instead it returns nil.

Am I using this function wrong or is there another error here?

Hi @jacwilso,

Sorry it sounds like that tutorial is out of date. The UiCanvasLuaBus is a deprecated bus. Use the buses named UiCanvasBus, UiElementBus etc instead. These are documented here: https://docs.aws.amazon.com/lumberyard/latest/developerguide/lua-scripting-ces-api-ui.html

In general we do not recommend using the old element Ids to access UI elements. The most flexible way is to use the Lua Script component and have a property on the script that is the EntityId of the element you want to use. You can then use the UI editor to connect the UI element to that Lua script property.

Some intro info on using the Lua Script Component is here: https://docs.aws.amazon.com/lumberyard/latest/developerguide/lua-scripting-intro.html

There is an example level that uses Lua with the UI system in SamplesProject. The level is called: Levels/UI/UiMainMenuLuaSample

Hopefully that will get you going with Lua and the UI system. Let us know if you have more questions.

Rob

Hi @jacwilso - check out the UiFeatures level in SamplesProject, select Components, Other Components, Fader for an example of the fader functionality. Here is the Fader Lua documentation:
https://docs.aws.amazon.com/lumberyard/latest/developerguide/lua-scripting-ces-api-ui-uifadercomponent.html

The assets for the the fader samples can be found here:
dev\Gems\LyShineExamples\Assets\UI\Canvases\LyShineExamples\Comp\Fader
dev\Gems\LyShineExamples\Assets\UI\Scripts\LyShineExamples\Fader\

Hi RobP,

Thanks for the reply, this is what I was assuming but was hoping it wasn’t the case. I have been reading through that documentation and was hoping to find something similar to find by id, because object names are subject to change, and I can’t use the Entity property picker because the script was located outside the canvas. I have created a work around so I’m not concerned about it working but rather it being modular and convenient to change down the road.

In that same tutorial it also references a function called HasFade(entityId), I was wondering if there was a function similar or if that too has been deprecated?

Hi @jacwilso,

I’m curious what the use case is for HasFade. Why does the script need to know if a UI element has the UiFaderComponent on it?

Rob

But that doesn’t answer my question, you are just pointing me to more documentation which I have already seen. I am asking if there is a function which returns true or false whether a Fader component is attached (or if there is a more generic one to test for other types of components).

I guess now that there isn’t a “find by” type of function it is less useful, however I feel like it would be good to have a function that simply tests for whether the object grabbed “Has blank”. It also is good for sanity checks and error testing.

Hi Chase,

This is really good to know! So when you say we, do you mean at Amazon? Because my followup question to you saying that is, should I just be using C++ to do anything in Lumberyard because it seems that the Lua functions/buses can be deprecated and just generally hard to get working.

Hi @jacwilso - good question. Let me see if there’s a way to do that in Lua. In C++, we typically use FindFirstHandler to determine if a given entity handles a particular interface/bus. For example:

bool isRadioButton = UiRadioButtonBus::FindFirstHandler(radioButton) != nullptr;

Hi @jacwilso - yes sorry for the confusing statement, by “we” I meant on the Lumberyard team =)

Using C++ vs Lua really depends on what you want to do, your desired workflow, the “customer” of your work, etc. Anything you can do in Lua, you can do in C++, but not everything in C++ is available in Lua.

Lua has a very fast workflow, great for rapid iteration, and is more accessible to other members of your team that aren’t engineers/programmers. C++ is more powerful but has a slower turnaround time and (for the most part) is generally inaccessible to anyone who doesn’t have coding experience.

If it helps, the “UiFeatures” level in SamplesProject was done completely using Lua.