How do i access a function?

Can anyone, explain me from this shown example how to access a function/void from a different script/class? Simple example:



            Do something();


#include "Example.h"

InputComponent->BindAction("DoSomething",IE_Pressed this, &ACharacter::ExampleFunction);


          Get Myfunction from Example.cpp();

Now i want to call the MyFunction(); function from the Example.cpp into my Character.cpp. But how do i do that? Using Pointers? Cast?

Would be nice if someone could show me the way how to do it based on my example. Thanks.

You need a refference of your Example Object before you can Call anything. Think about it you can spawn 100 different Example Objects. Which one of them does execute the DoSomething? Think about it like you have AEnemy calss and it has a Function called TakeDamge() you surly dont want every AEnemy in your Level to Take Damge right? Just a single one.

But you can also make a static Function this one you can call trough your Namespace like AExample::DoSomething() but in this case it does not get called from the individual Objects. This function does basicly dont know who is the Caller think of it more like a Global think. So if you would have a AEnemy::TakeDamage() it would not know about the AEnemy that does the Call and would not know what specific AEnemy Object should take damage.

But if the static Function does not do anything per Object you can use static.

So now you ask yourself how would you get a refference to a AExample Object? There are many ways but usually you are in charge to Set a refference somewhere up where your ACharacter can reach it. Or get it from Something else like a Overlap Event (you need to Cast to AExample to check if it is indeed a AExample Object you Overlap) same goes for Linetraces. Or wherever you Create the AExample object gets also a refference pointer he can send to other Objects like your PlayerController.

Thats just a rough Explanation I recommand you to pick up a C++ book to learn the foundamentals or take also a look at this Video its for Blueprints but the Communication part falls under the same logic. Get a refference to you desired Object than you can Call functions on it.

Hope this helps you to understand the Problem you are facing right now.

Oh one last thing “void” is the return Value. And void basicly means return nothing. Call this Function and return me nothing, just exectue something.

i think he is thinking that void means function :stuck_out_tongue:

could be thats why the rough explanation without going into technical things to much =)

You might want to start from here: