Hi,
I want to have a UProperty as a Member of my Class. However I do not mean having a c++ variable, that is reflected as a UProperty like this:
UPROPERTY(EditAnywhere, blablabla)
float member;
but having the member itsself be the UProperty like this
UProperty member;
or as a pointer of course. I want to use it to store a varibale of dynamic type without templating the whole class. Is this possible? If so, how do I create a UNumericProperty for example? I have tried
NewObject<UNumericProperty>();
and
CreateDefaultSubObject<UNumericProperty>("member");
but I dont really understand where UProperty belongs in this context (if it as treadet as a UObject or what else)
I have a hunch this would not work. Instances of UProperty are associated with UClasses, not with instances of UObjects. You may be better off using either a template class or a TUnion. Maybe describe a little more what you are trying to accomplish?
I am writing a framework to bind an input to several actions that go like this:
press key-> add x1 to y1, set x2 to y2.
press other key-> set x3 to y3 and so on
With x being a u property, I can use this on members of any class and any type which is quite convenient.
I do this by having the inputAction call a method that calls a multi-cast delegate. In this delegate I store pointers to x and y as payloads, and the actions as a function call.
What I wanted to use this specific construct for is to have an easy way of serializing it:
I currently save the name of x (since it’s a uproperty) and the tag of its component (which I will keep unique). I can then just find all my pointers again by searching for the tag and the property name. I still have to save y though. This question basically would just have avoided having a single serialization-class for each type.
This is however not a big issue, I just wanted to know if it was possible. I think I will simply template the class (unless you have a better suggestion). Thank you for your answer.
I just realized I can call the delegate directly from the action, since executing it is done using the broadcast function
It is, I think, technically possible to reference UProperty of an existing class in another class, but I assume a reference to a UProperty is not reliable as it will change if the class is modified either from compiling c++ code or compiling blueprint.
The safer way to do it is to save the UClass where the property is in along with the property’s Name. Both are guaranteed to be serialized properly and getting the UProperty instance is trivial if you have those two.
Creating UProperty on the fly just like what you plan to do, however, is not possible. UProperty is a special object that is stored as a linked list in a class, it is part of the reflection system that works like a map to get a certain value from a a certain offset in an object’s byte data. UProperty that is created outside of the reflection system will not work properly and will be a dangerous map to a value in an object, and I just don’t see it as implementable without changing the engine code.