0

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.

  1. 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;
  1. 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;
        }
    }
}
  1. Animation.h File

Passed to blueprint:

public:    
bool KeyPress, HoldingActor, StartingGrab, Grabbing, DroppingActor, GrabbedButNoActor;
  1. 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

  1. "CharAnimation.cpp.obj was modified or is new"
  2. "Grabber.cpp.obj was modified or is new"
  3. "CharAnimation..gen.cpp.obj was modified or is new"
  4. "Grabber.gen.cpp.obj was modified or is new"

Var Errors

  1. "Grabber.cpp.obj : error LNK2005: "public: static bool UGrabber::IsKeyPress" (?IsKeyPress@UGrabber@@2_NA) already defined in CharAnimLogic.cpp.obj"
  2. "Grabber.gen.cpp.obj : error LNK2005: "public: static bool UGrabber::IsKeyPress" (?IsKeyPress@UGrabber@@2_NA) already defined in CharAnimLogic.cpp.obj"
  3. "fatal error LNK1169: one or more multiply defined symbols found"

Things I tried

  1. 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.
  2. I tried making a pointer to my grabber class from my CharAnimation.cpp to access the variables.
  3. 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.
  4. 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.
  • 1
    Defining the static member variables needs to be done from the `.cpp` file, otherwise any translation unit including the header will get it's own version of the variable resulting in the linker error. – fabian Jun 25 '22 at 19:29
  • @fabian "Defining the static member variables needs to be done from the .cpp file." This was it. You are a god sent, and I feel like an idiot lmao. Thank you! – SirMrTyler Jun 25 '22 at 19:47

0 Answers0