Author Topic: Simple Question: Changing content based on List Selection  (Read 9717 times)

sintua

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 63
    • View Profile
Simple Question: Changing content based on List Selection
« on: April 19, 2012, 01:33:53 PM »
I feel like this is stupid-easy and I'm just missing something, somewhere.

I have a popup list of items I've populated with the names of some generated characters. When a character is selected in this list, I want to show their information (picture, description, etc) on the right. I've got everything set up except I don't know how to change the widgets to show the selected character's information. I see popup list has "a targeted eventreceiver and "functionname" but I'm not really sure how to use them, and the documentation didn't really elucidate anything.

What am I missing? :-\

EDIT: Also, I thought this'd be easier than it turned out to be: How do I find the index of the selected item in a popup? I can find the string, but not the index number? (Since the list might have strings that are the same, I wouldnt want to just grab the first whether that's the selected one or not)
« Last Edit: April 19, 2012, 02:21:32 PM by sintua »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Simple Question: Changing content based on List Selection
« Reply #1 on: April 19, 2012, 03:40:43 PM »
1. Using them is trivial -- set the event receiver to the game object of the script you want to receive the event, and the function name should be the function that will be triggered when something is selected from the list.

2. Don't put things in the list that have the same name. Call them something different. :P

sintua

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 63
    • View Profile
Re: Simple Question: Changing content based on List Selection
« Reply #2 on: April 24, 2012, 10:44:40 PM »
Ok, that was pretty obvious... I thought they were two different unrelated things; that makes more sense.

Is there any way to refer to the index of the listed items in the menu, rather than the string?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Simple Question: Changing content based on List Selection
« Reply #3 on: April 24, 2012, 11:54:04 PM »
Nope, everything works in strings. When setting the selected value, you similarly use UIPopupList.selection = "String Value", so it's this way for consistency. If you want an index, integrate it into your item, ie: "1. First", "2. Second", etc.

sgolby

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Simple Question: Changing content based on List Selection
« Reply #4 on: July 30, 2012, 04:36:09 PM »
Is there any sample code / documentation (not an API reference dump) on how to use the Popup List ? (and other things in nGUI)  I'm really struggling with the way the nGUI info is presented, even after watching the videos and looking at the examples in the package.  Everything stops just short of a functional example, it took me a considerable amount of data mining to find out I needed to do this to get data back from nGUI. 

   void Awake () {
      UICamera.genericEventHandler = this.gameObject;
   }


Thankfully another thread had code to tell me how to detect what was clicked using UICamera.lastHit.collider.

Please, please, please put a couple of sample scripts in the paid nGUI package of buttons/lists actually *doing* something for each example so we don't have to search and guess for each little thing.

I'm now basically trying to guess how "Using them is trivial" and I'm getting this error after adding my OnSelectionChange() function.

function OnSelectionChange() {
   print ("UIPopupList.selection = " + UIPopupList.selection);
}   


Assets/_scripts/GameManager.js(767,57): BCE0020: An instance of type 'UIPopupList' is required to access non static member 'selection'.

It seems this is passed in as a string variable and isn't set like UICamera.lastHit.collider. <sigh>

This works

function OnSelectionChange(selectedItem) {
   print ("selectedItem = " + selectedItem);
}   



ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Simple Question: Changing content based on List Selection
« Reply #5 on: July 30, 2012, 06:16:02 PM »
You don't need to set the generic event handler. It's only needed if you want to get a copy of all the events. All events, by default, go directly to colliders. A button has a collider on it, and clicking on the button actually send an event to that collider's game object, for example. If that game object has scripts on it that react to OnClick, things will happen.

For the functional popup list... why don't you just follow the tutorials? One of the tutorials covers popup lists, including how they're used. Tutorial 11, to be exact. Selecting an item from the popup list changes the sprite's color. Another functional popup list can be found in the Localization example. There, selecting a choice from the popup list changes the UI's language.

The error you're getting clearly tells you that you need an instance of the popup list. You're using its type (UIPopupList). Where is the actual instance? This is the equivalent of you saying "Mazda.Unlock()". Which Mazda? Where is it? How do I know what it is? You need an actual reference for this to work.

My suggestion -- please learn the basics of Unity (GetComponent function in particular). You will have a very hard time understanding NGUI -- no, everything in scripting really -- if you don't.
« Last Edit: July 30, 2012, 06:22:17 PM by ArenMook »

faceOFF

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Simple Question: Changing content based on List Selection
« Reply #6 on: August 10, 2012, 05:18:36 AM »

This works

function OnSelectionChange(selectedItem) {
   print ("selectedItem = " + selectedItem);
}   


Thanks a lot!
It very helped!

Tethys Interactive

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Simple Question: Changing content based on List Selection
« Reply #7 on: May 16, 2014, 10:33:29 AM »
The 3.0 switch over has thrown us for some loops as well. I'm posting on this thread as it is related to my problem.

Some may ask WHY was Event Receiver with Function Name removed from UIPopup list. I just want to know, how to get that functionality back or work around it. Was very easy for the scripting newbie - slam a function into the script, tie it to that and all done, just like buttons and onclick. Now it has some kind of delegate system. I also looked for the tutorials, found them, and discovered the mentioned Tutorial 11 for popup list is missing. :( We will obviously always need good programmers to do the cool stuff, but basic setup of GUI shouldn't require them, IMO, with this kind of plug-in. Thx in advance for any replies!

The code in question switches the character controller. I had the popup list tied to this script:

private void OnControllerChange (string sel)
   {
      switch (sel) {
      case "Default":
         GameManager.Player.transform.SendMessage("OnControllerTypeChange",ControllerType.Normal);
         break;
      case "Mouse":
         GameManager.Player.transform.SendMessage("OnControllerTypeChange",ControllerType.MouseRotation);
         break;
      case "Click":
         GameManager.Player.transform.SendMessage("OnControllerTypeChange",ControllerType.ClickToMove);
         break;
      }
   }

With the Cases listed in the list in UIPopup List.
« Last Edit: May 16, 2014, 11:05:14 AM by Tethys Interactive »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Simple Question: Changing content based on List Selection
« Reply #8 on: May 16, 2014, 02:39:33 PM »
Register the callback like so (or just select it in inspector -- that's even easier):
  1. EventDelegate.Add(yourPopupList.onChange, YourCallback);
Your function becomes:
  1. public void OnControllerChange ()
  2. {
  3.     switch (UIPopupList.current.value)
  4.     {
  5.         ...
  6.     }
  7. }

Tethys Interactive

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Simple Question: Changing content based on List Selection
« Reply #9 on: May 16, 2014, 06:17:53 PM »
Register the callback like so (or just select it in inspector -- that's even easier):
  1. EventDelegate.Add(yourPopupList.onChange, YourCallback);
Your function becomes:
  1. public void OnControllerChange ()
  2. {
  3.     switch (UIPopupList.current.value)
  4.     {
  5.         ...
  6.     }
  7. }

Thanks so much, worked like a charm.