Member Valiable losing value on Tick (c++)

You must be setting bTriggerPulled somewhere. Is it a simple boolean, no UPROPERTY and such?

Does it log “Trigger” at least once?

What whappens when you set bTriggerPulled to true, before the end of Tick? Does it log “Trigger” now?

Hi.
I’m currently with a problem when trying to use tick to decide if I should or not do something based on a member variable boolean value.

Currently, I have a BindAction on my Player Character class, that when a given key is pressed, set the value of my member variable bTriggerPulled to True:

void AShooterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);
    PlayerInputComponent->BindAction("HandleTrigger", IE_Pressed, this, &AShooterCharacter::PullTrigger);
}

void AShooterCharacter::PullTrigger()
{
	if(GEngine != nullptr)
	{
		GEngine->AddOnScreenDebugMessage(1, 2.f, FColor::Yellow, TEXT("Trigger is pressed"));
	}

	bTriggerPulled = true;
}

In my Tick() function, I am checking bTriggerPulled to decide if I print or not something to Console Log:

void AShooterCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	
	if(bTriggerPulled)
	{
		UE_LOG(LogTemp, Warning, TEXT("Trigger"));	
	}
}

The problem is that the bTriggerPulled variable always is set back to false when Tick() is called. I didn’t make to fund a place where that value is being overwritten, and when inspecting that value searching for all its use, only these two place are showed.

I already cleaned the value /Saved and /Binaries and /Intermediate folders to generate a new solution, but it dint resolved my problem.

Sorry, but this can only happen if you’re setting the variable to false somewhere or PullTrigger() function is not being called.

Are you sure those 2 places are the only ones? Perhaps you’re setting the variable to false from some other class?

Sadly, neither Visual Studio is finding another reference.
I’m thinking if maybe the UE4Editor is changing that value somehow? But it doesn’t make sense, since it’s a new variable, not a UProperty, and that class is not inherited anywhere besides in a Blueprint.

Before I had made the bTriggerPulled a UPROPERTY(), but I erased that macro to see if the problem stop, with no success.

Trigger is not logged even once.
If i put a “bTriggerPulled = true” before the end of Tick() but after the if statement, the Trigger log happens as intended. But my target here is to be able to flag and un-flag the bTriggerPulled variable, and call another function when is True.

Is PullTrigger() executed? Other than that, I’m out of ideas.

PS: it’s good pratice to always initialized your variables, either in the declaration or in the constructor.

Yep, it’s. I put a AddOnScreenDebugMessage inside to make sure its executed.

haha. Don’t worry EvilCleric. Let’s hope that someone see this and help us somehow. :slight_smile:

So, I discover what was the problem, it’s a silly one, but I will swallow my pride and share here anyways.

My class AShooterCharacter is inherited in a blueprint, and that blueprint is placed N times on my level, the enemies. When I was debugging, my IDE don’t necessarily differed from the instances of that specific class. Because only one specific instance of AShoterCharacter (my player character) was responsible for changing the value of bTriggerPulled, when I was debugging Tick(), the value from another instance of my class was being displayed.

So yeah, a bit silly :slight_smile:

Kudos for @TheKaosSpectrum for mentioning that earlier on discord, really helped me!

i actually mentioned this on Discord when you had the issue

In fact, you really have. Sorry for not giving you the deserved credits.