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 
------------------------------------- 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 
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 
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.