How to set a bool on an ActorComponent from an Actor

Hi there,
First of all, I’m very new to C++. I’m trying to set a bool to true from an Actor in an ActorComponent, but when I run the game, it throws me an error that I can’t figure out. I’m almost pretty sure that the problem is lying in the casting, but still… please, help!

Here is the relevant code from the Actor.h:

And the Actor.cpp:
actor.cpp

What I’m trying to do in the ActorComponent.h:
actorcomponent.h

And in the ActorComponent.cpp:
actorcomponent.cpp

Basically, in my game there is a turret (the Actor) that rotates if that bool is true. I have another Actor (which has the ActorComponent) that the player can interact with to turn the rotation on.

Also, the error that I’m getting (basically, the Editor crashes)

The error referencing the { in the Actor.cpp

Please, help me. I would like to learn from my mistake. Thanks!

First thing, Instead of

Turret->ATurret::SetSwitch();

It should be

Turret->SetSwitch();

Secondly,

Since the component is on another actor,

Cast<ATurret>(GetOwner())

will fail since the Owner is not a Turret and the Turret variable will be nullptr, hence the crash when calling SetSwitch.

Since you want the player to interact with (I assume) a button which turns on the turret rotation. I suggest using an Interface instead.

You’ll will also want to null check any pointers before using:

Turret* turret = Cast<ATurret>(GetOwner())
if(turret != nullptr)
{
 turret->SetSwitch();
}

Thank you! While I was waiting for the approval, I researched it and got it work.

1 Like