Results 1 to 8 of 8
  1. #1
    MSgt. Shooter Person
    Join Date
    Mar 2011
    Location
    Quebec, Canada & Florida, USA
    Posts
    279

    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!
    Last edited by UDK_lover; 07-17-2012 at 10:06 PM.

  2. #2
    The Sacrifice
    Join Date
    Jul 2011
    Location
    Sweden
    Posts
    370

    Default

    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
    MSgt. Shooter Person
    Join Date
    Mar 2011
    Location
    Quebec, Canada & Florida, USA
    Posts
    279

    Default

    Thanks very much!

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

  4. #4
    The Sacrifice
    Join Date
    Jul 2011
    Location
    Sweden
    Posts
    370

    Default

    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
    The Sacrifice
    Join Date
    Jul 2011
    Location
    Sweden
    Posts
    370

    Default

    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
    MSgt. Shooter Person
    Join Date
    Mar 2011
    Location
    Quebec, Canada & Florida, USA
    Posts
    279

    Default

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

  7. #7
    The Sacrifice
    Join Date
    Jul 2011
    Location
    Sweden
    Posts
    370

    Default

    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
    MSgt. Shooter Person
    Join Date
    Mar 2011
    Location
    Quebec, Canada & Florida, USA
    Posts
    279

    Default

    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!


 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Copyright ©2009-2011 Epic Games, Inc. All Rights Reserved.
Digital Point modules: Sphinx-based search vBulletin skin by CompletevB.com.