Pointer to an array of the static meshes

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?

UpdatedComponents = Meshes;

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.)

1 Like

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.

And it sounds like you’ve got that sorted! :slight_smile:

1 Like