Two For loops cause "not all control paths return a value"

Ok i have a pretty simple code that counts X and Y up,

FVector addCoords()
    {
    	FVector TempCoord;
    	TempCoord = FVector(0, 0, 0);
    	for (int8 x = -2; x <= 2; x++)
    	{
    		for (int8 y = -2; y <= 2; y++)
    		{
    			TempCoord = FVector(x, y, 0);
    			

 			return TempCoord;
    		}
    	}
    }

Pretty Straightforward i assumed. Count them up, give the X and Y cords out as a FVector for further usage. Done that hundreds of times back when i learned Java in school.
However now i get the mentioned message that not all control paths return a value. But the point with “for” loops is, that i have just one way through, no breaks, no conditions. Just go from -2/-2 25 times up to +2/+2, return it as a Vector and done. So what seems to be the issue here?

It’s unclear what the intention is here. As it stands, that method would only be able to return precisely one vector with the values (-2, -2, 0).

Could be a confusion of terminology perhaps? An FVector in UE is just a group of three floats representing a position or a direction. I might be way off base here, but from your description it sounds like you’re looking to make a list, which in UE would be done using a TArray.

Without seeing the entire function, can’t tell you positively about the error code. But, the loop that you did show is a mistake. It is not doing what your description for it is. It will return the first value, no incrementing will happen.

It is legal to return directly from in a for loop, but it is not good practice. There are break and continue keywords for a reason. If you want to end a loop early, set your variable, then break. A goto is perfectly acceptable to break from inside a nested for loop, it is actually maybe the only reason it’s still in the language. But, you aren’t testing for a condition inside the loop, so it makes no sense why you are returning out of it prematurely.

A local variable should be set, or appended to in the loop, then outside the loop at the bottom of the function return the variable if it is one. If your appending to a referenced list you don’t need to return anything.

Ahh Yeah i recreated it inside of Blueprint and i had a mistake in my thought process.
If i use 2 Loop inside each other i connected for example the spawn of an Actor to create a whole lot of blocks or whatever. BUT there, i connected the Loop Body to the next Position, so basically putting it into my for loop.

Yeah thanks. You helped me a lot :slight_smile: My thinking was quite off.