Expose struct operators to Blueprint

Hi there!

I have structs defined in c++ and want to expose custom operators to Blueprint (like at least adding two structs).

Is this even possible? I can’t mark them as UFUNCTION as USTRUCT cannot have those.

Thanks and cheers

yes :

USTRUCT(BlueprintType)
struct FexampleStruct {
	GENERATED_BODY()

	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = example)
              float exampleFloat;

I was talking about operator overloading like so

	FORCEINLINE operator FVector() {
		return FVector(X, Y, Z);
	}

I want to be able to use +*/ and type conversion operators in BP.

Sorry for reviving a necro post but i’m looking to do the same thing…

Hey there, happy to answer this!

Most (In fact I think all) of the binary operators in Blueprint are written in quite a similar way. Let’s take FVector as an example.

FVector declares FVector FVector::operator*(float Scale) const in Vector.h, and UKismetMathLibrary declares the following static method which is exposed to Blueprint in KismetMathLibrary.h:

/** Scales Vector A by B */
UFUNCTION(BlueprintPure, meta=(DisplayName = "vector * float", CompactNodeTitle = "*", ScriptMethod = "MultiplyFloat", ScriptOperator = "*;*=", Keywords = "* multiply"), Category="Math|Vector")
static FVector Multiply_VectorFloat(FVector A, float B);

The implementation of this static method is simply return A * B;.

Really, this is all you need. But you probably also want to make the node look and behave like the other operator nodes in Blueprint. To do that, you’ll need to make sure you have these three Specifiers in you UFUNCTION macro’s Meta=():

  • DisplayName: Human readable friendly name. This is what appears in the Blueprint context search
  • CompactNodeTitle: This is what makes the node appear like all the other operator nodes you’re already familiar with
  • Keywords: A space separated list of words to help this node show up in a context search

CompactNodeTitle is the one you need for this to work, but DisplayName and Keywords will make yours (or your designers) life easier too!

That should be it, hope this helps!

P.S. For anyone wondering, ScriptMethod and ScriptOperator expose the method to Python as a member method of the FVector type

1 Like

Just see this after soome while. The post is a year old too!

Thank you for taking the time to answer! I’ll remember to give it a shot once I stumble upon this again!