Author Topic: help passing extra data using UIEventListener  (Read 5256 times)

SquigglyFrog

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 7
    • View Profile
help passing extra data using UIEventListener
« on: October 22, 2014, 03:00:33 PM »
Ok, first I'll admit I am clearly a beginner when it comes to events and delegates and the like.. Currently I have a UIWidget with a button script attached, fileItemWidgets
  • ..
  1. UIEventListener.Get(fileItemWidgets[5].gameObject).onClick += OnAccountFileSelected;

What I'd really like to do is pass an INT into that OnAccountFileSelected.. everything I've tried so far hasn't worked, and what I want to end up with is something like this.

  1.     void OnAccountFileSelected(GameObject g, int ID)
  2.     {
  3.         print("Clicked filename: " + ID.toString());
  4.     }
  5.  

I checked the docs and I see this UIEventListener.FloatDelegate  which would work, but i'm unsure how to actually use that and attach it to my buttons... suggestions / help is appreciated!

dilshod

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 7
  • Posts: 14
    • View Profile
Re: help passing extra data using UIEventListener
« Reply #1 on: October 23, 2014, 06:30:16 AM »
At least you can do:

  1. UIEventListener.Get(fileItemWidgets[5].gameObject).onClick = (go) => {
  2.         OnAccountFileSelected(go, 123);
  3. }
  4.  
« Last Edit: October 23, 2014, 11:08:03 AM by dilshod »

SquigglyFrog

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: help passing extra data using UIEventListener
« Reply #2 on: October 23, 2014, 08:59:17 AM »
Well, that throws an error, probably though due to my lack of knowlege on events and listeners.. appreciate the help, will continue to dig into it and google some more..

dilshod

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 7
  • Posts: 14
    • View Profile
Re: help passing extra data using UIEventListener
« Reply #3 on: October 23, 2014, 11:31:28 AM »
Well, that throws an error, probably though due to my lack of knowlege on events and listeners.. appreciate the help, will continue to dig into it and google some more..

My bad, updated the code above.
You'll have more problems with anonymous functions, you'd better try this:
  1. void Init() {
  2.     var listener = UIEventListener.Get(fileItemWidgets[5].gameObject);
  3.     listener.parameter = 1234;
  4.     listener.onClick = OnAccountFileSelected;
  5. }
  6.  
  7. void OnAccountFileSelected(GameObject g) {
  8.     int ID = (int)UIEventListener.Get(g).parameter;
  9.     print("Clicked filename: " + ID.ToString());
  10. }
  11.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: help passing extra data using UIEventListener
« Reply #4 on: October 23, 2014, 03:41:59 PM »
  1. UIButton btn = GetComponent<UIButton>();
  2. EventDelegate.Set(btn.onClick, delegate () { Debug.Log("Inline delegates work fine too"); });
Don't use UIEventListener when you already have a delegate provided by the button class.

SquigglyFrog

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: help passing extra data using UIEventListener
« Reply #5 on: October 23, 2014, 04:03:35 PM »
Hey thanks for that! The 2nd actually makes a lot more sense to me, and going back to the updated 1st example, I see how that works as well. I know this is a weak point of mine, and I truly appreciate both examples! Doesnt help that the doc has me on Pain Pills right now so the code this morning was in a bit of a haze. lol