Download

Unable to use variables from a different actors header file

Hey, so recently I setup a way to dash and along with that a Boolean that enables and disables the ability to dash(Setup in the third person characters header file). I also setup a actor that when you collide with it it destroys the actor and should set CanDash to true, but when using

AThirdPersonCharacter::CanDash = true;

the error “a nonstatic member reference must be relative to a specific object” occurs. I also tried doing this,

AThirdPersonCharacter ThirdPersonChar;

ThirdPersonChar.CanDash = true;

It gets to 75% init and throws a breakpoint with the error “FObjectInitializer::Get() can only be used inside of UObject-derived class constructor.”

If you could help me with this problem or propose another idea to achieve this same function that would help so much.

This is a tough one since your question shows a misunderstanding or lack of knowledge of objective oriented programming in general.

I’d be happy to write an explanation of what you’re doing wrong here using your example if you want. Other than that I suggest reading a bit about what classes and objects are.

It would be great, if you explained what I did wrong, but I will also try to learn a lot more about the object oriented side of CPP.

This is not really a solution to your problem, but rather an explanation of how you need to approach the solution. I’d suggest you read this, and then find a guide for handling overlaps / collisions in UE4. There’s a lot to understand so take it easy. Repetition is key.

The basic foundation of OOP is classes and objects pretty much. Classes and Objects are connected, but not the same.

To put it simply. A Class is the instruction on how to create Objects. Basically, the class is the mould, and the object is the result of the mould. An object is an instance of a class.

So, AThirdPersonCharacter is your Class. When you want to create an actual AThirdPersonCharacter, the engine runs through the Constructor in your class and creates an Object. In C++ a constructor looks like “ClassName::ClassName”. You can create as many objects as you’d like from the same class, all containing the same properties and functions. After an object is created, you can modify it without modifying other objects of the same class.

In OOP, there are certain types of properties and functions marked as ‘static’. Static properties and functions do not modify a specific object, but the class directly.

AThirdPersonCharacter::CanDash = true;

This is a static call. You are referring to the class directly and not an object. Static properties are used when we want to change something on the class. This is rare.

What you want is to change something on the object.

AThirdPersonCharacter ThirdPersonChar;
ThirdPersonChar.CanDash = true;

This is a call on a specific object, so this is “correct” in that sense.
However, the first line creates a new object of type AThirdPersonCharacter. Then it changes CanDash on that newly created object. It does not reference an already existing object in the world, which is likely what you want to change.

You need to somehow get a reference to the object you want to change. How you do this will depend on what class is attempting to change the object.
If you simply want to change CanDash to true in your object, you just write “CanDash = true;” and it will change CanDash for the object itself.

If you set CanDash from the object you collide with, you need to retrieve the colliding character in the object and reference that.
Imagine the life of your Collision Actor. What does this actor need to know? Well it doesn’t need to know about a whole lot when it’s just standing there. It needs to know about itself, that it is in a world (your map), if it’s being possessed by an AI or Player and that’s basically it. It has no idea if there are other actors or objects in the world, since it doesn’t matter to it.
Suddenly another actor collides with it. Now it needs to know about this other actor that collided with it, so this triggers an event with details about the object that hit it. What hit me? Was it a sword, a bullet, another person? You can use this event, to change CanDash on the actor that collided with it.

Can’t remember what the exact name of the functions are, but every actor has a collision box, and when this collision box detects a collision, it will trigger an event, which runs some code. This event will contain information about who the actor collided with, and this will be the character you want to change CanDash on.

Hope this is of some help to you :slight_smile:

Hey, just figured it out, I set it up so it make candash true every time the third person character collides with a trigger volume. Then shaped the trigger volume around the obstacle so that it looks like you picked something up and can now dash again. Thank you so much for your help man, appreciated.