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

maxdev

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Passing Instance References Via EventDelegate
« on: April 17, 2014, 12:30:23 PM »
Hey guys,

I am making a combo system that gets an array of positions and spawns sprites.
I then move these sprites to the score on the top left of the screen via TweenPosition. When they reach the end of the tween, I'd like to run a function and pass the object that finished to it.

This is basically what works, however, I have no idea how to pass the gameobject(insc) to that function:
EventDelegate.Add(insc.GetComponent<TweenPosition>().onFinished, AddToScore);

I'd definitely love some input on this as I tried reading the docs and only found a way
to read parameters but not write.

Thanks very much!
Max

maxdev

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #1 on: April 21, 2014, 04:18:26 PM »
I tried several things to solve this and even watched a tutorial by Aren about this, but have yet
to find a way to do it purely though code. Any ideas will be appreciated.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #2 on: April 22, 2014, 04:36:20 AM »
UITweener.curent always tells you which tween triggered the callback, even without having to pass anything. If you need other custom parameters specified via a script, you can do it like so:
  1. EventDelegate.Parameter a = new EventDelegate.Parameter();
  2. a.obj = this;
  3. a.field = "someField";
  4.  
  5. EventDelegate.Parameter b = new EventDelegate.Parameter();
  6. b.obj = this;
  7. b.field = "anotherField";
  8.  
  9. EventDelegate del = EventDelegate.Add(insc.GetComponent<TweenPosition>().onFinished, AddToScore);
  10. del.parameters = new Parameter[] { a, b };

hexaust

  • Newbie
  • *
  • Thank You
  • -Given: 14
  • -Receive: 1
  • Posts: 35
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #3 on: May 19, 2014, 09:10:35 AM »
UITweener.curent always tells you which tween triggered the callback, even without having to pass anything. If you need other custom parameters specified via a script, you can do it like so:
  1. EventDelegate.Parameter a = new EventDelegate.Parameter();
  2. a.obj = this;
  3. a.field = "someField";
  4.  
  5. EventDelegate.Parameter b = new EventDelegate.Parameter();
  6. b.obj = this;
  7. b.field = "anotherField";
  8.  
  9. EventDelegate del = EventDelegate.Add(insc.GetComponent<TweenPosition>().onFinished, AddToScore);
  10. del.parameters = new Parameter[] { a, b };


When I try to do this I receive different errors:

  1. The best overloaded method match for `EventDelegate.Add(System.Collections.Generic.List<EventDelegate>, EventDelegate)' has some invalid arguments
  2. Argument `#2' cannot convert `method group' expression to type `EventDelegate'
  3. Property or indexer `EventDelegate.parameters' cannot be assigned to (it is read only)
  4.  

Also I can not use:
  1. del.parameters = new Parameter[] { a, b };
  2.  

it throws an error
  1. The name 'Parameter' does not exist in the current context.
  2.  

and I replaced it with

  1. del.parameters = new EventDelegate.Parameter[] {delTroop};
  2.  


What I am trying to do is to set the highlighted part (red) from code.
« Last Edit: May 19, 2014, 09:16:46 AM by hexaust »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #4 on: May 19, 2014, 01:22:25 PM »
Method group? What does your code actually look like?

hexaust

  • Newbie
  • *
  • Thank You
  • -Given: 14
  • -Receive: 1
  • Posts: 35
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #5 on: May 19, 2014, 01:28:14 PM »
I have method like:
  1. public void DoStuff(CustomType arg) {
  2.       // do something with arg
  3. }
  4.  

and on the button I run:
  1. EventDelegate.Parameter a = new EventDelegate.Parameter();
  2. a.obj = this;
  3. a.field = "someField";
  4.  
  5. EventDelegate del = EventDelegate.Add(button.onClick, DoStuff);
  6. del.parameters = new EventDelegate.Parameter[] { a };
  7.  

Maybe I am confused, what does "someField" represents?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #6 on: May 19, 2014, 05:16:06 PM »
This will only work if "this" is "CustomType", because that's what you are passing there in the EventDelegate.Parameter.obj. This also assumes that 'someField' is also of type CustomType.

hexaust

  • Newbie
  • *
  • Thank You
  • -Given: 14
  • -Receive: 1
  • Posts: 35
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #7 on: June 13, 2014, 03:54:50 AM »
Bleah, I can't get this to work. Can anyone help me with a functional example please?
Thank you.

 :'(

hexaust

  • Newbie
  • *
  • Thank You
  • -Given: 14
  • -Receive: 1
  • Posts: 35
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #8 on: June 13, 2014, 04:12:12 AM »
I get this error when declaring the EventDelegate:
  1. EventDelegate del = EventDelegate.Add(button.onClick, callback)
error CS0029: Cannot implicitly convert type `void' to `EventDelegate'

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #9 on: June 13, 2014, 05:49:41 AM »
It's a void function. It doesn't return anything. The error tells you exactly that.

EventDelegate.Add adds a callback to the event delegate list. There is nothing to return.

hexaust

  • Newbie
  • *
  • Thank You
  • -Given: 14
  • -Receive: 1
  • Posts: 35
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #10 on: June 13, 2014, 07:30:25 AM »
I'm sorry, but I don't understand. I took this from your example and I really can't get anything to work. All that I want is to set the method argument (if that's how it's called) by code. Again I'm talking about the red highlighted section:




It's a void function. It doesn't return anything. The error tells you exactly that.

EventDelegate.Add adds a callback to the event delegate list. There is nothing to return.

UITweener.curent always tells you which tween triggered the callback, even without having to pass anything. If you need other custom parameters specified via a script, you can do it like so:
  1. EventDelegate.Parameter a = new EventDelegate.Parameter();
  2. a.obj = this;
  3. a.field = "someField";
  4.  
  5. EventDelegate.Parameter b = new EventDelegate.Parameter();
  6. b.obj = this;
  7. b.field = "anotherField";
  8.  
  9. EventDelegate del = EventDelegate.Add(insc.GetComponent<TweenPosition>().onFinished, AddToScore);
  10. del.parameters = new Parameter[] { a, b };

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #11 on: June 13, 2014, 08:16:54 PM »
Ah, I see.

You can create a new delegate via
  1. EventDelegate del = new EventDelegate(callback);
You can then pass 'del' into the EventDelegate.Add(button.onClick, del);

I'll make the EventDelegate.Add(list, callback) function return an EventDelegate in the next update.

maxdev

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #12 on: June 21, 2014, 12:44:45 AM »
Hey guys, I for the life of me cant get this to work, partially because I have yet
to fully understand delegates and also due to some lack of examples for this case.

  1. EventDelegate.Parameter a = new EventDelegate.Parameter();
  2. a.obj = insc;
  3. a.field = "test";
  4.  
  5. EventDelegate.Parameter b = new EventDelegate.Parameter();
  6. b.obj = insc;
  7. b.field = "anotherField";
  8.  
  9. EventDelegate del = new EventDelegate(this.testyay);
  10. EventDelegate.Add(insc.GetComponent<TweenPosition>().onFinished, del);
  11.  
  12. del.parameters = new Parameter[] { a, b };
  13.  

  1. public void testyay() {
  2.         // get my gameobject insc
  3. }
  4.  

I get several errors:
Quote
error CS0246: The type or namespace name `Parameter' could not be found. Are you missing a using directive or an assembly reference?
Quote
Property or indexer `EventDelegate.parameters' cannot be assigned to (it is read only)

How can I get insc object in testyay function?

Thanks in advance!
« Last Edit: June 21, 2014, 01:06:19 AM by maxdev »

maxdev

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #13 on: June 21, 2014, 02:14:45 PM »
If I write this, it works. However how can I read insc variable in testyay function?
  1. IEnumerator SpawnCalc()
  2.         {
  3. GameObject insc = Instantiate(prefab, transform.position, Quaternion.identity) as GameObject;
  4.  
  5. EventDelegate del = new EventDelegate(this.testyay);
  6. EventDelegate.Add(insc.GetComponent<TweenPosition>().onFinished, del);
  7.  
  8. del.parameters.SetValue(insc, 0);
  9. }
  10.  

  1. public void testyay() {
  2. print(insc.name);
  3. }
  4.  

Thanks!
« Last Edit: June 21, 2014, 03:09:26 PM by maxdev »

maxdev

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Passing Instance References Via EventDelegate
« Reply #14 on: June 21, 2014, 03:34:46 PM »
Also I am getting a null ref on this line:

  1. del.parameters.SetValue(insc, 0);
  2.