Announcement

Collapse
No announcement yet.

Strange Behavior with UStaticMeshComponents

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Strange Behavior with UStaticMeshComponents

    So I'm running Engine v4.0.0 still, but I don't think this is a bug, I'm probably just trying to do it wrong. I'm trying to create an actor that creates 6 or however many instances of a UStaticMesh, and then line them up in a row. The way I went about it was with
    Code:
    for (int i = 0; i < 10; i++)
    	{
    		TempMesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("ModelComp1"));
    		if (TempMesh != NULL && TempMesh->IsValidLowLevel())
    		{
    			TempMesh->BodyInstance.SetCollisionProfileName("PhysicsActor");			// Collision profiles are defined in DefaultEngine.ini
    			TempMesh->StaticMesh = Mesh.Object;
    			TempMesh->SetRelativeScale3D(FVector(0.25f, 0.25f, 0.25f));
    			//TempMesh->SetSimulatePhysics(true);
    			TempMesh->SetRelativeLocation(FVector(250*i, 0, 0));
    			MeshArray.Add(TempMesh);
    		}
    	}
    but this just crashes the game as soon as I try to put the actor into the scene. I'm pretty sure I'm just doing some C++'ing wrong. The error that's returned is like "SubobjectInits[Index].Sub Object != Subobject". However if I remove the for loop, and just leave the code that's inside it to run once, it creates a second model at (250,0,0) in the world, that stays static there, not moving with the root component. Any ideas or things you'd need more detail on?
    My actor's .h file: http://pastebin.com/EzPUMaCh
    My actor's .cpp file: http://pastebin.com/SHeUYvVf

    #2
    Yes, you are indeed C++'ing wrong. First of all get rid of TempMesh, then declare your mesh components array like so:

    Code:
    UPROPERTY()
    TSubobjectPtr<UStaticMeshComponent> MeshArray[10];
    Then change your loop to:

    Code:
    for (int i = 0; i < 10; i++)
    {
        MeshArray[i] = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, *FString::Printf(TEXT("ModelComp%d"), i));
    
        if (MeshArray[i] != NULL && MeshArray[i]->IsValidLowLevel())
        {
            ...
    
            MeshArray[i]->AttachParent = MeshComp;
        }
    }
    That should get rid of that crash of yours, and fix the "not moving with the root component" issue.
    Last edited by enlight_2014; 05-13-2014, 03:39 PM. Reason: Fix call to CreateDefaultSubobject in the loop
    Contact: enlight in #unrealengine IRC channel on Freenode, or @macagonator on Twitter

    Comment


      #3
      Thanks for the help dude. I tried changing the UStaticMeshComponent to a UCustomMeshComponent as per your code, but unfortunately your code doesn't compile with either of them. I'm not sure how to fix this error:
      Attached Files
      Last edited by Kybe; 05-10-2014, 07:14 PM.

      Comment


        #4
        Originally posted by Kybe View Post
        Thanks for the help dude. I tried changing the UStaticMeshComponent to a UCustomMeshComponent as per your code, but unfortunately your code doesn't compile with either of them.
        Err, that was a typo, obviously I should've written UStaticMeshComponent not UCustomMeshComponent, fixed it above. You've only shown me the Intellisense error, what error does the compiler give you when you try to use the array of UStaticMeshComponent?
        Contact: enlight in #unrealengine IRC channel on Freenode, or @macagonator on Twitter

        Comment


          #5
          I got it to compile now. But it instantly crashes with this error.

          Sorry that its in image form, I couldn't attach the Visual Studio debugger to the process.
          Last edited by Kybe; 05-12-2014, 08:32 PM.

          Comment


            #6
            Can you pastebin the current version of your source please?
            Contact: enlight in #unrealengine IRC channel on Freenode, or @macagonator on Twitter

            Comment


              #7
              KillerCube.h - http://pastebin.com/H8AmhECR
              KillerCube.cpp - http://pastebin.com/9hHqZeHN

              Yep. I appreciate your patience btw

              Comment


                #8
                You need to pass in unique names for your components to CreateDefaultSubobject() (they need to be unique within the context of the Outer object which is the first argument to that method), otherwise what happens is an existing component with the same name gets returned instead of a new one being created. Note that the code I've posted previously generates a unique name for each array element to avoid this problem.
                Contact: enlight in #unrealengine IRC channel on Freenode, or @macagonator on Twitter

                Comment

                Working...
                X