Problems with C ++ and actors with static meshes

I started to create a simple mobile project of a pool game in C ++ to be able to practice my skills with C ++. I know that blueprints are 10x slower than C ++ and so I decided to create in C ++ beyond learning. I did a pool cue in Maya I exported, texturize in substance painer 2.6 and minded in unreal and created a collision.

Ok I created an actor for the pool cue then I decided to add the static meshe by code seeing some tutorials here of the same forúm, but none worked, some even had to use the TSubobjectPtr but the visual studio 2017 said that it did not work anymore.

So what’s the best way to put the static meshe right through c ++ so that it already appears with it as soon as I play in the world editor and can add behavior like force etc.

I am using unreal 4.16

Hi,

The preferred method to add components like StaticMeshComponent to Actors is to create the Component in code, and then set the actual StaticMesh in a Blueprint derived class.

MyActor.h (any access specifier works except ‘private’)



//Somewhere in MyActor.h
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
UStaticMeshComponent* MyStaticMesh;


MyActor.cpp



//Constructor
AMyActor::AMyActor() :Super()
{
    MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));

    //Use this line of code if you want the StaticMesh to be the RootComponent
    RootComponent = MyStaticMesh;

     //Use this line of code if you want to attach the StaticMesh to an existing RootComponent
    MyStaticMesh->SetupAttachment(RootComponent);
}


Then you create a BP class based on the native MyActor, and your StaticMeshComponent will be visible, as it would have been added from Blueprints. Assign a mesh there, and it should work!

Thanks man, I’ll be testing this right now.

Taking advantage of the occasion, Blueprints derived from C ++ code have the same performance as a pure C ++ or will it be slow as blueprints?

So I did exactly as you said, visual studio accuses 1 pointer member access error of static meshe and says that a static meshe can not be equal to a scene component (root = static meshe line) but when I click Compile in unreal editor, compile normally.

But when I play the actor in the level the engine crash, follows the print:

Here is the engine log:

My Actor’s Code:

Cue_A.h

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

#pragma once

include “CoreMinimal.h”
include “GameFramework/Actor.h”
include “Cue_A.generated.h”

UCLASS()
class THESNOOKER_MOBI_API ACue_A : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor’s properties
ACue_A();

protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;

public:
// Called every frame
virtual void Tick(float DeltaTime) override;

//Cue Meshe Asset
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Cues")UStaticMeshComponent * mCue_A;

};

Cue_A.cpp

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

include “Cue_A.h”

// Sets default values
ACue_A::ACue_A()
{
// 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;

//Cue Asset
mCue_A = CreateDefaultSubobject&lt;UStaticMeshComponent&gt;(TEXT("SM_CUE_A"));

//Root component
RootComponent = mCue_A;

//Anex Root Component
mCue_A-&gt;SetupAttachment(RootComponent);

}

// Called when the game starts or when spawned
void ACue_A::BeginPlay()
{
Super::BeginPlay();

}

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

}

Hi,

The crash is probably due to the fact that you have used both lines of code that I have commented, warning you to only use one of them :smiley: I’m sorry if it was misleading!

If you want your mesh to be the root component of the Actor, use the "RootComponent=mCue_A; line, if you have an existing root component (apparently not) use the "mCue_A->SetupAttachment(RootComponent) line to attach the mesh to it! The way you have it now, it tries to attach itself to itself.

I hope it makes sense now! :slight_smile:

EDIT: Also, if you go to Advanced mode when writing your comment, you can insert code in a more convenient way!

Regarding your previous question, I definitely wouldn’t call Blueprints “slow”. Obviously, they are slower than native code, but them being 10x slower was a “rule of thumb” mentioned in a thread by one of the devs many engine versions ago.

If you write all the functionalies of a class in C++, then create a Blueprint version of it to add some additional stuff (or assign assets), I wouldn’t even consider the “performance” cost of it, however I am not aware of how much exactly it is.

If you really worry about it, you can have a look into nativizing your Blueprints:
https://docs.unrealengine.com/latest/INT/Engine/Blueprints/TechnicalGuide/NativizingBlueprints/

Thanks man now it worked.