UInstancedStaticMeshComponent::GetInstanceTransform pointer problems

Hello world.

I’m trying to compare two different UInstanceStaticMeshComponents to see if they have any overlapping instances, then remove those instances from both.

The code

Gives me a C3867 error: 'UInstancedStaticMeshComponent::GetInstanceTransform': function call missing argument list; use '&UInstancedStaticMeshComponent::GetInstanceTransform' to create a pointer to member

I tried then to use

			if (&ParentRect->WExtStraightMesh->GetInstanceTransform[b] == &WExtStraightMesh->GetInstanceTransform[a])
			{
				&WExtStraightMesh->RemoveInstance[a];
				&WExtStraightMesh->RemoveInstance[a];
			}

to no avail. How would I use pointers in this scenario?

____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

EDIT: For those who may come across this same problem, this is what the code, corrected, ended up looking like:

However, now new error arises - not at compiling, but at playing. The second for loop, wherein I try to get

ParentRect->WExtStraightMesh->GetInstanceCount()

crashes the game. No coherent error message is given by the error reporter. Thoughts?

Your code has multiple errors in it, both semantic and syntactic. In addition to errors that will prevent the code from compiling, it could also loop infinitely, and also not have the effect you want or do nothing at all.

The reason your code is also unlikely to work at all, even if you fixed all of the basic errors, is because you are comparing transforms, which consist of multiple floating point components. Unless you wrote the memory directly and then never touched it again with a fp operation, the comparisons are very much unlikely to ever be true. You would need to determine some epsilon/tolerance and then use that as a threshold for determining equivalency. There’s no way to know what that would be for your use case in isolation, so you would need to explain what you’re doing.

Here are your various problems anyway, in case you’re interested:

&WExtStraightMesh->RemoveInstance[a];
&WExtStraightMesh->RemoveInstance[a];
  • The & is doing nothing.

  • You’re using [] instead of () for a function call.

  • You’re removing from the same index twice with an operation that will move data around, so the second removal will either remove something undesired, or will do nothing, because the index will be invalid the second time and the code for RemoveInstance(…) will return false.

    for (int a = 0; a < WExtStraightMesh->GetNumChildrenComponents(); ++a)

  • GetNumChildrenComponents() is completely unrelated to the number of mesh instances for the instanced mesh renderer.

    for (int b = 0; a < ParentRect->WExtStraightMeshes->GetNumChildrenComponents(); ++b)

  • Unless the two ‘WExtStraightMeshes’ fields point to the same component (you should never do this with UActorComponents), the loop condition never changes. This will loop infinitely every time.

    if (ParentRect->WExtStraightMesh->GetInstanceTransform[b] == WExtStraightMesh->GetInstanceTransform[a])

  • This will probably never evaluate to true, except under very specific circumstances (mesh instances were placed using code and then never serialized/deserialized or sent over the network, and never changed with any math operations).

There is missing araguments for “GetInstanceTransform” function

UFUNCTION(BlueprintCallable, Category="Components|InstancedStaticMesh")
bool GetInstanceTransform
(
    int32 InstanceIndex,
    FTransform & OutInstanceTransform,
    bool bWorldSpace
)

Try This

FTransform TransformA;
FTransform TransformB;

ParentRect->WExtStraightMesh->GetInstanceTransform(a, TransformA);
WExtStraightMesh->GetInstanceTransform(b, TransformB);

if(TransformA == TransformB)
{
//.....
}

Your method has fixed one bug…and I now face another.

		for (int b = 0; b < ParentRect->WExtStraightMesh->GetInstanceCount() - 1; ++b)

This line crashes the program, particularly the ParentRect-> part; if I just do WExtStraightMesh without it, there are no problems.

You bring up various points, which I had realized later and forgot to edit back into the post :stuck_out_tongue:

The code block is now as such:

	for (int a = 0; a < WExtStraightMesh->GetInstanceCount() - 1; ++a)
	{
		for (int b = 0; b < ParentRect->WExtStraightMesh->GetInstanceCount() - 1; ++b)
		{
			/*FTransform A; WExtStraightMesh->GetInstanceTransform(a, A); FVector AV = A.GetTranslation();
			FTransform B; ParentRect->WExtStraightMesh->GetInstanceTransform(b, B); FVector BV = B.GetTranslation();
			if (AV == BV)
			{
				WExtStraightMesh->RemoveInstance(a);
				ParentRect->WExtStraightMesh->RemoveInstance(b);
			}*/
			UE_LOG(LogTemp, Warning, TEXT("TEST2"));
		}
		UE_LOG(LogTemp, Warning, TEXT("TEST1"));
	}

and now there is a crash on play, with the second for line being the culprit (commenting it out fixes the problem). Thoughts?

,You are trying to modify collections whilst iterating over them and most likely walking off the end of one of them when iterating. And you are also skipping the next member when removing a member. You should only increment (++a, ++b) when not removing a member. That’s what I’m reading.