Returning Reference to Structure Not Working!

AngularRat
Tue, 12 Oct 2021 01:05:36 +0000
Editor Scripting

I have a TArray of USTRUCTs. I use these to draw a bunch of rectangles, and it works fine. All is exposed to Blueprints as well, which is where the problem is.

I have a function (FindRoomById) that gets a particular USTRUCT from my TArray, and for whatever reason I cannot get this to return a reference to the original item properly. So any changes made are not reflected in the source array. HOWEVER, within the function itself, I clearly have a reference and not a copy, because if I chance a value there (as shown in code below), it works and my color changes to Red.

Yet when I use the returned reference after calling the above function in Blueprints, nothing I do to the structure affects the original…for some reason, I always have a copy. What is going on? What am I missing? There must be something about the pointer dereferencing or my function parameter definitions that I’m missing, but I can’t find it.

Sample Code for Struct:

USTRUCT(BlueprintType)
struct ARMAPTOOLS_API FRoom
{
	GENERATED_BODY()

public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Room")
	int32 Id;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Room")
	FVector Location;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Room")
	int32 Width;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Room")
	int32 Length;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Room")
	int32 Height;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Room")
	FLinearColor Color;

	FORCEINLINE FRoom();

	FORCEINLINE FRoom(int32 id, FVector location, int32 width, int32 length, int32 height);
};

Sample Code for Function to Get Reference to Item:

bool AARMapGenerator::FindRoomById(int32 id, FRoom& roomRef) {
	bool status = false;

	FRoom* ref = (GeneratedRooms.FindByPredicate(
		[&](FRoom otherRoom) {
			return otherRoom.Id == id;
		}
	));

	if(ref != nullptr) {
		ref->Color = FColor::Red.ReinterpretAsLinear();
		status = true;
	}

	roomRef = *ref;

	return status;
}

2 posts - 1 participant