0

I'm trying to define a function to take UActorComponent as an argument and use FindComponentByClass to initialize based on its classType, so I used template as follow:

template<class T>
void FindActorComponent(T *aComponent){
    aComponent = GetOwner()->FindComponentByClasss<T>();
    if(aComponent) //doSomething;
    else //doOther;
}

However, I found that the passed object by pointer(aComponent) came out nullptr from function, although it's not after initializing inside it!

So, I searched for a solution for the situation and found this: Pass pointer by reference, and after trying it by changing the argument from (T *aComonent) to (T *&aComponent) I found it working!

Can anyone explain to me the concept behind this, and why it didn't affect the passed object although it's passed by pointer?

Also if I want to execute some functions based on passed class type(like if it's UInputComponent to BindAction), how should I do this? I tried to use GetClass() but I couldn't go anywhere with it!

Thanks in Advance.

AstroMan
  • 61
  • 1
  • 10
  • 1
    Because you've passed the pointer by `value`. Which means the function gets a copy of the pointer. When you assign the pointer it'll only be visible to that function. It's exactly the same as passing an `int` by value. The function gets its own copy of the `int`, any changes to the `int` in the function body will not be visible to the caller. – WBuck Jul 26 '21 at 01:11
  • @WBuck Could you please provide the implementation in case of double pointer instead of pointer to reference? Also the second part of the question about checking class type to execute some code? Thanks! – AstroMan Jul 26 '21 at 01:22
  • What's wrong with returning a pointer? `if(aComponent != nullptr && aComponent->IsA()){ /* do something*/ }`. – George Jul 26 '21 at 07:13
  • @George There's nothing wrong with returning a pointer, as well as using a reference to pointer without return, which is safer and working without problem. I was looking for an explanation which provided by WBuck, and an implementation using double pointer instead of reference to pointer which I can't find or implement myself! – AstroMan Jul 26 '21 at 10:33
  • @George Regarding the provided code, when I use it to UE_LOG the action it compiles & works just fine and log the component name, but if I use it to BindAction() or execute any function related only to UInputComponent and not common between UActorComponent it doesn't want to compile and gives error of: **BindAction is not a member function!** – AstroMan Jul 26 '21 at 10:41
  • @George i.e. `if(aComponent->IsA()) UE_LOG(LogTemp, Warning, TEXT("%s is an inputComponent"), *aComponent->GetName()); //Works just fine!` but if I use BindAction like: `if(aComponent->IsA()) aComponent->BindAction("Pressed", IE_Pressed, this, &UClick::Press); //gives previously mentioned error!` – AstroMan Jul 26 '21 at 10:49
  • 1
    `if(UInputComponent* InputComponent = Cast(aComponent)) InputComponent ->BindAction("Pressed", IE_Pressed, this, &UClick::Press);` – George Jul 26 '21 at 12:27
  • @George That really worked! Thanks George for your help, if you could please provide a solution includes using double pointer as function argument and any recommendation or guidance for me, I'd appreciate that very much! Thanks again man. – AstroMan Jul 26 '21 at 14:11

0 Answers0