Hello! I am relatively new to C++ and UE4, and for an experiment, i have tried adding this line of code in the public section of the AActor class in Actor.h
virtual void Hit();
However i get the following error when i try to compile (well, when i try and add code to the project, oddly enough, i can compile fine just with the compile button)
I am not sure what the problem is.
I just noticed you wrote *" i have tried adding this line of code in the public section of the AActor class in Actor.h" *
Are you editing the Actor class itself?
You should not edit the actor class.
You should create a child class and work on that
create a MyActor class that derives from Actor.
You can do that in the editor / content browser / new C++ class / Parent Class Actor
that should open up both your New Class Header and CPP file in Visual Studio
Compile them with no changes.
Then add the function declaration like you did to the .h file and the prototype to .cpp it should work.
I am editing the actor class, the idea was to use a virtual function so that the children of the actor class can have their own prototypes of Hit(), so i’m guessing this isn’t the way to do it. Thanks for the help!
But to refer to your errors, you’ve got linker errors because you didn’t provide an implementation. You can write in the header file e.g. virtual void Hit() {} to provide a empty one. But like Colorado said, just don’t edit the Actor class.
Create a new base class
Actor
|
v Unit (your class with the hit function
|
v Your classes which should inherit the hit function
If you modify AActor, then you modify everything that inherits from it, including Epic’s classes. Sometimes you want to do that. That said, unless you intend to send a pull request to Epic, you have just forked the engine and take full responsibility for merging your edits with theirs for each and every update that Epic releases. Not fun. This could be like, one or more full-time jobs worth of person-hours, depending on how much you change.
If your change should only affect YOUR code, then you’ll want to create a subclass of AActor with your edits, and then subclass THAT subclass for any class that should inherit from it (as the quoted statement shows).
I believe you can just implement an empty function in the cpp file. Keep what you have in your header and add TheClass::YourFunction(){} etc… Unresolved external symbol most likely means you are using that function somewhere but there is no implementation for the linker. As long as you override it in the child class it will call the child’s version. Also, you can call the parent function if you decide to not have it empty and have some boilerplate code. Hope this helps