dimanche 25 juillet 2021

Template function to FindComponentByClass in Unreal Engine

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.

Aucun commentaire:

Enregistrer un commentaire