Download

Help with finding the correct location

Hi all,

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.

1.jpg

BUT when adding another cube everything goes wrong:

e81f9ad9afd4899a4a5bd19e128166b2f0cae390.jpeg

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);
	}	

...
....
}



Any help on this would be much appreciated.

Thanks,
Jon

“BUT when adding another cube everything goes wrong:”

can you explain this some more, what exactly is the order of events?

is it like

add bunch of cubes and attach each to base

then rotate base

all is well

then add another cube to base and attach

and all goes bonkers?

Rama

That is basically it yes.

I add cubes and attach them to the base cube, rotate the base cube, add another cube and their location resets.

FYI:
Everytime the CubeConstruction() function is called I remove all the attached actors first then spawn a new amount based on X.

Thanks

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!

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

  1. Then store the rotation of that actor

  2. then delete everything

  3. then recreate all the cubes, with their rotation at 0, just going in straight line

  4. then attach them all to their parent

  5. 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)!

Fresh start, new perspective, better code!

Rama