Hi !
I’m a little bit confused about components, arrays, and their “translation” into C++ code…
I’m trying to add an array of components (let’s say instanced static mesh) in a blueprint in order to mimic the procedural foliage spawner but I actually couldn’t do it so far, probably because I miss some key ideas behind this component thing.
- I can add instanced static mesh component into my blueprint (under “components”) but I can’t make it an array.
- I can add instanced static mesh component as a blueprint variables (under “variables”) and then set type to be an array, but then I’m unable to set the array elements (either within the editor - making it public - nor within the blueprint).
I’m quite new to blueprint and my background is c++ programming so what would really help me is to know how this is translated to a C++ class.
When declaring an array of objects in C++, there are two possibilities:
- array of objects
- array of pointers (or smart pointers) to objects
I would assume blueprint converts arrays using the second option as when adding a new element it it set to “None” by default (and one doesn’t want to duplicate objects each time it is added to an array).
But the thing is I’m unable to call component constructor in blueprint… “Resize” node doesn’t seem to call any constructor, and “Construct” node gives an error if you try to use any component class…
Thanks for insights and help on this !
UObjects (all blue color objects in blueprints) are never converted to objects, they can only be created dynamicly and there instances and life cycle are fully managed by the engine and you only operate on there pointers, when you create object or a actor you only get pointer to it.
With components there extra limitation dictated by role, they need to be added to actor in order ot function properly, in C++ some component can be created from NewObject and they function, like AudioComponent without attaching it to any actor (in fact some play sound engine functions do so) but most of them require attachment to actor, it also reason why most components has dummy actor class equivalent if you want to use specific component on the level like UCameraComponent have ACameraActor or ULightComponent has ALight.
So in blueprint you should use Add Component node, in C++ you also need to use that function to attach components to actors:
https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/CreateComponentFromTemplate/1/index.html
Thanks ! That was a great help
I even tried adding my own C++ actor component which contains an array of InstancedMeshComponent and a bunch of access function but it keeps crashing the editor… Even if the code compile when I call component default constructor to initialize my array, it wasn’t sufficient. A component must be added to something else, by calling an Add…() function. That was the key concept I was missing :/.
So now, I have a forloop that add as many InstanceStaticMeshComponent as I need, copy them to an array and then in another loop get a random one from the array and add instances of the mesh to the scene.