In UE5 I'm trying to update some variables declared in a CharAnimation.h file and defined in a tick function within my CharAnimtion.cpp file. The variables are used within a separate animation blueprint based on my CharAnim.h/.cpp files in unreal. These variables change based on a player key press in a separate class declared/defined in my Grabber.h/.cpp file(s). I'm attempting to do this by declaring static class variables within the Grabber class. Although, I keep getting multiple errors.
My Code
It's only when attempting to compare the static variables that I get problems. My grabber feature, blueprint variable visibility, and KeyPressed functions work as intended so I'll just use some generic code snippets to simplify the readability of my problem.
Grabber.h File
Variables to pass to animations computation:
class: UGrabber {
public:
static bool IsKeyPress, IsHoldingActor, IsStartingGrab, IsGrabbing, IsDroppingActor, IsGrabbedButNoActor;
};
bool UGrabber::IsKeyPress = true;
bool UGrabber::IsHoldingActor = false;
bool UGrabber::IsStartingGrab = false;
bool UGrabber::IsGrabbing = false;
bool UGrabber::IsDroppingActor = false;
bool UGrabber::IsGrabbedButNoActor = false;
Grabber.cpp File
Key press function that updates static variables:
void UGrabber::Pressed()
{
// Player press grab key //
IsKeyPress = true;
// Player pressed key and hit something movable //
if(PlayerHitSomething) {
// Player isn't holding something and will now begin grab animation //
if(IsHoldingActor == false) {
IsStartingGrab = true;
IsGrabbing = true;
IsHoldingActor = true;
IsKeyPress = false;
}
// Player press key and is already holding something, they will drop it //
} else {
if(IsHoldingActor == true) {
IsDroppingActor = true;
IsDroppingActor = false;
IsHoldingActor = false;
IsKeyPress = false;
// Player tried to pick something up that wasn't movable //
} else {
IsGrabbedButNoActor = true;
IsHoldingActor = false;
IsGrabbedButNoActor = false;
IsKeyPress = false;
}
}
}
Animation.h File
Passed to blueprint:
public:
bool KeyPress, HoldingActor, StartingGrab, Grabbing, DroppingActor, GrabbedButNoActor;
Animation.cpp File
Assigns blank variables to static variables computed in grabber.cpp
#include "Grabber.h"
UpdateEveryFrameFunction() {
KeyPress = IsKeyPress;
Holding = IsHoldingActor;
StartGrab = IsHoldingActor;
Grab = IsGrabbing;
DropActor = IsDroppingActor;
GrabbedNoActor = IsGrabbedButNoActor;
}
Object Errors
- "CharAnimation.cpp.obj was modified or is new"
- "Grabber.cpp.obj was modified or is new"
- "CharAnimation..gen.cpp.obj was modified or is new"
- "Grabber.gen.cpp.obj was modified or is new"
Var Errors
- "Grabber.cpp.obj : error LNK2005: "public: static bool UGrabber::IsKeyPress" (?IsKeyPress@UGrabber@@2_NA) already defined in CharAnimLogic.cpp.obj"
- "Grabber.gen.cpp.obj : error LNK2005: "public: static bool UGrabber::IsKeyPress" (?IsKeyPress@UGrabber@@2_NA) already defined in CharAnimLogic.cpp.obj"
- "fatal error LNK1169: one or more multiply defined symbols found"
Things I tried
- I attempted to make a global library and include it in both .cpp files. But I continuously got "Multiple defined" variable errors. So I figured using static class variables would be best because they give ONE variable instance.
- I tried making a pointer to my grabber class from my CharAnimation.cpp to access the variables.
- I tried to make an object of the class but it only created copies of the variables so while the original variables changed in my grabber files, they did not change in my CharAnim files Grabber object.
- I've tried a lot more, but I've been working on allowing the animations I made to update based on my grabbers logic for about 4 days now.