Download

c++ to blueprints, what target?

Hey all, I have an Actor iterator function in c++, like this one:

A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine ForumsObject%26_Actor_Iterators,_Optional_Class_Scope_For_Faster_Search

Where I add all actors of a class to an array, then call the array from inside a UMG HUD blueprint graph. It won’t run as the c++ node is ‘target, self’.

What SHOULD I have as the target for it? A reference? Or do i need to make it static and remove the target pin?

thanks!

Can you show me your exact code, the class where you created this function and the Blueprint node?

Because right now i would say you just need to call this inside the class you created the function or call it from a reference of this class.

Hi, thanks for your reply… Code and BP as follows:

//.h

UFUNCTION(BlueprintCallable, BlueprintPure, Category = “VPDATA”)
TArray<FString> StringStaticMesh();

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

//.cpp

TArray<FString> AListMesh::StringStaticMesh()
{

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

return MeshActors;

}

//bp (Inside the UMG graph)

cf6677a537d34d0021217680d3ee3d261507e77a.jpeg

Try removing “BlueprintCallable” from the UFUNCTION() Makro.

Your “BlueprintPure” is already a function that can be called without an object of that class created.

I would also say you better create an array INSIDE of the function, fill it, and return it. Because without an object
i’m not sure if the array exists.

If you use BlueprintCallable instead of BlueprintPure, you will need an object of that class. That means you will have
to spawn the “AListMesh” and then use a reference of that spawned actor.

You can also step back from using an actor and create a UObject. Then you could create the object and call the function.
You would only need a BlueprintPure function that creates an UObject of your class and returns it (there is a tutorial
from Rama i guess and Ramas Plugin also contains such a node).

But for testing if it would work, change the UFUNCTION to:


UFUNCTION(BlueprintPure, Category = "VPDATA")
TArray<FString> StringStaticMesh();

and inside of the function in your cpp, do this:




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

     }

return MeshActors;
}


Also delete the UPROPERTY MeshActors, so that you don’t have it doubled.

thanks! So with your code, if I make a blueprint from the listmesh c++ class, I can access the array from the level blueprint.

BUT, not from inside my HUD graph. Do i need to create a ‘global’ variable to access it from anywhere? Or would a UObject be better for this?

Aha, adding static to the .h declaration of each function makes the target pin vanish. This seems to work…

UFUNCTION(BlueprintPure, Category = "VPDATA")
	static TArray&lt;FString&gt; StringStaticMesh();

thanks again for your help.

… BUT, now I need to cast the entries in the array to staticmeshActors. It doesn’t seem to let me, as they are strings…

If i switch the array to this:

it will not compile, giving ‘expected a pointer’ and no operator matches operand’ errors.

//.h

UFUNCTION(BlueprintCallable, Category = "VPDATA")
	static TArray&lt;AStaticMeshActor&gt; ActorsStaticMesh();

//.cpp

TArray<AStaticMeshActor> UListDataObject::ActorsStaticMesh()
{
TArray<AStaticMeshActor> MeshActors;

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

}

return MeshActors;

}

UFUNCTION(BlueprintCallable, Category = “VPDATA”)
static TArray<AStaticMeshActor*> ActorsStaticMesh();

Do this. You need to use a pointer here. Also why don’t you just save the actors in an array. Use your function and save the returned array in your GameInstance
or GameState. A class that you can access from everywhere.

Remember to change this for every TArray.

thanks again.

Good call re saving as another array. First though… This now dies on the ADD line. If i comment it out, it runs. do i have my syntax wrong? I have tried with Add(); and it errors.

TArray<AStaticMeshActor*> UListDataObject::ActorsStaticMesh()
{
TArray<AStaticMeshActor*> MeshActors;

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

}

return MeshActors;

}

How about

MeshActors.Add(*Itr);

(:

i tried that first. :slight_smile:

It gives me:

Error 1 error C2664: ‘int32 TArray<AStaticMeshActor *,FDefaultAllocator>::Add(AStaticMeshActor *const &)’ : cannot convert argument 1 from ‘TObjectIterator<AStaticMeshActor *>’ to ‘AStaticMeshActor *&&’

4	IntelliSense: no instance of overloaded function "TArray&lt;InElementType, InAllocator&gt;::Add [with InElementType=AStaticMeshActor *, InAllocator=FDefaultAllocator]" matches the argument list
        argument types are: (TObjectIterator&lt;AStaticMeshActor&gt;)
        object type is: TArray&lt;AStaticMeshActor *, FDefaultAllocator&gt;

Have you tried “MeshActors.Add(*Itr);”?

Itr is an iterator, not the actual pointer, so you should need to dereference it before you get the actual pointer.

Success! Many thanks!