Author Topic: Event trigger, notify and so on  (Read 3072 times)

Lumind

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 14
    • View Profile
Event trigger, notify and so on
« on: May 29, 2014, 06:56:03 AM »
Watched a video tutorial about Event Delegates but still can't get on with it.
I have a Popup List. In previous old NGUI versions I simply had to set some object on Event Receiver and Function Name.
But now, as I understand, I need to set the same object  in Notify in OnValueChange section of Popup List, and set Method (which is function, right?). I made the needed function public but there appeared Arg 0 and I don't know what to set there.
Here's the code:

  1. public static string MyselectedItem;
  2.  
  3.         public void OnSelectionChange (string selectedItem) {
  4.                 UILabel lbl = GameObject.Find("MainBuildPriceText").GetComponent<UILabel>();
  5.                 UISprite sprite = GameObject.Find("MainBuildPicture").GetComponent<UISprite>();
  6.                 if (selectedItem == "[FF6633]Kiosk")
  7.       {
  8.                         MyselectedItem  = selectedItem;
  9. //print ("selectedItem = " + selectedItem);
  10.                         sprite.spriteName = "KIOSK";
  11.                         lbl.text = BuildMain.build1_price.ToString();
  12.       }
  13.                 if (selectedItem == "Store")
  14.       {
  15.                         MyselectedItem  = selectedItem;
  16. //print ("selectedItem = " + selectedItem);
  17.                         sprite.spriteName = "kramnytsia";
  18.                         lbl.text = BuildMain.build2_price.ToString();
  19.       }

With nothing set on Arg 0 it says:
failed to convert parameters
Expected: System.String selectedItem


Edit: Put code in [ code ] tag. -Nicki.
« Last Edit: May 29, 2014, 10:20:22 AM by Nicki »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Event trigger, notify and so on
« Reply #1 on: May 29, 2014, 08:13:43 AM »
What did you pass for Arg 0? It expects a string. You need to pass UIPopupList.value to it.

That said, your script can be optimized much better. GameObject.Find should NEVER be used. It's terrible practice, and it's VERY slow.

Consider this function instead:
  1. public void OnSelectionChange (UILabel lbl, UISprite sprite)
  2. {
  3.     MyselectedItem = UIPopupList.current.value;
  4.  
  5.     if (MyselectedItem.Contains("Kiosk"))
  6.     {
  7.         sprite.spriteName = "KIOSK";
  8.         lbl.text = BuildMain.build1_price.ToString();
  9.     }
  10.     else if (MyselectedItem == "Store")
  11.     {
  12.         sprite.spriteName = "kramnytsia";
  13.         lbl.text = BuildMain.build2_price.ToString();
  14.     }
  15. }
Note how much cleaner it is. And it's far more performant than yours.

Lumind

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Event trigger, notify and so on
« Reply #2 on: May 29, 2014, 08:45:46 AM »
Oh, thank you very much. It all looks more easy now, and thanks for optimizing my script :)