Download

Add a mesh to a c++ class

I am asking this again as the last post that asked this has the old engine versions way of implement this. Also is it possible to add a mesh that can be changed in Blueprints? Thanks in advance

You can add a mesh by using Components, all default components are created the same way.

First, inside your header file you need to define your component, in this case I’m going to assume you mean a “Static” Mesh, but this works the same for Skeletal Meshes.

YourClass.h



UPROPERTY(VisibleAnywhere,Category = "Mesh") //This adds it so you can see it in Blueprints
UStaticMeshComponent* StaticMeshComponent;


That’s easy enough, but it still needs initialized inside your default properties.

YourClass.cpp




AYourClass::AYourClass(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
  StaticMeshComponent = ObjectInitializer.CreateOptionalDefaultSubobject<UStaticMeshComponent>(this,TEXT("NameYourComponentHere"));
}


This will create a blank Component that you can then assign a static mesh to inside the editor using a Blueprint, or other means if you wish.

[EDIT]: What you told me doesn’t work :confused: :


 Tests.h:

  Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"
#include "Tests.generated.h"



DECLARE_LOG_CATEGORY_EXTERN(MyLog, Log, All);
UCLASS()
class MYPROJECT2_API ATests : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ATests();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;
	
	UPROPERTY(VisibleAnywhere, Category = "Mesh") //This adds it so you can see it in Blueprints
		UStaticMeshComponent* StaticMeshComponent;


};



 Tests.cpp :

// Fill out your copyright notice in the Description page of Project Settings.

#include "MyProject2.h"
#include "Tests.h"

DEFINE_LOG_CATEGORY(MyLog);
// Sets default values
ATests::ATests()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	
}

// Called when the game starts or when spawned
void ATests::BeginPlay()
{
	Super::BeginPlay();
	UE_LOG(MyLog, Warning, TEXT("Hello World"));
}

// Called every frame
void ATests::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}

ATests::ATests(const FObjectInitializer& ObjectInitializer):Super(ObjectInitializer)
{
	StaticMeshComponent = ObjectInitializer.CreateOptionalDefaultSubobject<UStaticMeshComponent>(this, TEXT("NameYourComponentHere"));
}



The errors I get are:


 

1>C:\Users\Miguel\Documents\Unreal Projects\MyProject2\Source\MyProject2\Tests.cpp(30): error C2511: 'ATests::ATests(const FObjectInitializer &)' : overloaded member function not found in 'ATests'
1>          c:\users\miguel\documents\unreal projects\myproject2\source\myproject2\Tests.h(12) : see declaration of 'ATests'
1>C:\Users\Miguel\Documents\Unreal Projects\MyProject2\Source\MyProject2\Tests.cpp(31): error C2550: 'ATests::{ctor}' : constructor initializer lists are only allowed on constructor definitions
1>C:\Users\Miguel\Documents\Unreal Projects\MyProject2\Source\MyProject2\Tests.cpp(32): error C2671: 'ATests::{ctor}' : static member functions do not have 'this' pointers



.

Any ideas?

I’m going to guess your problem stems from the fact that 1) the constructor that takes FObjectInitializer is not declared in your header file, and 2) you have two constructors defined.

You are correct, I am starting to slowly understand this. Thank you!