Author Topic: Sending Parameters within Buttons?  (Read 24623 times)

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Sending Parameters within Buttons?
« on: November 21, 2012, 10:08:34 PM »
This has been bugging me for ages, seems like a really easy thing to do but yet without fiddly work arounds I can't seem to cleanly get this to work.

Anyway, my example.

Let's say I have a flick panel full of buttons, I duplicate lots of buttons from the 1st entry, but each button has OnClickButton. So each button points to the same function in code.
What I want is something like "OnClickButton(1)", "OnClickButton(2)" etc. But it seems you can only point to a function but not pass it any parameters.
So unless I have something like:-

void OnClickButton(int p)
{
}

void OnClickButton1()
{
 OnClickButton(1)
}

void OnClickButton2()
{
 OnClickButton(2)
}

void OnClickButton3()
{
 OnClickButton(3)
}

... etc
I can't get which button was clicked in the flick panel.
Any ideas on how I could do this effectively?

ArenMook, I looked in many of your threads. Such as this one

http://www.tasharen.com/forum/index.php?topic=318.msg2254#msg2254

and I am completely lost.

In the end I got around the 'issue' by renaming my Button objects to simply numbers and then grabbing the Unity GameObject object name using MyObject.name and then converting the string to an int.
« Last Edit: November 21, 2012, 10:27:28 PM by ENAY »

JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: Sending Parameters within Buttons?
« Reply #1 on: November 21, 2012, 10:35:48 PM »
Let's assume that you have the following script attached to every button (the gameobject with the collider) in your flick panel.  Also, we assume that each of your buttons has a unique name.

  1. void OnClick()
  2. {
  3.   switch (this.gameobject.name)
  4.   {
  5.     case "button_1_unique_name":
  6.       // do some stuff
  7.       break;
  8.     case "button_2_unique_name":
  9.       // do different stuff
  10.       break;
  11.   }
  12. }
  13.  

You have one block of code that handles the button presses for all your buttons.  If you want, you can have the case statements call static methods in another class, if you want to organize your code that way.

Or did I completely not understand what you were describing?

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: Sending Parameters within Buttons?
« Reply #2 on: November 21, 2012, 10:46:39 PM »
Thanks for the post JRoch.

Yes that is what I am trying to do. This OnClick() I guess is something I write then and then attach as a separate script to each button?

Up till now I have been sending all my UIButtonMessages to the main source in each menu.

In a nut shell what you have described is what I am trying to do. However with this method, I would still have to create an instance of my menu object so I could send it there, or send another message.

What I am trying to do basically is to send a single message to OnClick but with args.

OnClick(int myint)

ie

--- UIButtonMessage(Script)
Script
Target
FunctionName
Args
Trigger
Include Children
---

JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: Sending Parameters within Buttons?
« Reply #3 on: November 21, 2012, 10:54:43 PM »
Well, let's back up a little and just talk abstract logic instead of a specific implementation.  So you have a bunch of buttons in a panel, and you want to have one piece of code that you can handle all the logic for all those buttons... that's pretty straightforward.

Can you provide a description of what you're trying to accomplish?  Why is it that you need to pass an int to another function when a specific button is pressed?  Is it because you're already coded up the function that wants an int?

JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: Sending Parameters within Buttons?
« Reply #4 on: November 21, 2012, 11:04:20 PM »
If you have a main class where you're doing all of your core logic and storing variables and whatnot, it's not strictly necessary to forward click events, in fact I'm not sure why you'd do that.  It would be better to simply call methods of that class that pass the data you need.  If your core logic class (or classes) aren't themselves completely static, you can at least store a handle to the instance in a public static variable, or something similar.

So instead of trying to cascade an OnClick event back to a different class, you instead simply call class methods.  Those methods, since you define and implement them, can have any type of parameters you like, include passing in/out by reference if it makes sense.

Keep in mind that every cscript you create is a class, and can contain additional classes.  I'm sure someone else who is more adept than I can detail why it might be good to use event messages between classes rather than directly calling methods.

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: Sending Parameters within Buttons?
« Reply #5 on: November 21, 2012, 11:11:08 PM »
Well judging from UIButtonMessage. I can't seem to find any other way of using NGUI to get messages to be sent to places other than just a function name.

The flick menu in question gets data from a server, it might be an e-mail message or a user profile. But whether it is either of those things, you are going to want to have some variables passing into a function.

All I can see at the moment is sending a message to a function without being able to pass in any variables. I might want an int, or a string so I can access a dictionary etc.

The best I can describe it as before, where I rename the function name to MyFunction1, MyFunction2 etc and have them all point to a MyFunction(int num)
« Last Edit: November 21, 2012, 11:14:29 PM by ENAY »

JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: Sending Parameters within Buttons?
« Reply #6 on: November 21, 2012, 11:15:59 PM »
I guess I'm not able to visualize in my head what you're trying to portray.  Can you sketch out in bullet points a simple sequence of events that you are trying to handle?

JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: Sending Parameters within Buttons?
« Reply #7 on: November 21, 2012, 11:29:47 PM »
I just looked at UIButtonMessage.  It's a canned script that you can use if you have simple needs, but there is no requirement that you use it to deal with UIButton events.  In fact, I've never used UIButtonMessage at all (that I know of, unless some other NGUI piece leverages that code.)

You can write your own button handler that includes the example code I posted above to figure out which button has been clicked, and then you simply invoke the methods of the target class you want.  I think you might have a somewhat loose understanding of Object Oriented Programming, which is getting in the way of your conceptualization of how these classes fit together, and how you can completely re-write pieces as needed.

For example, you could copy the UIButtonMessage.cs script file and rename it to MyUIButtonMessage.cs, change the internal class header to be "MyUIButtonMessage" and then implement an OnClick handler that has the parameters you want to pass.  Then attach MyUIButtonMessage to the object you want, and it would do exactly what you want.  There are a multitude of different ways to approach this functional task.
« Last Edit: November 21, 2012, 11:32:23 PM by JRoch »

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: Sending Parameters within Buttons?
« Reply #8 on: November 21, 2012, 11:35:25 PM »
Ok, let's simply say I have a simple e-mail list flick Panel.

I create a button "Read Message" and one UILabel.

I get from the server a number, it says for example 4, I have 4 e-mails. So I clone the collecting of one button and one uilabel 4 times.

Now I have cloned the objects 4 times, they are repositioned in the panel and scrolling works. Ok so far?

All the "Read Message" buttons are the same and point to the same function.
Let's say

"void GetEMailMessage()"

Unfortunately my function to get that e-mail message requires a pointer and my function is.

"void GetEMailMessage(int pointer)"

But none of the buttons are able to pass a parameter to this function, just call it.


To get around this I renamed each of the cloned objects to a number. It looks something like this.

0 - MyUILabel
  |
   --MyUIButton

1 - MyUILabel
  |
   --MyUIButton

2 - MyUILabel
  |
   --MyUIButton

3 - MyUILabel
  |
   --MyUIButton

All my buttons point to "void GetEMailMessage()"

So I grab the name of the object and int.TryParse it and then pass that into a different function.

My question is, is there a better way of doing this?

I hope this explanation makes sense :)

JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: Sending Parameters within Buttons?
« Reply #9 on: November 22, 2012, 12:02:06 AM »
Well, off the top of my head, you seem to need to attach another piece of metadata to your button gameobjects.  For example, you could add a tiny custom component like this:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MyButtonData : MonoBehaviour
  5. {
  6.  
  7.     private int _intValue = -1;
  8.  
  9.     public int ButtonIntValue
  10.     {
  11.         get { return _intValue; }
  12.         set { _intValue = value; }
  13.     }
  14.  
  15.     void OnClick()
  16.     {
  17.         MyMainClass.ButtonHandlerFunction(ButtonIntValue);
  18.         // or get an instance to a gameobject and call the appropraite class method
  19.     }
  20. }
  21.  

When you instance the button you have to take the gameObject and use .GetComponent<"MyButtonData"> and then set ButtonIntValue to be the integer you want.  In OnClick() we are using the int value that is stored in this component (which is a class, really) and then firing some static method that is part of MyMainClass.  You can also, instead, also store in a reference to the gameObject that is the panel owner, or whatever else, that might have the master logic handling class attached, and call that instance method.

If this is not making sense to you, then you really need to learn about C# and OOP at a basic level, because you seem to be trying to attack this via old C++ style functional coding or something.

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: Sending Parameters within Buttons?
« Reply #10 on: November 22, 2012, 12:36:12 AM »
Thanks a lot for all your help and advice JRoch.

I was tempted to just hack UIButtonMessage but I think I will rethink my approach a little. Or just stick with my kludge for now.

Thanks again :)