How to create an object of a structure and use a FString as the name of the object?

Sorry if my question sounds dumb. I’m trying to create a new object from a structure, and I want to use a FString value as the name.

Example:

struct Object
{
//stuff here

void functionName(FString iString)
{

//Creating a new object with the name from my string
Object (FString(TEXT(iString)));

}


};

Unfortunately this doesn’t work. Does anyone know of a way to make this work?

Hmmm… Unfortunately I am not quite sure what you’re trying to achieve. If you want to have a sort of name in a struct you’d do it like this:

struct MyStruct 
{

     FString StructName;

     MyStruct() : StructName("Default")
     {}

     MyStruct(const FString& InString) : StructName(InString)
     {}
};

struct Structure
{

};

struct Structure2
{
Structure a; //me creating an object of structure

void CreateStructure(FString, iString)
{
Structure iString; //me attempting to use the value of iString as the name of a new Structure object.
}

};

This is what I’m trying to do. I want to use the value of a FString to use as the name while creating a new object.

Wait… the name of the VARIABLE? That’s not possible. I assume you haven’t programmed before? Why would you need this?

Hi Snowl0l

I am not sure of what you are trying to achieve, but from your explanation i conclude that you want to refer (or ‘map’) a certain structure with an arbitrary name which is a string. And assuming you’re doing that because you will have more than one of them in the game then you may want to use a TMap , something like this:

/// This is the data structure

USTRUCT(BlueprintType)
struct FMyStruct
{
	...	
};

/// This is somewhere inside an object that manages the structure

TMap<FString, FMyStruct> MyStructs;

void GetOrCreateStruct( const FString& Name, FMyStruct& OutMyStruct )
{
	FMyStruct* Entry = MyStructs.Find( Name );
	if ( !Entry )
	{
		// There's no such entry, add a new one
		OutMyStruct = MyStructs.Add( Name, FMyStruct() );
	}
	else
	{
		// Yes there is, use it as the output
		OutMyStruct = *Entry;
	}
}