Get "this" in C++ Returns Same Pawn for Every Player

At first I posted code but then I figured it may have nothing to do with my code. But what would cause the following behavior:

Getting “Self” in BP works

-“This” does not do the same thing as “Self”

May I ask what it is you are trying to do?

I don’t understand fully what the issue is here as “this” is just a way to reference the instance of the class you are currently in, which is the same as “self” in Blueprint.

Hi , I am trying to get “this” because I need to reference the local player so that a line trace ignores it for a shoot function. :slight_smile:

^^^ so i definitely think this issue is becasue of setup in blueprint

What class are you running the trace from? Or, more specifically, can you paste the code that you are writing?

Absolutely

void AGun::Shoot_Server_Implementation(FVector Start, FVector Direction, AActor* IgnoreActor, AGun* Gun)
{
	Gun->Shoot_Implementation(Start, Direction, IgnoreActor);
}

bool AGun::Shoot_Server_Validate(FVector Start, FVector Direction, AActor* IgnoreActor, AGun* Gun)
{
	return true;
}

void AGun::Shoot_Implementation(FVector Start, FVector Direction, AActor* IgnoreActor)
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("SDSD"));
// attempt to get the correct player, was previously passing "this" from player. This doesnt not work either.
	IgnoreActor = ->GetAttachParent()->GetOwner();
	if (Role < ROLE_Authority)
	{
		Shoot_Server(Start, Direction, IgnoreActor, this);
		return;
	}
	if (!bIsShooting && bCanShootAgain)
	{
		FCollisionQueryParams CQP = FCollisionQueryParams(FName(TEXT("CQP")));
		CQP.AddIgnoredActor(IgnoreActor);
		CQP.bTraceComplex = true;
		FHitResult HitResult(ForceInit);
		FVector End = Start + Direction * FiringDistance;
		GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECC_Visibility, CQP);
		if (HitResult.Actor != NULL)
		{
			APlayerCharacter* PlayerCharacter = Cast<APlayerCharacter>(HitResult.GetActor());
			if (PlayerCharacter != NULL)
			{
				DealDamage_Multicast(PlayerCharacter);
			}
		}
		bIsShooting = true;
		bCanShootAgain = false;
		References = ->GetAttachParent()->GetOwner();
		Shoot(Start, Direction, IgnoreActor);
	}
}

In this context, “this” is going to be the instance of AGun, not the player.

In the AGun class, I would have a “PlayerOwner” Pawn type variable, which I would assign when I “give” the gun to the owning Pawn class. Then, I would use the “PawnOwner” reference as the one to ignore, instead of trying to get some attach parent → owner (which I have no clue what will be returned).

Something like:

h

	UPROPERTY(BlueprintReadOnly, Category = "My Gun")
	Pawn *PawnOwner;
	
	UFUNCTION(BlueprintCallable, Category =  "My Gun")
	void GiveGunTo(Pawn *InPawn);

cpp

void AGun::GiveGunTo(Pawn *InPawn)
{
	if(InPawn)
	{
		PawnOwner = InPawn;
	}
}

Then, in your Pawn class, when you create a AGun and give it to that Pawn, you can call the AGun function:

MyAGunReference->GiveGunTo(this);

Hello again ,

The problem with that is for some reason “this” is returning player 0, and will still not work correctly. Im going to try it in a clean project, but would you check your forums inbox?

Ok so now for some reason bp and cpp return player 0 i assume its intended that way, but for some reason this is my code now and the server cannot be shot by client, the client calls up to “call 5” though when the gun shoots server

void APlayerCharacter::DealDamage(APlayerCharacter* PlayerCharacter, float Damage)
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Called 4"));
	if (Role < ROLE_Authority)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Called 5"));
		DealDamage_Server(PlayerCharacter, Damage);
	}
	PlayerCharacter->Health -= Damage;
}

void APlayerCharacter::DealDamage_Server_Implementation(APlayerCharacter* PlayerCharacter, float Damage)
{
	DealDamage(PlayerCharacter, Damage);
}

bool APlayerCharacter::DealDamage_Server_Validate(APlayerCharacter* PlayerCharacter, float Damage)
{
	return true;
}