Noob here with a (hopefully) quick question. My goal is to create a “prop” class that is basically a mesh that can take damage. Preferably with the ability to pass a string with the relative path to the mesh to be spawned as an argument.
Now, I am having an issue with the mesh part. I created the prop class using the in-editor tool using AActor as the parent. I have tried using AActor::AddComponent and UWorld::SpawnActor and I can’t get them to create a mesh. I knew there was going to be an issue when I couldn’t even find a way to pass the string of the mesh to be created. Any suggestions?
Edit: Here is my code
Prop.h
// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "GameFramework/Actor.h"
#include "Prop.generated.h"
/**
*
*/
UCLASS()
class AProp : public AActor
{
GENERATED_UCLASS_BODY()
UPROPERTY()
int32 m_Health;
/** Static Mesh Comp, Set In BP Default Properties */
UPROPERTY(VisibleAnyWhere, BlueprintReadOnly, Category = "StaticMesh Components")
TSubobjectPtr<UStaticMeshComponent> m_Mesh;
virtual void BeginPlay() OVERRIDE;
};
Hey,
kindof an old thread, but for those stumbling with this problem, heres the solution working for me as of UE4 v4.8
First of all there are 2 moments at which you may want to create a mesh; at construction time and at runtime. Theres a different procedure for each.
For these examples the mesh I want to load is called “BlameGT_Mesh” and in the UE4 editor is found at “Content/MyCharacter/Meshes/weapons/”
The Actor is Weapon and the mesh I instantiated during its construction is called baseMesh.
So ill be replacing Weapons’ mesh called baseMesh with the newly loaded BlameGT_Mesh.
At construction time: (when you’re constructing the actor with “xclass::xclass(const class FObjectInitializer& FOI)…”
you may use the method describe previously by oOo.DanBO.oOo
FString pathName= "StaticMesh'/Game/MyCharacter/Meshes/weapons/BlameGT_Mesh'";
//do take notice of the ' symbols. they seem important.
ConstructorHelpers::FObjectFinder<UStaticMesh> mySSMesh12412(*pathName);
//if you're inputing a FString, the * is necessary.
Weapon->baseMesh->SetStaticMesh(mySSMesh12412.Object);
At runtime: (as of my understanding, every rest of the time when not in construction time)
FString pathName= "StaticMesh'/Game/MyCharacter/Meshes/weapons/BlameGT_Mesh.BlameGT_Mesh'";
//do take notice of the ' symbols. they seem important.
//also of the . and that we repeat the name.
UStaticMesh* mySSMesh12412= Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(), NULL, *pathName));
//if you're inputing a FString, the * is necessary.
Weapon->baseMesh->SetStaticMesh(mySSMesh12412);
I think thats it. This is my first post on these forums, so if I forgot something, please let me know.