Author Topic: Passing Instance References Via EventDelegate  (Read 10078 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #15 on: June 21, 2014, 04:05:40 PM »
del.parameters.SetValue(insc, 0) sets the value of the first parameter -- that is you need to actually have a parameter for it to work.

Give your function a parameter.

maxdev

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #16 on: June 21, 2014, 04:21:44 PM »
  1. IEnumerator SpawnCalc()
  2. {
  3. GameObject insc = Instantiate(prefab, transform.position, Quaternion.identity) as GameObject;
  4. insc.GetComponent<TweenPosition>().enabled = true;
  5.  
  6.         EventDelegate.Parameter a = new EventDelegate.Parameter();
  7.         a.obj = insc;
  8.         a.field = "test";
  9.  
  10.         EventDelegate del = new EventDelegate(this.testyay);
  11.         EventDelegate.Add(insc.GetComponent<TweenPosition>().onFinished, del);
  12.  
  13.         del.parameters.SetValue(a, 0);
  14. }
  15.  

  1. private void testyay(EventDelegate.Parameter f)
  2. {
  3. print(f.obj.name);
  4. }
  5.  

Why doesnt this work? I'd very much appreciate a working example since it seems to me like I've tried
every combination I can think of or find and it still doesnt work...
« Last Edit: June 21, 2014, 04:27:16 PM by maxdev »

trenrod

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 4
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #17 on: June 22, 2014, 07:03:59 PM »
I got a working example its all in one class called MyClass

  1. public class MyClass: MonoBehaviour {
  2. ...
  3.     // Used to make a reference to the value of the parameter for the method you want to call.
  4.     // Used for EventDelegate.Parameter.Set(...
  5.     public string ITEM_VALUE = "XXX";
  6. ...
  7.  
  8.     public void init(){
  9.        
  10.         //EventDelegate to call method test in the same class with a given parameter where the value is ITEM_VALUE
  11.         EventDelegate eventDel = new EventDelegate();
  12.         //Target object
  13.         eventDel.target = this;
  14.         //Method name
  15.         eventDel.methodName = "test";
  16.        
  17.         //EventDelegate.Parameter for the parameter in the methode defined before
  18.         //First parameter is the object which contains the attribute wich has the name of the second parameter
  19.         EventDelegate.Parameter p = new EventDelegate.Parameter(this, "ITEM_VALUE");
  20.         eventDel.parameters.SetValue(p, 0);
  21.         EventDelegate.Set(isbn.GetComponentInChildren<UIButton>().onClick, eventDel);
  22. ...
  23.     public void test(string panelName){
  24.         Debug.Log("Test was pressed: " + panelName);
  25.     }
  26. ...
  27.  

Output after a click on the button:
Test was pressed: XXX

Drop a comment if you cant to adjust it to your exsample.

Have fun.

Edit: comments
« Last Edit: June 22, 2014, 07:12:22 PM by trenrod »

maxdev

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #18 on: June 22, 2014, 08:47:41 PM »
Thanks very very much trenrod, it works perfectly!