Announcement

Collapse
No announcement yet.

Adding SceneComponents in constructor, something going wrong

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

    Adding SceneComponents in constructor, something going wrong

    Hi all, I want to add a number of SceneComponents to my actor class. The components are just used for organization purposes. I use this code in the constructor of my actor class:
    Code:
    RootComponent = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("Root"));
    
    ActiveGroup = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("ActiveGroup"));
    InactiveGroup = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("InactiveGroup"));
    LootLocations = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("LootLocations"));
    
    ActiveGroup->AttachParent = RootComponent;
    InactiveGroup->AttachParent = RootComponent;
    LootLocations->AttachParent = RootComponent;
    I'm experiencing some issues with this, though. When making a blueprint based on this class, adding static meshes and dragging it into the world, all is fine. However, after saving and reloading the map I always get these messages:
    Code:
    Error Failed import for SceneComponent /Game/Blueprints/Gathering/BP_RN_Chest.Default__BP_RN_Chest_C:ActiveGroup
    Error Failed import for SceneComponent /Game/Blueprints/Gathering/BP_RN_Chest.Default__BP_RN_Chest_C:InactiveGroup
    Error Failed import for SceneComponent /Game/Blueprints/Gathering/BP_RN_Chest.Default__BP_RN_Chest_C:LootLocations
    Which leads me to believe I'm not using the correct way for adding scene components in code. Can anyone help?
    Journeyman's Minimap - Available on Marketplace - Forum topic - Video

    #2
    Dear Nisshoku,

    Can you show us the .h for your .cpp shown above?

    Can you temporarily try something other than SceneComponent just to see if it is related to scenecomponent somehow?

    ~~~

    Must Attach to Root Component

    You are not setting the attach parent for these new components it looks like!

    Code:
    ActiveGroup->AttachParent = RootComponent;
    InactiveGroup->AttachParent = RootComponent;
    LootLocations->AttachParent = RootComponent;


    Rama
    UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

    ♥ Rama

    Comment


      #3
      Thanks for replying! Here is the declarations in the .h file:

      Code:
      public:
      	USceneComponent *	ActiveGroup;
      	USceneComponent *	InactiveGroup;
      	USceneComponent *	LootLocations;
      I am setting the component's AttachParents to the RootCompoent right?

      I have tested with other components, like BoxComponent but the same result.
      Journeyman's Minimap - Available on Marketplace - Forum topic - Video

      Comment


        #4
        You need to add the UPROPERTY keyword to subobjects such as components, or the engine's management internals will ignore it and blueprints will not be saved with said subobjects. This is what's happening here -- the initial construction goes fine, but when you save your level, the components are lost, leading to these errors when the level is subsequently loaded.

        Code:
        	UPROPERTY()
        	USceneComponent *	ActiveGroup;
        	UPROPERTY()
        	USceneComponent *	InactiveGroup;
        	UPROPERTY()
        	USceneComponent *	LootLocations;
        That's just the very basics. There is a lot you can do with managed properties in the editor, like making them editable so these components don't always have the same locations. This is done through the VisibleInstanceOnly/VisibleAnywhere/EditInstanceOnly/EditAnywhere property keywords. I won't go into too much detail as this is just touching the tip of the iceberg and there are lots of examples and tutorials out there to delve in.

        -Camille

        Comment


          #5
          Also:

          SceneComponentName->AttachTo(RootComponent), might be a bit clearer.

          Comment


            #6
            cmartel, thanks, that makes perfect sense. I didn't make them UPROPERTIES because I figured I didn't need them as variables for event graphs and such, but it makes sense that that also affects saving. Adding UPROPERTY() and recompiling the affected blueprints solved the issue!

            Karner, good point. I read somewhere that ->AttachParent = is the way to do it in constructor (ref), but I think that is outdated and now that everything is working fine again I'll switch back to AttachTo.

            Thanks all!
            Journeyman's Minimap - Available on Marketplace - Forum topic - Video

            Comment

            Working...
            X