Dissecting a basic UE4 class in C++ - Avoiding Taboos


After reading a lot and watching some videos, I’m happy to be having a smooth progression in UE4.
Still, there’s something that concerns me: I don’t like the idea of doing something because someone told me to do it, without understanding exactly what’s going on, so I decided to ask here.

I apologize if this is trivial for you guys, but this is so specific I’m having a hard time finding the magic search string to get a topic that explain to me what’s going on.

So, this is the most basic class, from what I understood from what I read:

class HAVEN_API ABaseBarrel: public AActor

	ABaseBarrel(const FObjectInitializer& ObjectInitializer);

	/** Simple collision primitive to use as the root component. */
	UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Barrel)
		USphereComponent* BaseCollisionComponent;

	/** StaticMeshComponent to represent the pickup in the level */
	UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Barrel)
		UStaticMeshComponent* BarrelMesh;


And, of course, it’s constructor:

ABaseBarrel::ABaseBarrel(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
	// Create the root SphereComponent to handle the pickup's collision
	BaseCollisionComponent = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("BaseSphereComponent"));

	// Set the SphereComponent as the root component
	RootComponent = BaseCollisionComponent;

	//Create the static mesh component 
	BarrelMesh = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("BarrelMesh"));

	//Turn physics off for the static mesh

	//Attach the StaticMeshComponent to the root component

So, there are 2 questions I’d like to ask:

First question:
Why do I need a [FONT=Courier New]USphereComponent* BaseCollisionComponent ?
I mean, I’ll ultimately want to use a mesh collision, and I’d hate to have something think (example: a wood plank) with a small collision sphere in it’s middle. Is this really necessary?
Sadly, I don’t know how to visualize this kind of collider within the editor.
Plus, if this is actually what happens, there will be a bunch of extra collision stuff scattered across the level, which might not only lead to strange collisions, but might also be extra CPU time going to waste.

Second question:
According to the C++ tutorial video, I’d need a [FONT=Courier New]UStaticMeshComponent* BarrelMesh to hold my mesh.
That’s fine, until I had to do some math with it.
I noticed that every single actor out there has a [FONT=Courier New]StaticMesh component (that you change in the editor) and I couldn’t really edit [FONT=Courier New]BarrelMesh by itself, in the editor.
BUT the mesh position and rotation wasn’t linked to [FONT=Courier New]StaticMesh, it was linked to [FONT=Courier New]BarrelMesh! - I feel this is confusing.

Is that [FONT=Courier New]UStaticMeshComponent* BarrelMesh component required there? How would I proceed if I were to remove it and have my class without it?

I apologize in advance if these aren’t the best formulated questions you ever got, but I’m a bit confused with both elements in a basic class. I hope you can explain to me why these are needed and how to proceed if they are not.

Thank you in advance!

  1. You don’t need a specific component for actors. In fact you can have actors without any components at all. However an actor does need a root component if you want to place it in the world, as it provides the actor’s transformation and usually that is also the component that has the collision. You can however mix and match it how you want. If your mesh should be the only collision and only component then there is no reason not to have it as the root component.

  2. Mesh Components contain all the information for the rendering and collision system to use a mesh. The mesh itself contains data that is transformed by the component, i.e. scaled or rotated. If you were to use only the mesh then the engine would need to load a new instance for every mesh you want to render, but with this system the mesh data only needs to be loaded once and all deviations from the default data set are handled by the component.

You can in fact edit the mesh of the component in the editor, simply replace VisibleDefaultsOnly with VisibleAnywhere or VisibleInstanceOnly, which tells the editor that this property can be seen on instances placed in the level.