TArray FString Conversion issue

I feel what I’m trying to do is fairly simple but I can’t get it working because of a type conversion issue.

I have a class (PlanetManager.cpp) that I want to include a static array of planet names (TArray) and then when the game starts I want to grab some random planet names from that array to use.

In my .h:

...
private:
    static TArray<FString> planetNames;
...

And then in my .cpp:

UPlanetManager::UPlanetManager()
{
...
	UPlanetManager::planetNames[10] = {
		"Zephyria", "Aurora Prime", "Nebulon-9", "Elysium-IV", "Galactica", "Nova Terra", "Stellaris-Alpha", "Celestia", "Luminara", "Orbitus-Minor"
	};
...
}

...

void UPlanetManager::GeneratePlanets()
{
	for (int i = 0; i < planetCount; i++)
	{
		FString planetName = UPlanetManager::planetNames[i];

                //do something with planetName
	}
}

However when I try to create the planetName variable inside GeneratePlanets() I get the following error in VS:

no suitable user-defined conversion from "TArray<FString, FDefaultAllocator>" to "FString" exists

I’m not quite understanding why the type of an individual element is not an FString with the way I’ve set it up.

Any help would be greatly appreciated.

Hey @fletch254,

I believe the problem is with how you are initializing planetNames and then accessing its elements. You’re attempting to assign a list of strings to a single array element is incorrect.

You can’t assign values to an array like planetNames[10] = {...}; — this syntax attempts to assign to the 11th element of the array, not initialize the entire array.

Try this:

// Initialize your static array
TArray<FString> UPlanetManager::planetNames = {
    "Zephyria", "Aurora Prime", "Nebulon-9", "Elysium-IV", "Galactica",
    "Nova Terra", "Stellaris-Alpha", "Celestia", "Luminara", "Orbitus-Minor"
};

Give that a try and let me know if you have any other problems.