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"

Blueprint function returning wrong value

Hello,

I have a pure blueprint function that returns a value based on a simple condition.

alt text

Now when the condition is not met anymore and the function has no return node, the last valid path is returned. In the case of this example, when the condition is false, the node still returns a value of 150.

I wonder if this is expected behavior or a bug ? Either returning zeroes or a log warning would make more sense and be less prone to errors.

This can easily be reproduced and i have included a test project in the post that shows the issue.

Download Test Project for UE 4.21

Have a great day!

Product Version: UE 4.21
Tags:
return.png (27.7 kB)
returntest.zip (2.3 MB)
more ▼

asked Nov 15 '18 at 12:39 PM in Bug Reports

avatar image

HEGI
106 7 13 15

avatar image ThompsonN13 Nov 15 '18 at 03:02 PM

from a non programming perspective i would say that the value is statically set. i realize that the value shouldnt be set from a code perspective until the node is called but its probably one of those quirks of blueprint.

avatar image HEGI Nov 15 '18 at 03:06 PM

I think this is very prone to errors in Blueprint. Now after looking at the nativized code, i realized it uses a void function with a pass-by-ref parameter and not a float function(Which does make sense). In C++ the code below does the same as Blueprints, but there you would be alot more careful. It's not that big of an issue, just alot more convenient and easier to debug if you forget a return node in a complex blueprint function.

 void ReturnTestFunction(bool bCondition, float& ReturnValue)
 {
     if (bCondition)
     {
         ReturnValue = 150.0;
     }
 
     UE_LOG(LogTemp, Log, TEXT("Return Value: %f"), ReturnValue);
 }
 
(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Try to make it like this:

 float MyFunction()
         {
         float Result = 0;
         
         if (Condition)
         {
         Result = 150.0f;
         }
     
     return Result;
     }


more ▼

answered Nov 15 '18 at 02:07 PM

avatar image

redbox
4.2k 80 17 103

avatar image HEGI Nov 15 '18 at 02:22 PM

Adding another return node will instantly solve the problem. The blueprint should exactly work as the code you posted there. If the condition is not met, it would basically return the float as 0 without the need to define a extra local variable . Like having a virtual return node.

avatar image redbox Nov 15 '18 at 02:25 PM

Convert it into C++ with blueprint nativization and check how it looks there.

avatar image HEGI Nov 15 '18 at 02:37 PM

Interesting, the nativized code doesn't mess with the reference parameter at all when the condition is not met.

 void AReturnTestBP_C__pf1010915279::bpf__ReturnTest__pf(/*out*/ float& bpp__ReturnxValue__pfT)
 {
     int32 __CurrentState = 1;
     do
     {
         switch( __CurrentState )
         {
         case 1:
             {
                 if (!bpv__Condition__pf)
                 {
                     __CurrentState = -1;
                     break;
                 }
             }
         case 2:
             {
                 bpp__ReturnxValue__pfT = 150.000000;
                 __CurrentState = -1;
                 break;
             }
         default:
             break;
         }
     } while( __CurrentState != -1 );
 }

 

So basically it would have to look something like this for it to work properly.

                  if (!bpv__Condition__pf)
                  {
                      bpp__ReturnxValue__pfT = 0.000000;
                      __CurrentState = -1;
                      break;
                  }

avatar image redbox Nov 15 '18 at 02:51 PM

Well, if you don't want to use local variable, you can write like:

 float MyFunction()
 {
 
 if (Condition)
 {
 return 150.0f;
 }
 
 return 0.0f;
 }
avatar image HEGI Nov 15 '18 at 03:01 PM

Thanks. Just thinking for Blueprints it would be more convenient to be able to just skip the return path and have some sort of virtual return node. In C++ making such a mistake, like forgetting a return path is pretty much impossible.

(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