Initialised UObject always returns NULL?
What I am trying to do is to attach a UObject to my item class that is going to hold all the configuration for the class, the definition looks like this:
And in the constructor I initialise it like this:
But everytime I try to use the ItemConfig outside of the item class it is always NULL, this is the code I am using:
The last code shown always crashes the project because of the "Item->ItemConfig->Weight" part is always NULL.
Any help is appreciated!
asked May 08 '16 at 10:23 AM in C++ Programming
I had trouble too with a similar issue. I tried the NewObject function back then with outer being some actor that is spawned. My guess was and still is that the garabage collector believes it is no longer referenced although it is. So instead try to init is with NewObject. Some other post I read the other day recommended making the ItemConfig an actor. Though I would rather recommend this:
I ended up creating an actor component which held the data of ItemConfig and I attached the component to my actor that needed the ItemConfig. That works and was also recommended to me at that time.
Just beside the point, it might be a better design choice to place the data in an actor component anyways because components can be attached dynamically. This would allow more a more flexibile and maintable design. That depends on your project though and is just a thought I wanted to provoke.
I'm inclined to suspect Universe is right -- it looks like this is a case of garbage collection removing your object when you are still using it.
As an alternative to creating an Actor-based or component-based version of your data structure, you might consider either using a hard (non-pointer) reference to the structure (if possible), or adding the object to the root set. According to the documentation on the engine's object handling, either of these ought to prevent the object from being cleaned up by the garbage collection. I found adding the object to the root set to work perfectly in some work I was doing, though depending on the lifetime of your actors, you may need to clean up the data structure yourself when destroying the actors.
I hope this helps!
answered May 09 '16 at 07:32 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here