vendredi 5 août 2022

'identifier undefined' in C++11 for-loop with USTRUCT

I am implementing logging functionality in Unreal Engine 4.27 (in C++). A key part of my code is a function that is called once per game-tick. This function is responsible for iterating over an array of actors that I would like to log data for, checking whether a new log entry should be written at this point in time and calling the necessary functions to do that.

I am iterating over elements of a TArray of UStructs: LogObject->LoggingInfo = TArray<FActorLoggingInformation>. This array is defined as a UProperty of LogObject. In the loop I have to change the values of the elements so I want to work with the original items and "label" the current item as "ActorLoggingInfo". I have seen this done generally in cpp and also with TArrays. And yet my code does not work, there is no error message, but ActorLoggingInfo is undefined, thus the if-condition is never met.

This is the for-loop:

for (FActorLoggingInformation& ActorLoggingInfo : LogObject->LoggingInfo) {
        if (ActorLoggingInfo.LogNextTick == true) {
            ActorLoggingInfo.LogNextTick = false;
            ...
        }
        ...
    }

This is the definition of FActorLoggingInformation:

USTRUCT(BlueprintType)
struct FActorLoggingInformation
{
    GENERATED_BODY()
public:
    FActorLoggingInformation()
    {
    }

    FActorLoggingInformation(int32 LogTimer, AActor* Actor, FString LogName)
    {
        this->LogTimer = LogTimer;
        this->LogNextTick = false;
        ...
    }
    // Specifies Logging Frequency in ms
    UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
    int32 LogTimer;
    bool LogNextTick;
    ...
};

This is the debugger at run-time:

Debugger at run-time

Additional Notes:

1. Something that consistently works for me is omitting the &, using:

for (FActorLoggingInformation ActorLoggingInfo : LogObject->LoggingInfo)

However, this is creating useless duplicates on a per-tick basis and complicates applying changes to the original objects from within in the for-loop, so it is not a viable option.

2. I have also tried auto& instead of FActorLoggingInformation& as used in the examples above, but I encountered the same issue, so I thought it would be best to be as explicit as possible.

I would be very thankful if you had any ideas how I can fix this :) Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire