x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

C++ Callback

Hey guys, Im wondering, is there an easy way to pass method as callback to another method in C++? Mostly, Im interested in the way like it could be done in C# with Action delegates, like:

 void main()
 {
      MethodWithCallback(CallbackMethod);
 }

 public void MethodWithCallback(Action callback)
 {
     //When all is done, call callback
     callback();
 }

 public void CallbackMethod()
 {

 }


I guess it would be done with Delegates, but not sure isnt there a simplier way here? Thanks!

Product Version: UE 4.15
Tags:
more ▼

asked Mar 21 '17 at 06:24 PM in C++ Programming

avatar image

Polosatiy
0 1 3 3

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

3 answers: sort voted first

Hi. You can use Unreal delegates itself:

  • Declare your delegate type using DECLARE_DELEGATE_* macro family.

  • Declare your function somewhere you want: void FunctionWithCallback( FDelegateType Callback );

  • Call it then: FunctionWithCallback( FDelegateType::CreateSP( SharedThis( this ), some args ) );

A delegate could be created from function using static methods of created delegate type:

  • CreateSP makes a delegate bound to something placed into the shared pointer.

  • CreateStatic allow you to bound a delegate to the static function

  • CreateRaw allow to use a raw pointer to an object and it's method as a function

  • CreateLambda allows to use c++ lambda functions

  • CreateUObject allows to bound delegate to a managed UObject

Also for simple callbacks you can use a function pointer style:

 void FunctionWithCallback( void(*Callback)(int, float) ) { Callback(1, 2.0f); };




more ▼

answered Mar 21 '17 at 06:45 PM

avatar image

c4tnt
1k 13 8 31

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

It sound like what you want are just raw C++ function pointers. You can learn more about them here

more ▼

answered Mar 21 '17 at 06:33 PM

avatar image

pulp_user
1.2k 46 29 59

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Nice! Guys, thanks a lot! This is exactly what I have been looking for!

Thanks a lot!

more ▼

answered Mar 21 '17 at 07:37 PM

avatar image

Polosatiy
0 1 3 3

avatar image pulp_user Mar 21 '17 at 11:24 PM

Glad I could help! Please mark one of the answers as correct, so this question is marked as solved.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question