I know there’s 1 or 2 questions out there like this, but I need a definitive and concise answer.
All I need to be able to do is spawn a cube at a position that I define and spawn it when I tell it to be spawned. That’s it, very simple however I can’t seem to get it to work at all. I’ve tried several different options but with no luck.
Now, you’ll want to add a new header library (this is so we can have a reference to cube blueprint anywhere)
Create ‘BP_Libs.h’ (if you build it straight in VS, it’ll put it by default in intermediate/projectfiles/, you’ll need to move this to source/someproject/ to be able to reference it)
Thank you for such a concise response it has really helped. Although now I’m having the problem that I am unable to spawn anything because GetWorld() comes up with the error: 'a nonstatic member reference must be relative to a specific object. I’m pretty sure it’s because this is being used in a static function. Is there any way around this?
Hey, your right - i didnt actually compile this before posting. Ive corrected my answer to a working solution (with help of google) and ive actually tested it this time!
Funny thing about this is that even if you change the position of the FVector the starting position does not change. It continues to spawn in the same place. The only thing that does change is the number inside of the transform panel of the editor.
hey, found the answer. It seems like it’s necessary to move the mesh, not the actor.
for my code I already overrode BeginPlay() in CubeActor.h
UPROPERTY(Category = Meshes, VisibleAnywhere)
UStaticMeshComponent *CubeMesh;
public:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
MycubeActor.cpp
// Called when the game starts or when spawned
void AViewCubeActor::BeginPlay()
{
Super::BeginPlay();
//SetActorLocation(FVector(0.f, 0.f, 300.0f));
CubeMesh->SetMobility(EComponentMobility::Movable);
FTransform relTrans = CubeMesh->GetRelativeTransform();
FVector loc = CubeMesh->GetComponentLocation();
FVector position = FVector(loc.X, loc.Y, loc.Z + 300.0f);
relTrans.SetLocation(FVector(position));
CubeMesh->SetRelativeTransform(relTrans);
}
What I just did isn’t a good way to do it. Just set CubeActor’s RootComponent to CubeMesh in its constructor. That way everything stays together instead of having to update both the mesh and the actor separately.