Create a FColor array inside a blueprint function library to get values inside of the array.

Hello, Basically, I have a bunch of objects in my game and I want them to get a color each of them. I have a table of the RGB values for the 61 objects of them. I want to create a function in my blueprint function library that will ask for a integer, and it will return in the FColor from the array. I just have some questions on how to initialize the array out.

Inside of my FunctionLibrary Class, do I just create the following?



private:
    TArray<FColor> ObjectColors = {FColor(255,255,0,255), 
FColor(255,220,0,255),
FColor(255,190,0,255),
...
FColor(0,255,255,255) };


Then, on the CPP function to get the value, I am doing the following



FColor UMyBPFunctionLibrary::GetFColorForObject(int Index) {
	if (Index < 13 || Index > 73)
	{
		return FColor(255, 114, 143, 255);
	}
	return ObjectColors.GetData(Index-13);
}


But it fails at the last return line. I get on Visual Studio an error underlining ObjectColors with “a nonstatic member reference must be relative to a specific object”.

So, how do I create a TArray variable, write in the values for each element, then use a function to get one of the values?

Hey Motanum. A TArray.GetData(index) returns a pointer (so in this case an FColor*), however your function isn’t made for pointers. There should be 2 solutions tho! The first one is de-referencing, you can do this by instead of having “return ObjectColors.GetData(Index-13);” having “return *ObjectColors.GetData(Index-13);”. The second option (which I generally tend to use) is just using “ObjectColors[index]”. Which just returns the value and not a pointer to it. I think this should solve it, and if not be sure to get back :)!

Thanks for the reply, However, I just did something else. I made DataTable object and simply export the colors as a CVS, much better solution as it doesn’t require a c++ recompile if I change up my data table values.

Nice, datatables rock!