Download

Interface inside Structs

Hi, so if I understand interfaces correctly, I have to send in the original actor that implements the interface when calling an interface function, example:

Every few seconds in my game I call a function that loops on a few arrays, and I call the this function on each element:


for (AActor* Background : BackgroundRecycleBin)
    {
        IChangeableDynamicMaterial* ChangeableMat = Cast<IChangeableDynamicMaterial>(Background);
        if (ChangeableMat)
        {
            ChangeableMat->Execute_ChangeMaterialColors(**Background**, 0.f, FVector(1.f, 0.f, 0.f));
        }
    }

I do that on 4 arrays, as you can see, I am casting to the interface for a lot of objects every few seconds, possibly bad for performance.
So I thought of looping on these arrays in beginplay and saving them in a ChangeableMat reference array, this is what I basically want to do:


    for (IChangeableDynamicMaterial* ChangeableMat : MaterialReferencesArray)
    {
        ChangeableMat->Execute_ChangeMaterialColors(???, 0.f, FVector(1.f, 0.f, 0.f));
    }

However now when I call the Execute function for the interface, I have nothing to pass in as the original actor

So what I thought of is fill an array of a struct at BeginPlay() that holds the AActor* OriginalActor(Background) and the IChangeableDynamicMaterial* ChangeableMat, and loop on that whenever I need, which saves the need to cast all these actors into interfaces every few seconds, the only problem is I just found out unreal doesn’t allow a struct that has an interface in it, so I have no idea what to do now.

The only thing I can think of is to use normal C++ struct but that doesn’t seem to work, I get hundreds of compile errors when I do that… Any one know how I can approach this?