I have a class called ABuildingManager, which is derived from AActor.
If I try to access member variables of this class, even inside itself, it crashes the editor.
I have created an ABuildingManager object inside my PlayerController class like this:
BuildingManager = NewObject<ABuildingManager>();
Inside BuildingManager.h i have
int32 a = 5
and in my ABuildingManager::SpawnObject() method I do
a = a + a;
Obviously a = a + a is some nonsense code but it still crashes. If I access a member variable of ABuildingManager i get crash no matter what. I have never experienced anything like this before. What am I missing here?
Edit:
Spawning in world fixed the problem. I have no idea why.
Hey Metalwrath, are you getting a null pointer error? I had a crashing project I panicked about for a bit before realizing it was a null pointer error. Can you paste the stack trace?
Hello, its not a null pointer error. It either does not print anything or says unhandled exception. I can’t post right now as im not on my computer but I will post it tomorrow.
I assume BuildingManager is a member variable inside your PlayerController?
If it’s a raw pointer (ABuildingManager* BuildingManager) make sure it’s marked as UPROPERTY(), otherwise it will be garbage collected.
Secondly, I would suggest you use the SpawnActor-function instead of NewObject.
Although I’m successfully able to use NewObject on actors on my end without any crash, I’ve personally never done it.
Try replacing the NewObject call with
I have tried UPROPERTY() macro but nothing changed
Yes It is a member in PlayerController. There is no problem when I access BuildingManager through player controller. But I get a crash if I access a variable of BuildingManager, even from inside BuildingManager itself.
Thing is I don’t really want to spawn it in world. In other projects I always created objects in code without spawning in world (they don’t need to be in world in my game) and had no problems. It is really weird because i am doing exactly like I did before but i am getting crash for even accessing a member variable of BuildingManager class. Either I miss something obvious or something in my game bugged out.
Edit: Spawning in world fixed the problem. I have no idea why.
If you don’t want to spawn it in world - don’t make it an Actor. If you only require an object containing calculations, I recommend you don’t make it an Actor, but a UObject. An Actor is specifically for objects requiring an in-world representation. I recommend you read up on Unreal’s class hierarchies and what each class is used for