I made my own pawn class because I needed a movement component and didn’t want to use DefaultPawn. I have added two components in code to this class. UPawnMovementComponent and UCameraComponent.
I go into the editor and make a blueprint that inherits from my custom class. I drag and drop an instance of this new blueprint. The blueprint appears as a camera because of the camera component, which is fine, but it cannot be moved and when I delete the instance it actually deletes my components from the blueprint editor. They show as unnamed right when I delete it, then upon reopening the blueprint editor all components are gone. The blueprint that I create does say Is Data Only: True. I feel that my problem is related to that, but I’m not sure how to make the BP not data only.
Two questions:
Why won’t this BP move in the editor, and why do the components get deleted along with the instance?
Alright. I’ve figured out that you must define a root component even though the editor makes one by default upon creating the blueprint. However, upon deletion of the editor instance of this blueprint, it still deletes all of my component. Why?
I took a look through a bunch of tutorials and saw that they include some form of BlueprintReadOnly in the UPROPERTY() macro. Well, my components didn’t have that. I can only assume that if you don’t have that tag, your components get garbage collected for some reason, because I added it and now I can delete the spawned object and the components remain.
To summarize, you must define a root component in the .CPP like this:
//Set root component. If you don't do this the editor takes a ■■■■ and won't move anything.
RootComponent = Root;
With Root being a USceneComponent I made.
I declared the components like this to prevent them from what I assume to be garbage collection:
//Declare Components. BlueprintReadOnly is very important for garbage collection. REMEMBER IT!
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
TSubobjectPtr<USceneComponent> Root;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
TSubobjectPtr<UCameraComponent> Camera;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
TSubobjectPtr<UPawnMovementComponent> Movement;
The BlueprintReadOnly tag is what stopped them from being deleted.