How can FVector pretends as USTRUCT?

In Vector.h, FVector isn’t declared as USTRUCT. But in Blueprint, it works as USTRUCT. How is it possible? Can I make a similar one?

Of course you can make a fake USTRUCT. The trick is to tell UHT to generate the reflection code without mess your own structure.

#if !CPP
USTRUCT(immutable, noexport, BlueprintType)
struct FTestVector
{
	UPROPERTY(EditAnyWhere, BlueprintReadWrite)
	float X;

	UPROPERTY(EditAnyWhere, BlueprintReadWrite)
	float Y;
};
#endif

struct FTestVector
{
	float X;
	float Y;
	void HelloVector() 
	{
		X = Y = 0;
	};
};

219373-test.png

See that? check NoExportTypes.h to see what happened in the Core module.

1 Like

I played around more, I am even more confused. I wrote this function inside a blueprint function library:

UFUNCTION(BlueprintPure)
static FVector TestFunction(FVector Vec);

FVector UGameplayAttributesLibrary::TestFunction(FVector Vec) {
	return Vec.Projection();
}

It can take in an FVector. When I call this from Blueprint, it will take in the FVector that is given in NoExportTypes.h. That FVector does not have a Projection method, but Unreal somehow understands we are talking about the C++ Vector and does the conversion. Then I return an FVector, which is the C++ FVector, but it converts back to the blueprint FVector so that I can set it inside blueprint. This is probably what the reflection system is meant to do from the start, I just never saw a class/struct be defined in two different places and used simultaneously. Very interesting.

Hello,
This is very interesting, can you elaborate on what is happening here? If I set float X to some value, which value am I really setting? How does float X in the USTRUCT and in the normal struct synchronize?

Check NoExportTypes.h . There is a another defination of FVector and it was marked as UStruct. So UHT will generate code for FVector in NoExportTypes.h .
You may confused about why there are two definations of FVector. and how does it compiled pass? Actually, NoExportTypes.h has a CPP Macro, NoExportTypes.h will just contains some cpp headers when compile. So NoExportTypes.H is just for UHT to generate code.