Call a function from a other class/actor in InputComponent->BindAction

Hey guys!

First of all: i’m pretty new to UE4.

I want the InputComponent->BindAction to call a function from a other class. How can i do that? I try to get this working for like 4 hours now and don’t get anything… So hopefully you guys can help me out!

When you need more Information, please let me know!

Sincerely :slight_smile:

bump

Does no one have a clue?

Bind your action to a function in your current class. From there call your function in another class.

Thank you so much for your answer!

Ok, this was pretty basic… Anyways, how do i call this function out of another class?

I have this code in the header file of my player (AKeyPickUp is, of course, the class with the function i want to call):



protected:
	UPROPERTY(EditDefaultsOnly, Category = "MyCategory")
		class AKeyPickUp* Other;


Now i want to call this function through:



Other->PickKeyUp();


But i get this error: Access violation when writing to position 0x0000000000000330

Am i missing something?

Ok, so i think i found the problem.

This was the entire function:



void AKeyPickUp::PickKeyUp()
{
	if (canPickUpKey)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Cyan, TEXT("Called PickKeyUp() function"));
		HasKey = true;
		this->Destroy();
	}
}


I only get this error when i use any variable from the KeyPickUp class. Otherwise, when i only call ‘GEngine->AddOnScreenDebugMessage’, it works perfect.

For explanation: the bool ‘canPickUpKey’ is true, when the player overlaps with the sphere collider of this actor. Only in this sphere, the player can pick up the key. But, as i said, it’s irrelevant which variable i use in this function, i’m always getting “Access violation when writing to position 0x0000000000000330”…

How are you setting the pointer Other? If you just declare it, it will just be NULL, and calling a function on it will crash. You’ll need to set it somehow (via a line trace, via an iteration over all AKeyPickUp instances etc).

Thanks for your answer!

This could be the problem. I posted all the code i use for this, but why is ‘AddOnScreenDebugMessage’ working then?

Are there any tutorials for iteration or line trace?

Sorry, i’m new to Unreal Engine and C++ and my knowledge comes from Unity.

Sincerely

I finally solved it!

The codeword was ‘iterator’.

I added/edited this code:



void AAbandonedPlayer::PickUpKey()
{
	for (TActorIterator<AKeyPickUp> ActorItr(GetWorld()); ActorItr; ++ActorItr)
	{
		ActorItr->PickKeyUp();
	}
}


Thank you both, that you have brought me on the right path! :smiley:

Sincerely

Oops, i forgot to mention, that i made a #include at the top of the player.cpp.


#include "KeyPickUp.h"

Just in case someone comes on this thread ^^