Hello, I have a pawn with an array of pointers to static meshes that are created and added to it during the game which looks like this:
TArray<UStaticMeshComponent*> Meshes;
In my movement component (which, by the way, is not inherited from some movement component of the engine) I want to get a pointer (or a reference) to this array to move the static meshes of the pawn.
I tried to just declare it like this:
TArray<UStaticMeshComponent*>* UpdatedComponents;
But it seems like this is invalid, the compiler text:
âInappropriate â*â on variable of type âTArrayâ, cannot have an exposed pointer to this type.â
However, I can declare a reference to an array like this:
TArray<UStaticMeshComponent*>& UpdatedComponents;
But I canât initialize it with CreateDefaultSubobject and I donât see any other suitable ways to create this component with initialization of this reference, so I guess I canât do it in the engine, is that right? Does this mean that I have to do something like this (with setter, of course) with each addition of a component to the array?
Is there a reason you canât store the meshes in the movement component itself? If the character still needs it sometimes, you can add an accessor to get the array. If the character needs it more often, make the character authoritative for the mesh array, and have the movement component ask the character for the mesh.
If itâs not really inherently something that âbelongsâ to either part of the character system, then it might actually make sense to make an additional componentâUStaticMeshCollectionComponent or something similarâand store the static mesh TArray<> there, along with functions to manipulate it properly, and an accessor that gives the reference the other pieces can use.
Basically, Iâd try to figure out who âownsâ the meshes. That way, you know who you can make the one âcanonicalâ source of authority for the list of meshes. Once thatâs done, anything else that uses that list just asks the authoritative source whenever it needs the list. (Instead of trying to save off their own copy which might or might not remain in sync.)
Is there a reason you canât store the meshes in the movement component itself?
The main reason is that I tried to make my movement component by analogy with UMovementComponent that have UpdatedComponent pointer, the second reason is that Iâm wondering if itâs possible to make such a âself-updatingâ copy of this array. Anyway, I think making the character authoritative for this array is what solves my problem, thank you.
Once thatâs done, anything else that uses that list just asks the authoritative source whenever it needs the list.
Did you mean that I should get an array from the pawn in the movement component every tick instead of setting it from a pawn for the component every time it changes or that I should use a delegate?
Yeah. What I meant about ownership was basically⌠the movement component needs the meshes every tick, from what you say. (Iâm guessing this is an assembled character and doing procedural movement, or something?)
Does the Character itself need the meshes every tick? If it does not, then you could argue that the meshes really âbelongâ to the movement component â it is the one that needs and uses them â and it might be worth having the movement component âownâ that array outright if itâs the only thing that uses it consistently. Then when the character needs to alter the array (to add/remove static meshes), it can relay that request to the movement component.
If both of them need it â or thereâs a good reason the character should conceptually âownâ the mesh array â then, conversely, it might be best make the character the authoritative copy and have the component ask for the array when needed.
As I donât know the full details of what youâre doing with this, youâre obviously better-equipped to know which should be authoritative for the array (or even if it would make sense to create a new component that existed to contain the âassembly instructionsâ and static mesh array)⌠I just meant basically that one thing ought to be authoritative, and the others query it.