UStruct Constructor

Is there a way of having a constructor in a UStruct? For example something like:

USTRUCT()
struct Weapon {
GENERATED_USTRUCT_BODY()	
	Weapon(FString n="", int32 d=0) : name(n), damage(d) {}
	UPROPERTY()
		FString name;
	UPROPERTY()
		int32 damage;
};
  • i updated the answer see below

I dont see a reason for struct constructors. Because most probably what you are aiming by using struct is extending blueprint functionality by creating a new type. Which means you need to have variables available to engine. Also there is a functionality provided by UE4 ‘make [struct name]’ inside blueprints. So no need for constructors.

By the way you may want to create your struct this way

USTRUCT(BlueprintType)
struct Weapon {
    GENERATED_USTRUCT_BODY()    

    UPROPERTY(BlueprintReadWrite)
    FString name;

    UPROPERTY(BlueprintReadWrite)
    int32 damage;
};

If you do so, you will be able to edit them inside blueprints

EDIT: I just remembered, you can only create default constructor or a constructor with FObjectInitializer as param inside a class. So most probably the same applies. Not sure though. But like i said. You dont need a constructor.

Better if you follow UE coding standards

You absolutely can have a constructor, just as in the code you posted. USTRUCT is a very lightweight reflected type and doesn’t really have any of the C++ level restrictions that you get with UCLASS. However, you need to prefix USTRUCT types with the letter F (see coding standards which Corpse0327 linked above). I believe in this case it is a requirement, not merely standard practice.

As for wanting a constructor - USTRUCTs are still C++ structs and can be used in C++ as well as exposed to blueprints, so there’s every reason to want to give them constructors. In fact I believe it is even necessary in order to initialize the properties properly, since I don’t think they are given default values automatically as they are in the case of a UCLASS.

Ok, got it. I had to add an ‘F’ prefix and put the struct into one of the headers generated by the editor so it looks kinda like that:

   #include "Kismet/BlueprintFunctionLibrary.h"
    #include "GameGlobals.generated.h"
    
    USTRUCT(BlueprintType)
    struct FWeapon {
    GENERATED_USTRUCT_BODY()
    	FWeapon(FString n = "", int32 d = 0) : name(n), damage(d) {}
    
    	UPROPERTY(BlueprintReadWrite, Category = Vars)
    		FString name;
    	UPROPERTY(BlueprintReadWrite, Category = Vars)
    		int32 damage;
    };

It seems to be working.

Actully you are free to use non-reflected structs and even classes, which in matter of fact engine using in it’s own code (So far i only see 2-3 cases of non-reflected classes, but there ton of structs) like FVector and FRotator which even have function in them which reflection system does not support functions in structs. Ofcorse by doing that those classes and structs won’t be managed by engine so you need to take care of cleaning them. There possibility of plugging them to blueprint via static blueprint functions in sperate UObject class, which is what engine do with it’s non-reflected structs.

Not sure about constructor in struct as UObject classes are so restrictive in terms of constructor because they are initiated in non-standard way (by holding default object CBO and copy it in memory), structs don’t have that, but i never tried using constructors yet.