Thursday, May 14, 2020

Declaring a TArray parameter in a BlueprintImplementableEvent

After a long day of programming in UE4, I ran into a problem that caused me quite a headache. I have a custom PlayerController subclass in a project I'm working on, and I wanted to add a method to trigger the need for the player to choose from among a list of options. The method should look something like this:
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent)
void ChooseFrom(TArray<UScenarioAction*> Actions);

I spent too long hitting my head against the resulting error message, which looks like this:
'void AMyPlayerController::ChooseFrom(const TArray<UScenarioAction *,FDefaultAllocator> &)': overloaded member function not found in 'AMyPlayerController'

This should work: I just want a function stub that the blueprint subclass implements. I tried every conceivable combination of annotations to the UPROPERTY and tried adding empty implementations in the cpp file, knowing that there really should be nothing wrong with the code.
It wasn't until I sat on the couch in the living room that I thought that the problem might be the parameter. Sure enough, if I took out the TArray parameter, everything worked as expected. I little poking around the web inspired me to try passing the array by constant reference:
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent)
    void ChooseFrom(const TArray<UScenarioAction*>& Actions);

Sure enough, that did the trick. This seems like a place where the compiler could give a much friendlier message; there's nothing about the message it provides that makes me think that the parameter needs modification.
Hopefully writing this quick note on my blog will help save someone else an hour's aggravation—even if that someone is just future-me.

2 comments:

  1. !

    Been trying to pass an array for a couple of days now with long compile times trying to figure this one out! And it was just a simple & that was needed haha! Thanks again :D Cheers

    ReplyDelete