How to retrieve newObject by name?

I have several objects that I’m creating using NewObject. I see I can provide a name for the object during construction. Is there a way I can fetch a pointer to this object using the name. What is the easiest way to specifically select an object spawned through NewObject?

In a nutshell, do you need a pointer to a created (during the runtime) instance of the object, so that you can call\access its properties later on? If so - searching by name might not be a solution you need.

NewObject returns value is:

A pointer to the spawned instance of
the specified class.

So, you can just save it and use it later in the code.

What is the easiest way to
specifically select an object spawned
through NewObject?

UYourActor* pointerToObject = NewObject<UYourActor>(Outer)

With that pointerToObject is a pointer to a created object.

You can use NewNamedObject() to achieve the same thing, but giving your newly created instance a custom name.

Based on the documentation:

That is the way I’m doing it. Currently I store the pointer in a TMap. I was just curious if there is already something like that assigned to the owning actor. When calling NewObject the object is a part of the owning actor right? SoI figured the owning object might have a TMap of some sort where I can just find inside it using the name.

Correct, but how would you do that inside of “CubeObject(Actor)”? this->find? I see a findcomponentbyClass. So it looks like this returns an array. how would I know which object in that array i’m reffering to? Is this array a TMap where I can then search it using the provided FName?

Lets say I create 20 newobject, and each one I give a unique name when I call newobject. It seems like this name provided would be the key to accessing that object later. Obviously the pointer that you create when you call newobject is the way to access it right then. This is why I have a Tmap with names, but this seems rather redundant.

Hmm…

I was just curious if there is already
something like that assigned to the
owning actor.

Not that I am aware of at the moment. Maybe give some background on what you want to accomplish, cause I might not clearly understand the issue.

Currently I store the pointer in a
TMap.

Assuming you store a data, where “string” is a name of the object, then you probably already using TMpa.contains(“ObjectName”) to retrieve the pointer by name. Other than that, maybe using Iterator to find Actor by Name in the Scene would do the trick (BUT - it can be a relatively heavy process).

SoI figured the owning object might
have a TMap of some sort where I can
just find inside it using the name.

That make me think you your are talking about Parent object and its Children objects that attached to it. And maybe you want to find a Child object that is attached to a Parent object? e.g:

  • CubeObject(Actor)
    • Cube2(Actor)
    • Cube3(Actor)

CubeObject.Find(“Cube2”)

Something like that?

how would I know which object in that
array i’m reffering to?

Loop through the children components and compare its names. If match - object found.

The following is pseudo code for a function to get a reference to the Child object:

   UObject* FindInParent(FString nameToFind){
        for each OBJ in YourParentObject->GetChildrenComponents(){
             if ToString(OBJ) == nameToFind{
                 return *OBJ
             }
        }
        return nullptr;
    }

Is this array a TMap where I can then
search it using the provided FName?

Yes, but it is Array, so you have to loop through it.

This is why I have a Tmap with names,
but this seems rather redundant.

This is not a bad approach, if that make sense to you. Personally, I like using TMap data structure in cases where it requires to access it frequently during the runtime.