Download

Add a StaticMeshComponent to an Actor

hi,
I simply want to add the cube shape as a StaticMeshComponent to an Actor

.h


UPROPERTY(Category = Character, VisibleAnywhere, BlueprintReadOnly)
TSubobjectPtr<class UStaticMeshComponent> CollisionComponent;
static FName CollisionComponentName;

.cpp



CollisionComponent = PCIP.CreateOptionalDefaultSubobject<UStaticMeshComponent>(this, ACheckPoint::CollisionComponentName);
if (CollisionComponent)
{
	CollisionComponent->SetStaticMesh(StaticMesh'/Game/Shapes/Shape_Cube.Shape_Cube');			
}

The red marked part dont work, but it should give you an idea what im trying to do. The question is how does it really work ?

Actor Components Wiki Tutorial

I have a wiki tutorial on this,

Let me know if that is not sufficient for some reason !

A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums,Making_Native%26_Deferred_Attached_to_Socket

Actor Components Tutorial

Thanks, It works :wink:

You’re putting function definitions and making function calls through pointers in the header file? I know VS2013 will allow it, but is that valid C++? Doesn’t all that belong in the cpp file?

Here is the better link about that, because this is the official documentation (search StaticMesh):

https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Classes/index.html

By the way, they also say that doing that is not the right thing to do. You may have to use Blueprint to define properties like that.

Any code you can have in a .cpp file you can have in a .h file. You could declare and define every function of a class in the .h file if you wanted to. It will increase your compile time dramatically though if you add all your code to .h files as it will have to parse that code for every other file that includes the header. However, the compiler tends to have an easier time inlining functions that are written in the header.

one more question about components…
My class extends AActor and ive added an component… but how can i attach the component to the actor ?
For now I can move the Actor but the component is not moving…


//Deferred Attachment (Ty Nick W.! Actual attach gets done after blueprint stuff)
JoyfulControl->AttachParent = Mesh;
JoyfulControl->AttachSocketName = FName(TEXT("JoyControl"));

or


//Deferred Attachment (Ty Nick W.! Actual attach gets done after blueprint stuff)
JoyfulControl->AttachParent = RootComponent;

Thanks RootComponent is what im looking for :slight_smile:
and Mesh is only for Pawn based classes ? right ?