Download

UPROPERTY and consts

And the issues continue.

I have a couple of constant values that are defined in the C++ code that I want exposed to blueprints so that they can use them. My current plan is to stick them as const values inside my derived GameMode class and use the UPROPERTY macro to flag them as BlueprintReadOnly. What I would really like is to make them “static” as well, but I’ll take constant instance variables. The problem however is UPROPERTY seems to be buggered with const. More specifically, the documentation mentions a Const specifier that I assume I need to use to tell Unreal that the variable is const. But when I attempt to do the following:

UPROPERTY(Const, BlueprintReadOnly)
const float ConstantParameter = 924375700.f;

I get the error:

Unknown variable specifier ‘Const’

As if UPROPERTY does not know what Const is. Which is, mildly irritating to say the least. This is in Unreal 4.6. My alternative is to specify the variables inside the class and provide Getter methods that are then exported with the UFUNCTION macro, but that seems heavy handed considering using UPROPERTY should by all rights just work. Anyone know if this is a bug or if I’m just misunderstanding how to use UPROPERTY?

My C++ Knowledge is not great (I`m still learning) but you should not need the const at the beginning, the blueprint wont be able to change the value as it is read only.

I think you should have in the .h file:

UPROPERTY(BlueprintReadOnly)
float ConstantParameter;

Then in the .cpp:

ConstantParameter = 924375700.f;

Hope that helps if im incorrect sorry just a suggestion :slight_smile:
Also you may need to put float in front of ConstantParameter in the .cpp im not sure

The purpose of the const is to prevent other C++ code from messing with the values. If I didn’t need this exposed to blueprints, I’d just use a #define and be done with it. Since I do need to make the values available to blueprints however, I need to basically figure out how to cheat.

I would imagine you want to use the BlueprintReadOnly property specifier.

Defining a function to return the values is my workaround, but it feels bloody cumbersome. The problem right now is that UPROPERTY itself doesn’t seem able to understand what the Const meta is. Whenever I try to use it in a UPROPERTY, the compiler errors out claiming Const is an unknown variable specifier. This feels like a bug in 4.6.1.

This is indeed a limitation of our header file parsing in 4.6.1. We don’t yet support const member variables, even if your intent is for them to be read-only yet serialized and visible to Blueprints. I’ll talk to our Core team and see if we can’t get it fixed in an upcoming release. Thanks for reporting it, zwabbit!

–Mike

Any news on this?
If we set value as BP read only won’t help much since even when the field is grayed out denying input, user can right click and use copy/paste on a value that should be fixed (but visible); that way ReadOnly fields can be change within editor which is quite ‘errr’…

I know this is the old post, but is it still not resolved? I’ve tried and have same problem for 4.24.3