After C++ inherited BP compile: compiled object becomes invalid
I have a C++ UClass and inherited a BP from it. Another C++ class, which is a widget, not a UObject, has a pointer to an object of that UClass. When I manipulate it in the editor, it is fine. However, when I recompile the inherited BP while the widget is still open, the pointer that the widget owns becomes invalid and only when I close the window and re-open it or otherwise reconstruct the widget the pointer becomes valid again.
It appears that when recompiling, object references to objects of that class first become invalid while the compiler updates, and then get fixed again - with other UObjects. Since widgets (Slate) are not UObjects, my guess is that that pointer does not update its reference automatically because of it.
How do I tell my C++ system it needs to reconstruct after a BP compile? Or if there's another, proper way, how do I fix this? Right now I have a raw pointer to that UObject, would using a TWeakObjectPtr help?
EDIT: I got more information now: the object that is being accessed while it is invalid isn't a nullptr. The original class name of the BP class is "CameraEvent_C", however the one being accessed is "REINST_CameraEvent_C" which seems like my assumption correct; the old object is supposed to be reinstanced, but something goes wrong, probably because the widget isn't a UObject.
2nd EDIT: Wrapping the pointer as a TWeakObjectPtr results in the object being "STALE", and if accessed, a nullptr.To clarify the question: How do I properly update references to UObjects of objects that aren't UObjects, since compiling the BP of the latter results in reinstancing the objects, which seemingly doesn't work with non-UObjects.
Yes, your assumption is correct. When you compile a BP, all instances of that class are destroyed and re-initialized. The Editor has an "OnObjectsReplaced" delegate that is called when that happens (it passes you the old UObject* and the new one to replace it with).
I would use a TWeakObjectPtr and then have your slate object just poll every frame to make sure that object is valid (you can just call "IsValid()" on the WeakObjectPtr), and if not - to fix it up.
Or you can have your Slate widget register with OnObjectsReplaced and then fix it up that way.
answered May 22 '19 at 02:47 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here