Setting up an use-key for many classes (c++)

I used the action mapping on the project settings to set up a “use”-key (E), then I went to my character.cpp to make the input component : InputComponent->BindAction(“use”, IE_Pressed, this, &ApracticeCharacter::usekey);
Furthermore I declared an void usekey() function in the character.h, now I would have to implement it in the character.cpp.
But I dont want this key to trigger one particular event like toggle a light, I just want to have other classes to be aware of this usekey() function to be called or not. I cant figure out to find the proper way to organize this. For example in the future I would like this E key with its usekey() function to pick up an item, to unlock a door, to toggle a light…and so on. So I probably dont want to cram this usekey function with all that stuff, there has to be a clean way to let other classes call this usekey() function and check if its being called or not, so the actual programming for those events remains in the classes. Please help.

Hi !

I think you should consider using Interfaces :

This will give you the ability to use the same functions (here, useKey()) on every class that implements the Interface, and to handle the use of the E key separately.

At least, it reinforce the fact that this should answer apfelnymous’s query :wink:

Hello,

I think what you are looking for are Interfaces. That way your “use” function can easily determine if an actor is usable.
Here is a tutorial about Interfaces.

Regards Jan

Looks like i have been a bit too slow :slight_smile:

Thank you, Im gonna learn about it and give a response !

Multiple class inheritance causes my compiler to mark some definitions red and state : “…is ambiguous”

Do both classes you are inheriting from have a function / member with the same name?

Nevermind, I did some unnecessary inheritance.

Im having an error I cant understand.

Info Performing 2 actions (4 in parallel)
Info UsekeyInterface.cpp
Error c:\program files\unreal engine\4.7\engine\source\runtime\core\public\delegates\DelegateInstancesImpl_Variadics.inl(603) : error C2338: You cannot use UObject method delegates with raw pointers.
Info c:\program files\unreal engine\4.7\engine\source\runtime\core\public\delegates\DelegateInstancesImpl_Variadics.inl(749) : see reference to class template instantiation ‘TBaseUObjectMethodDelegateInstance (void),>’ being compiled
Info with
Info C:\Users\Admin\Documents\Unreal Projects\practice\Source\practice\UsekeyInterface.cpp(9) : see reference to class template instantiation ‘TBaseDelegate,>::TUObjectMethodDelegate’ being compiled
Info ]
Info [
Info UserClass=AUsekeyInterface
Info with
Info c:\program files\unreal engine\4.7\engine\source\runtime\core\public\delegates\DelegateSignatureImpl_Variadics.inl(72) : see reference to class template instantiation ‘TBaseUObjectMethodDelegateInstance’ being compiled
Info ]
Info UserClass=AUsekeyInterface
Info [
Info -------- End Detailed Actions Stats -----------------------------------------------------------
Info ERROR: UBT ERROR: Failed to produce item: C:\Users\Admin\Documents\Unreal Projects\practice\Binaries\Win64\UE4Editor-practice-1676-Win64-DebugGame.dll
Info Cumulative action seconds (8 processors): 0,00 building projects, 0,13 compiling, 0,00 creating app bundles, 0,00 generating debug info, 0,00 linking, 0,00 other
Info UBT execution time: 6,68 seconds