c++ string array to HUD?

Hi all, I am trying to iterate through a list of static mesh actors in c++, then print them to a scrollable textbox in the HUD using UMG.

I have this:

for (TObjectIterator<AStaticMeshActor> Itr; Itr; ++Itr)
{
TArray<FString> MeshActors;
MeshActors.Add(Itr->GetName());
}

Which i THINK is storing them in an array… But how can i access the items in the array from blueprints?

Or am I going about this all wrong?

thanks!

Your totally doing this wrong.

First, you don’t want to declare a new array every iteration of your for loop.
Second, you don’t want to declare the array within your for loops scope (the curly brackets) because when the loop is done, the data goes “out of scope” and is removed from memory.
Third, you want to have a method which returns the array of strings. You can either return it as a function return value or as a function parameter by reference.

Once you’ve got a function which generates the list of names for your static mesh actor, then you can call it within the UMG blueprint to get the values and insert those strings into a scrollable text box. Don’t forget to set your UFUNCTION parameter to “BlueprintCallable”.

You’ll also want to double check to make sure that your “AStaticMeshActor” actually iterates over a list of mesh actors.

Thanks, That makes sense! Can I return an array from a void function? Or do i need to make it a struct or something?

so i have… cpp

void AListMesh::StringStaticMesh()
{
TArray<FString> MeshActors;

for (TObjectIterator&lt;AStaticMeshActor&gt; Itr; Itr; ++Itr)
{						
	MeshActors.Add(Itr-&gt;GetName());
	
}

//return MeshActors;

}

…the return doesn’t work, understandably…

.h

UFUNCTION(BlueprintCallable, Category = "DATA")
	static void StringStaticMesh();

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "DATA")
	TArray&lt;FString&gt; MeshActors;

No, “void” means, that you have an “empty” return. In other words, you have NO return. If you want to return a TArray<FString>, then you need to put “TArray<FString>” instead of void into the definition and declaration.

Also if you declare the array in your .h file, you don’t need to create it in the function again.

If you make it EditAnywhere and BlueprintReadWrite, then you won’t need a return value. Your function will change the array with the loop and you can access this array in blueprints because of the UPROPERTY makro and its options.

Got it. thanks for your help!

So in the blueprint, i have the return value as an array. Do i need to use the ForEachLoopWithBreak to split the array values out? or is there a node i don’t know about?

Got it. I’m an idiot. the forEachloop was not linked in the exec flow.