Download

Call function from other class

Hello,

I just start using unreal and have a quick question about how to properly call a function from another class.

In my project, there’s time that I want to destroy an object (a box for example)



#Character.h#
UPROPERTY(VisibleAnywhere, Category=Mesh) 
class ABoxA* a; // reference to class BoxA

and when I call it in Character.cpp file

a->DestroyBox();

----------------

#BoxA.cpp#
void ABoxA::DestroyBox() {
	Destroy();
}

It looks fine with IntelliSense but whenever I fire a function, it failed instantly.

Any help will be greatly appreciate.

I don’t know what you mean by function failed but I assume you mean that the function did not do anything but it didn’t crash the engine either.

In this case you need to first check if the function is actually being called at all. To do this add a simple

GEngine->AddOnScreenDebugMessage(FMath::Rand(),5, FColor::Cyan,“DestroyBoxCalled”)

to the destroy box function or something similar and see the results

If “Failed” means it crashed, then your ‘a’ variable is probably null.

The editor will tell you about any errors when you compile it, except for that. Make sure your a variable isn’t null, possibly using the method shown above.

Also if you read the crash log, it usually gives helpful error messages.

Paste code inside of the code tag.

He’s trying to apply Unity basic concepts and gonna have a hard time by doing so.
Is easier route to simply ignore your Unity experience and learn how Unreal works instead.

you need to use cast, if you can’t tell me to make example and there’s other example on the forum

May i ask why you put a Random Function into the index parameter? If you want to achieve that messages don’t overwrite previous ones, then you can just
put in a “-1” :smiley:

@Topic: I’m also pretty sure you are calling the function without having an actor in it.
That thing you declared there is a pointer! You may want to read up the pointer usage in C++.
C# is way simpler in that term, but also doesn’t offer that much control.