I have a basic container setup for my Widgets which show content using a Widget Switcher. I would want to be have the nested Widget cast to it’s parent widget, but get parent or get containing object and cast to class of the parent widget doesn’t do anything.
Converting everything to make use of Dispatchers was a slight pain but now that everything’s working I can’t believe I’ve never made use of these - thank you!
I can’t believe I’ve never made use
of these - thank you!
You have, without realising it:
The humble button above comes with 5 built-in dispatcher events; when you place a button on the canvas, you’re placing a child in the parent.
You want the children of the widget switcher to send data back to the switcher? You don’t need to cast, dispatch that data. Safer, easier and more efficient overall.
If you must use direct communication, expose a parent reference on the children. Again, no need to cast here.
I want the Widget inside another Widget (or a Widget Switcher) to execute Custom Events that the parent Widget (that has the Widget Switcher) has.
I haven’t had to use dispatch in my projects before, how is it easier and more efficient?
About direct communication - I tried setting up a system where the nested Widget has a variable that holds the reference to the parent Widget but it all got very messy and overlycomplicated as I built more complex elements. I would like to avoid this mess if at all possible.
About direct communication - I tried
setting up a system where the nested
Widget has a variable that holds the
reference to the parent Widget but it
all got very messy and
overlycomplicated as I built more
complex elements. I would like to
avoid this mess if at all possible.
Yup, this gets messy. Consider the following:
Here’s a child widget:
It has an Event Dispatcher and a Button that will fire it. You can add all kind of data to the dispatcher, including a self reference to the child itself if necessary - perhaps the parent will want to know which child is calling it.
And here’s the parent:
It grabs all the children, registers their dispatcher call. Now every time a child’s button is pressed, the parent will hear about it and this event will be called. You can then have it fire anything in the parent.
Alternatively, if you prefer to do it manually, the children’s dispatchers’ events are available to the parent like so:
No references, no casting.