StaticMeshComponent

Hello,

i want to write Pure C++ function in my Actor class which will create me Static Mesh Component with Sphere Collision for which i can change radius float which is 150.000000f in my case.

Blueprint example:

Blueprint code example:

C++ Header file:

i just included Components/StaticMeshComponent.h and from there im lost.

I hope you guys understand me, english is not my main language. Im newbie and i want to learn this engine without any blueprints.

Thanks!

First of all, don’t ever look at nativised generated code to learn.

Secondly, i highly recommend a C++ course to learn the basics. Then a UE4 C++ course to understand some core concepts of the engine and its C++.

Third, to your actual question:

Remove that include you added, now in that same header file, add

UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
class UStaticMeshComponent* StaticMeshComponent;

UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
class USphereComponent* SphereCollision;

now in your .cpp file, you will see the “constructor”,

AInteractable_CPP::AInteractable_CPP()
{
	PrimaryActorTick.bCanEverTick = true;

}

add the following lines;

StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>("StaticMeshComponent");
RootComponent = StaticMeshComponent;
SphereCollision = CreateDefaultSubobject<USphereComponent>("SphereCollision");
SphereCollision->SetupAttachment(RootComponent);

so finally it looks like:

AInteractable_CPP::AInteractable_CPP()
{
	PrimaryActorTick.bCanEverTick = true;

    StaticMeshComponent= CreateDefaultSubobject<UStaticMeshComponent>("StaticMeshComponent");
    RootComponent = StaticMeshComponent;

SphereCollision = CreateDefaultSubobject<USphereComponent>("SphereCollision");
SphereCollision->SetupAttachment(RootComponent);
}

Now add to your cpp file, the following includes

#include "Components/SphereComponent.h"
#include "Components/StaticMeshComponent.h"

Now close editor, compile and reopen editor, and you will see your actor will have the static mesh component.q

Thanks, i didn’t noticed that i included in header file. But where is Collision Sphere and how do i add radius so i can tweak it from code?

Collision Sphere is SphereComponent.

SphereComponent->SetSphereRadius(150.f);

I really suggest you go learn some C++ and then do some UE4 C++ tutorials.