I’ve been working on an inventory system and tried to get some restrictions for new items within blueprint defaults using the meta specifiers.
An item have, for example, a size property, which contains 2 integer values that shall be positive. (I could use unsigned integers, but then I would have to take care of many conversions and I would still need the metas for boolean value).
So, to meta specifers should take care of that:
meta = (DefaultValue = 1, ClampMin = 1)
On of those properties looks like this:
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "InventoryItem|MiscInfo", meta = (DefaultValue = "1", ClampMin = "1"))
As far as I understood, if I attempt to make a new blueprint derived from the class containing ‘ItemRarity’ as property, it should have a DefaultValue of 1 in the default section. But that doesn’t happen. Even so, if I try to type in a value below 1 or use the scale bar it adjusts accordingly to the ClampMin specifier.
Using the meta “EditCondition” works correctly, though.
I was wondering if I miss sth. or if its use is different from what I try to attempt here. Hope someone can clarify that
P.S.: EditDefaultsOnly may not have sth. to do with it; another property is of type boolean and has EditAnywhere but still doesn’t get initiated
I’m having the same question. I’d like to set default values for my UPROPERTY’s. ClampMin works, DefaultValue not. I can’t find any documentation about the meta tags. Are they even the way to do this? I find it hard to believe that nobody else in the last 9 months has tried to set default values for some blueprint variables…
It seems to me this is by design, and you’re supposed to use constructor to initialize UPROPERTies
Automatic Property Initialization
UObjects are automatically zeroed on
initialization, before the constructor
is called. This happens for the whole
class, UProperties and native members
alike. Members can subsequently be
initialized with custom values in the
From the wording of the documentation, that’s talking about run time, not editor use. A class constructor is not used by the Class UObjects except during runtime. What that quote is probably making UObject initalization clear. In many C++ compilers, if the programmer does not initialize a variable or object (without a default constructor), behavior is undefined.
And right after that:
UObjects and UProperties are
understood by the editor, and the
editor can expose these values
automatically for editing without the
need to write special code. This can
optionally include integration into
the Blueprint visual scripting system.
There are many options to control the
accessibility and exposure of
variables and functions.
This clearly indicates that the editor respects values, which supports the assumption that there must be a way to set default values for our custom classes.
Your mistake is in where you are putting your default value. I’m not sure DefaultValue is a standard meta tag, although perhaps it was when the question was written. At any rate, I am unable to find it in current documentation that describes it.
To set a default value, you can initialize the variable in your header.
int32 ItemRarity = 1;
You can remove “DefaultValue” meta tag. This assignment alone will give you your default value.
In C++, this often is not liked by compilers. I only tried it after failing to find any other solutions, and refusing to believe that forcing every variable to zero for default editor values could be “by design”.
By initializing your variables in your header, you can set a default values to something other than zero. As far as I can tell, this will only affect objects you place from this point forward. Objects already placed in the world, whether zeroed or customized to another value, will be unaffected. Class functions, including constructor, of course can override this “default” value with ease (unless your variable is a constant).