Dear @WTBFriends,
Hi there!
Welcome to the Unreal Engine Community!!!
My personal recommendation is to avoid trying to create the components in C++ constructor, as Blueprints makes setting all the initial values so much easier and more intuitive.
If you do indeed want to manipulate the components in C++ rather than BP, I would do something like
class .h
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="FrogTurret")
TArray<UStaticMeshComponent*> Parts;
This is an empty array, until you then, in the Blueprint of this class, Add to the Array in the Constructor Script.
The advantage of using the Construction Script is that when you make changes and compile, the changes become immediately evident in your viewport, without even running the game.
And with the randomization you want, you could just keep hitting the compile button
and watch all the generations occur, seeing your logic play out with every click rather than every compile → load editor → run game cycle which takes a lot longer
~
Then, you can add 4 Static Mesh Components to the Blueprint version of the C++ Class, and have full access in C++ to these easily-created components, by adding each of them to the C++ array.
With the array, you also have easy C++ identification of your 4 parts, because they are now indexed:
Parts[0] goes to the far left,
Parts[1] goes next to Parts[0]
etc.
So the C++ code is highly readible.
~
I personally would do a rough draft of your desired positions of your parts in the BP Constructor script.
Once you have the logic and the math working the way you want, then you could move the logic to C++ for performance, though if it is a one time positioning, there’s not really a performance benefit to using C++.
C++ would be the ideal way to arrange these parts if the logic involved was heavily math intensive, over time / the logic has to run many times.
Unless you really find vector math clunky to do in BP,
a BP implementation, with C++ option for access via the Array I mentioned above,
will result in you having the fastest and cleanest and least-crash-prone development cycle.
If you want to vary the number of parts, you can add the static mesh components dynamically, which is also much faster to do in BP than C++
~
Below you can see I’ve very quickly added dynamic static mesh components in constructor script that vary in count, stacking vertically from an offset (ZeroVector at the moment).
This is how simple your final code can look, if you then add all the components to an array like I mentioned, now you can access them in C++ and you’re done
~
All that said, of course I Love UE C++
, and this is the kind of setup I myself use to have:
- Easy Access to the components in C++ and in BP
- Easiest and fastest creation of the components and setting of default values
Just make sure to always
check your pointers
, and your array access itself!
if(Parts.Num() < 1)
{
//parts not added yet
return;
}
if(Parts[0])
{
//do things to the first part
}
So translating to your current C++ Code now,
just change the UE4 macros of “pieces” and move all your creation logic to BP Constructor,
and you will have an easy, maintainable solution,
That you can test very quickly ( compile button → click click click ),
and yes, if you put the positioning logic in BP Constructor Script,
each time you create one of these actors, you will get a new version of your puzzle, just like you want, with the least amount of C++ code to have to keep track of and keep stable from crashing.
Especially in multiplayer games, things don’t always become stable as new clients join/leave until around the time of the BP Constructor Script, so the less you do in the C++ constructor, the faster you will get to a stable release.
Rama