How can i assign static mesh to level like unreal editor with C++?

Hello!

I want to assign static mesh to level like unreal editor !

So, How do I write the source code with C++ ?

Please answer about my question.

Thank you :smiley:

------------------------------------- ADD ---------------------------------------------------------------------

Add ++ that class is UObject … So, I can’t (). always value is “NULL”.

1 Like

Hey 4aries17
You could check the SpawnActor Function from the UWorld Class.
My Attempt would be:

UWorld* const World = ();
if (World)
{
    auto Actor = World->SpawnActor<AActorClass>(AActorClass::StaticClass());
    UStaticMeshComponent* ProjectileMesh = NewObject<UStaticMeshComponent>
                                            (Actor,
                                            UStaticMeshComponent::StaticClass());
    ProjectileMesh->RegisterComponent();
    Actor->RootComponent = ProjectileMesh;
    UStaticMesh* Projectile =  //... Some way to load meshes in runtime....//;
    if(Projectile)
        ProjectileMesh->SetStaticMesh(Projectile);
}

I never tried this code, but thats the only way I could think of doing something like this :slight_smile:
If the code doesn’t compile I will look into this again.

Good Luck

Greetings

Thank you !
I’ve tried . But I’m a Unreal novice.
I can not access RootComponet.

Do you know perhaps for this reason?

When you drop asset on level it spawn actors and UE4 have ready actor to contain those asset in level, usally just containg component that handles that asset

Hey 4Aries
Sry I didn’t know that RootComponent is protected.
Instead of using Actor->RootComponent you use Actor->SetRootComponent(ProjectileMesh);

Greetings

Hey . Thats cool :slight_smile:
I didn’t know there were predefined Actors for this

Hello,
I wrote some code for you as example.

//Header

#pragma once

#include "Engine/StaticMeshActor.h"
#include "MyStaticMeshActor.generated.h"

UCLASS()
class *PUT_HERE_YOUR_PROJECT_API*_API AMyStaticMeshActor : public AStaticMeshActor
{
	GENERATED_BODY()
public:
	AMyStaticMeshActor();
};

// Source

#include "*PUT_HERE_YOUR_PROJECT_HEADER*.h"
#include "MyStaticMeshActor.h"

AMyStaticMeshActor::AMyStaticMeshActor()
{    
	UStaticMesh* StaticMeshTmp = LoadObject<UStaticMesh>(NULL, TEXT("/Engine/BasicShapes/Sphere.Sphere"));
	if(StaticMeshTmp != nullptr)
	{
		GetStaticMeshComponent()->Mobility = EComponentMobility::Movable;
		GetStaticMeshComponent()->SetStaticMesh(StaticMeshTmp);
	}
}

You can spawn this with AMyStaticMeshActor* MyNewACtor = ()->SpawnActor< AMyStaticMeshActor >();
Also it is possible to drag and drop from content browser.

I hope this was usefull for you.
.

1 Like

Note that maybe except BSP, everything needs to be an actor to be in the world. What you see in Overlay is definitly a actor

Thank you very much!
It’s very usefull to me!

But I don’t know where input a “AMyStaticMeshActor* MyNewACtor = ()->SpawnActor< AMyStaticMeshActor >();” this code.

And How can i Get a StaticMeshComponent ?
I don’t know that funtion logic “GetStaticMeshComponent()->”

Sorry to my code level …

4aries17.

Thank you,
But that class is UObject … So I can’t (). return value is “NULL”… allways …

So, Is there a way to spawn in UObject class? or Is there a way to () in UObject class ?

I am glad to hear my first answer was usefull. Don’t forget to mark the acceptable answer, this help to see solution without full story for people from google.

I believe at begining of learning the UE4 YourGameMode::BeginPlay is good place for spawning something, most of time spawning actors is totally up to you.