Hi,
I had the same problem, and I found two possible solutions.
-
If your actor is created in the Unreal Editor, then you can simply spawn it by code like this:
UClass* MyItemBlueprintClass = StaticLoadClass(UObject::StaticClass(), NULL, TEXT("/Game/Weapons/axes/DoubleAxeActor.DoubleAxeActor_C"), NULL, LOAD_None, NULL);
FActorSpawnParameters SpawnInfo;
SpawnInfo.Owner = this;
SpawnInfo.Instigator = Instigator;
ApsItemActor* obj = spawnManager->currentWorld->SpawnActor(MyItemBlueprintClass, newlocation, GetActorRotation(), SpawnInfo);
The pre-requisite is that your actor is replicated. Please note that actors deriving from AActor are not replicated by default, so you need to add in their constructor:
bReplicates = true;
or within the editor you should flag your actor as “Replicates”. It is one of the properties in the details panel.
-
If you want to create your actor completely within C++, and you have only the StaticMesh and the Material in the Editor (this was my case), you can create it like this:
FActorSpawnParameters SpawnInfo;
SpawnInfo.Owner = this;
SpawnInfo.Instigator = Instigator;
ApsItemActor* obj = GetWorld()->SpawnActor(ApsItemActor::StaticClass(), newlocation, GetActorRotation(), SpawnInfo);UStaticMeshComponent* MyMeshComponent = NewObject(obj, UStaticMeshComponent::StaticClass(), TEXT(“Mesh”));
UStaticMesh* MeshAsset = Cast(StaticLoadObject(UStaticMesh::StaticClass(), NULL, TEXT(“StaticMesh’/Game/Weapons/axes/doubleaxe02abc.doubleaxe02abc’”)));
UMaterial* MaterialAsset = Cast(StaticLoadObject(UMaterial::StaticClass(), NULL, TEXT(“Material’/Game/Weapons/axes/doubleaxe02c_Mat.doubleaxe02c_Mat’”)));MyMeshComponent->SetStaticMesh(MeshAsset);
MyMeshComponent->SetMaterial(0, MaterialAsset);
MyMeshComponent->SetWorldLocation(newlocation);
MyMeshComponent->SetIsReplicated(true);MyMeshComponent->RegisterComponent();
obj->AddOwnedComponent(MyMeshComponent);
obj->SetRootComponent(MyMeshComponent);
Also in this case the pre-requisite is that your actor is replicated. Same case for case 1.
If your actor is invisible client side, means you didn’t replicate it, or you didn’t set the position properly. Please note the SetWorldLocation call despite the fact you are already giving the location in the spawnactor command. For me it works only if I call explicitely SetWorldLocation