Bad function definition in C++ function library using USTRUCT

Hi peeps,
First time coding some C++ code, I’m currently trying to create a simple way to project a box to my screen. I’m therefore trying to make a function that takes in a UBoxComponent and gives me a FIntRect that surrounds my box.

Unfortunately, I am having trouble with the definition of the function with the USTRUCT FIntRect, the compiler gives me a “Bad function definition” in my .h that I don’t really understand.

Here are the codes :
.h

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Runtime/Core/Public/Math/IntRect.h"
#include "BlueprintFunctions.generated.h" 


/**
 * 
 */
UCLASS()
class INTERSTELLARQUEST_API UBlueprintFunctions : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

    UFUNCTION(BlueprintCallable, Category="Custom functions")
    static USTRUCT *IntRect boxProjection();
	
};

.cpp

#include "BlueprintFunctions.h"


USTRUCT *FIntRect UBlueprintFunctions::boxProjection()
{
    FIntRect test = FIntRect(1,1,2,2);
    
    return &test;
}

I also have a" Use of undeclared identifier ‘UBlueprintFunctions’" and “Unknown type name ‘USTRUCT’” showing in my cpp file in Xcode that i can’t explain.

Thank you in advance !

static USTRUCT *IntRect boxProjection();

That is wrong.
UStructs are value types, you should do:

UFUNCTION(BlueprintCallable, Category="Custom functions")
 static FIntRect boxProjection();


FIntRect UBlueprintFunctions::boxProjection()
{
     return FIntRect(1,1,2,2);
}

Hey there, try this instead:

.h

FIntRect & boxProjection();

.cpp

FIntRect & UBlueprintFunctions::boxProjection()
{
	FIntRect test = FIntRect(1, 1, 2, 2);

	return test;
}

Thanks for your answer, I now have an "Unrecognized type ‘FIntRect’ - type must be a UCLASS, USTRUCT or UENUM.

This was the reason I had added the USTRUCT before the FIntRect. I’m going to see if i can find a fix for that in the forums.

Thanks for your answer, I now have an "Unrecognized type ‘FIntRect’ - type must be a UCLASS, USTRUCT or UENUM.

This was the reason I had added the USTRUCT before the FIntRect. I’m going to see if i can find a fix for that in the forums.

If that is the case then you might be missing some core includes. Another thing, you might need to get rid of the & sign on the return value, the & is more efficient because it’s a direct reference to what you’ve created (instead of a copy), but it might get garbage collected, so you have to see if you run into problems, just take it out.

When you say core includes, it’s in my files or in the main project files?

FIntRect is a structure from Unreal Engine, so you need to have includes that tell the compiler where to find the definition for it. Worst case you can try to include it manually like:

#include "Runtime/Core/Public/Math/IntRect.h"

I have included this library already, and it compiled fine if I used the FIntRect within the function (as long as it’s not in the header following the UFUNCTION declaration).

This compiles :

.h
UFUNCTION(BlueprintCallable, Category="Custom functions")
    static int boxProjection();

.cpp
int UBlueprintFunctions::boxProjection()
{
    FIntRect test = FIntRect(1,1,2,2);
    return 1;
}

I figured it’s something to do with those two posts but I didn’t get it to work yet.

Did you add the include for the library on the cpp or the h?

It is in the .h as shown in my code above, and the error occurs in the .h on the line I define the function.

I didn’t need to include that, see if you on your project’s header file you have EngineMinimal.h, try Engine.h to see if it helps.

I tried using Core.h without success.

And Engine.h?

Still has the same error. By the way, I really wanted to thank you for taking the time to help me out here.

I checked engine source code and FIntRect is a C++ type. Not exported as BlueprintType.
You won’t be able to use it as a UPROPERTY inside your own module.

You can however copy FIntRect code from IntRect.h and .cpp to your own code module and attach USTRUCT(BlueprintType) to it.

Thank you very much for that answer, I understand why it does not work now! I’ll try to work around that.
Thanks for your time !

Glad to know it’s fixed :slight_smile:

Will be fixed yet, but I understand what I did wrong so it’s going to be soon !
Thanks for your help and have a good day !