Announcement

Collapse
No announcement yet.

How To define a function with multiple return values as blueprint in C++

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How To define a function with multiple return values as blueprint in C++

    Hello, Community!

    I am working on some functions that can be used in both C++ and blueprints. I need some functions to be returned with multiple values.
    I know I can define a struct whenever I need multiple values to be returned in C++.(Many structs to define .....)
    Since the blueprint functions can be returned with multiple values, there must be some way that can do the same thing in C++.


    I am new to the Engine and C++,please help me out.

  • #2
    Blueprint functions with "multiple" return values are actually just C++ reference parametres to determine what the return values are.

    For instance:

    Code:
    UFUNCTION( Category=Foo, BlueprintCallable )
    void Foo( const FBar& Bar );
    Since the reference is const and the function therefore cannot modify it, Blueprint will treat it as an "in" param. Conversely:

    Code:
    UFUNCTION( Category=Foo, BlueprintCallable )
    void Foo( FBar& Bar );
    Since the reference is not const, you can modify it in the function, so Blueprint assumes that is the intent and treats it as "out". Continuing from there, you can have multiple non-const reference params:

    Code:
    UFUNCTION( Category=Foo, BlueprintCallable )
    void Foo( FBar& Bar, FVector& Baz, FRotator& Qux );
    All of these mutable params will be treated as "out" by Blueprint, resulting in multiple "return" values. This is how engine functions do it.

    -Camille

    Comment


    • #3
      Originally posted by cmartel View Post
      Blueprint functions with "multiple" return values are actually just C++ reference parametres to determine what the return values are.

      For instance:

      Code:
      UFUNCTION( Category=Foo, BlueprintCallable )
      void Foo( const FBar& Bar );
      Since the reference is const and the function therefore cannot modify it, Blueprint will treat it as an "in" param. Conversely:

      Code:
      UFUNCTION( Category=Foo, BlueprintCallable )
      void Foo( FBar& Bar );
      Since the reference is not const, you can modify it in the function, so Blueprint assumes that is the intent and treats it as "out". Continuing from there, you can have multiple non-const reference params:

      Code:
      UFUNCTION( Category=Foo, BlueprintCallable )
      void Foo( FBar& Bar, FVector& Baz, FRotator& Qux );
      All of these mutable params will be treated as "out" by Blueprint, resulting in multiple "return" values. This is how engine functions do it.

      -Camille

      Thank you, cmartel! I tried a little using the following code and it worked!!!

      UFUNCTION(BlueprintCallable, Category = "MyBlueprintFunctionLibrary")
      static void switchValue(const float a,const float b, float &a_out, float &b_out);

      void UMyBlueprintFunctionLibrary::switchValue(const float a, const float b, float &a_out, float &b_out)
      {
      a_out = b;
      b_out = a;
      }


      Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	15.8 KB
ID:	1061928

      Thank you so much!!!

      Comment

      Working...
      X