Results 1 to 8 of 8

Thread: Assign a Delegate Function declared in another class???

  1. #1
    UDK_lover
    Guest

    Question Assign a Delegate Function declared in another class???

    Hi and thanks for coming!

    Now suppose I have this:
    Code:
    class MyClass extends Object
    
    var delegate<MyDelegate> MyCallBack;
    
    delegate MyDelegate();
    
    
    function MyFunction()
    { 
        `log("MyFunction called!");
    }
    
    
    DefaultProperties
    {
       MyCallBack=MyFunction
    }
    That works...

    But is it possible to have MyFunction() declared in an other class instead of MyClass? How would "MyCallBack=MyFunction" be written instead?


    Thanks a lot and don't hesitate to ask for clarifications!

  2. #2
    evr
    Guest
    I haven't worked with delegates in UnrealScript but from what I can tell by just reading http://udn.epicgames.com/Three/Unrea...Delegates.html it seems very possible. The class you're going to have MyFunction() in needs to have a reference to the object with the delegate, then it can just set it.

    Code:
    class MyClass extends Object;
    
    var delegate<MyDelegate> MyCallBack;
    
    delegate MyDelegate();
    Code:
    class ClassWithFunction extends Object;
    
    function SetCallback(MyClass Other)
    {
    	Other.MyCallBack = MyFunction;
    }
    
    function MyFunction()
    {
    	`log("MyFunction called!");
    }

  3. #3
    UDK_lover
    Guest
    Thanks very much!

    So it's not possible to seperate the SetCallback() from MyFunction?

  4. #4
    evr
    Guest
    What do you mean? If you mean that you still want to set it with default properties, then no. The reason being that you always need to have an object with the function the delegate is pointing to and an object where the actual delegate is. If these objects aren't the same (which they are in your first example), then they can't be set with default properties simply because there is no way for a default property to have a reference to an object (except a reference to itself, which again applies to your first example).

  5. #5
    evr
    Guest
    If you mean that you just want to seperate SetCallback() and MyFunction() so they are in different classes, then yes you can. You still need a reference to an object which has MyFunction() though.

    Code:
    function SetCallback(MyClass Other, ClassWithFunction OtherWithFunction)
    {
    	Other.MyCallBack = OtherWithFunction.MyFunction;
    }
    Like I said, I haven't worked with delegates in UnrealScript but this is how I understand it works. You'll have to try it out though.

  6. #6
    UDK_lover
    Guest
    I tried doing Other.MyCallBack = OtherWithFunction.MyFunction; and it said Invalid or unknown function 'OtherWithFunction'

  7. #7
    evr
    Guest
    Okay, well try either this...

    Code:
    function SetCallback(MyClass Other, ClassWithFunction OtherWithFunction)
    {
    	Other.MyCallBack = delegate<MyDelegate>(OtherWithFunction.MyFunction);
    }
    Or this...

    Code:
    function SetCallback(MyClass Other, delegate<MyDelegate> theFunction)
    {
    	Other.MyCallBack = theFunction;
    }
    The last one really should work, it's basicly taken directly from UDN. Just pass any function with the same footprint as MyDelegate.

  8. #8
    UDK_lover
    Guest
    It compiled with your last example! thanks so much!!

    Only thing is I had to put deletage<MyClass.MyDelegate> instead of just deletage<MyDelegate> because it couldn't find it in that class

    Thanks again!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •