I’m having trouble when spawning an actor at the correct location after it’s base actor has been rotated. Here’s some images to show whats going wrong:
The far left cube is the base. The others have been spawned on it’s X axis and attached. When rotating the first cube the others rotate with it correctly.
BUT when adding another cube everything goes wrong:
Here’s my code:
/*
*/
FVector AMyBuilderActor::FindLocationX()
{
if(StaticMeshArrayX.Num() > 0)
{
// Find the last index of the array
for(int32 i = 0; i < StaticMeshArrayX.Num(); ++i)
{
int32 LastIndex = StaticMeshArrayX.FindLast(StaticMeshArrayX*);
return StaticMeshArrayX[LastIndex]->GetActorLocation();
}
}
return GetActorLocation();
}
/*
* The int X (default value) is increased and CubeConstruction() is executed when a new cube is to be spawned
*/
void AMyBuilderActor::CubeConstruction()
{
FActorSpawnParameters SpawnInfo;
SpawnInfo.Owner = this;
FVector NewLoc = GetActorLocation();
// Reset and clear existing cubes before rebuilding
ClearCubes();
int32 Xx = X - 1;
NewLoc = FindLocationX();
for(int32 i = 0; i < Xx; ++i)
{
NewLoc.X += 200.f;
NewCubeMesh = GetWorld()->SpawnActor<AMyBuilderActor>(this->GetActorClass(), NewLoc, GetActorRotation(), SpawnInfo);
ApplySettings(NewCubeMesh); // Apply all settings such as attach to this parent, set owner etc
StaticMeshArrayX.Add(NewCubeMesh);
}
...
....
}
So it sounds like you are the one detaching everything, and re-adding it, when a new cube is added?
Sounds like you just need to plan your function better!
I’d recommend identifying the root actor of the other cubes when doing the delete/reconstruction.
The parent of all attached actors is easy to find in Actor.h
Then store the rotation of that actor
then delete everything
then recreate all the cubes, with their rotation at 0, just going in straight line
then attach them all to their parent
then rotate the parent actor to the value you stored
sounds like your question is one of organization and systematizing your processes, and is not related to any issues with the engine itself.
I for one know that one I attach stuff it doesn’t magically detach and change the rotations of other actors attached to the same base.
So plan your reconstruction process that you are doing and you should be good to go!
I can highly recommend re-writing your reconstruction function now that you have new information about what is not working right and have a clearer perspective on your goal
Your own previous code can easily hold you back!
Clear your mind, clear your code (so it doesn’t confuse your mind)!