problem help

I have never encountered this before not sure how i would fix it, any ideas?



//Line 39
	WeaponBase->Character = Instigator;


My guess is something like:



WeaponBase->Character = Cast<AFpsVsBotsCharacter>(Instigator);


Pretty much what said. You are trying to assign an APawn to an AFpsVsBotsCharacter Variable which is sort of like trying to dress up a Neanderthal as a Homo Sapien, Homo Sapiens have a few new features Neanderthal may not. Basically this is an unsafe assignment and you need to Cast it up, effectively telling the compiler “Shut up I know what I am doing”. It is perfectly safe to cast down to a lower leveled class which is why you will never get a warning about it.

thanks also is there a better way of doing this?
Atm when i start my game crashes because the WeaponBase reference is a nullptr but not sure how i would call it. i have tried Character->WeaponBase = this; but that didnt do anything its still nullptr

I need a reference for my weapon class to get the first person camera which i have done here



//WeaponBase Class
////.h
	class AFpsVsBotsCharacter* Character;

//.cpp
	Character->WeaponBase = this;

//I need the reference for this
	FVector StartTrace = Character->GetFirstPersonCameraComponent()->GetForwardVector();


I then add it into my begin play



//Character class
	WeaponBase->Character = this;
	WeaponBase->Character = Cast<AFpsVsBotsCharacter>(Instigator);

///.h 
	class AWeaponBase* WeaponBase;


I do this so i can use it for my onfire method



void AFpsVsBotsCharacter::OnFire() {
	if (WeaponBase == nullptr) { return; }
	WeaponBase->WeaponFire();
}


This will prevent null references for WeaponBase.



if(WeaponBase)
{
    WeaponBase->Character = Cast<AFpsVsBotsCharacter>(Instigator);    
}


Im not sure how to set my “WeaponBase” variable so it isn’t a nullptr

Basically this is what i have got

I created a “character” variable in my weapon base class to get the first person camera



	class AFpsVsBotsCharacter* Character;


Then i needed to get the character variable to set it, so i made a variable called “WeaponBase” in my character class



	class AWeaponBase* WeaponBase;


I used “WeaponBase” to get my character variable from the weapon base class then i set the “Character” variable in my character class



/////BeginPlay//////

	if (WeaponBase == nullptr) { return; }
	WeaponBase->Character = this;
	WeaponBase->Character = Cast<AFpsVsBotsCharacter>(Instigator);


The problem is that “WeaponBase” is a nullptr because it isn’t set but i dont know how to set it. Any ideas on how i will set the “WeaponBase” so it isn’t nullptr

You’ll need to spawn and then assign “WeaponBase” to what was spawned, to get a reference; assuming that WeaponBase is an Actor.

Thanks i got it to work also is this a bug or have i done something wrong?

My line trace wont shoot in the air, it will only shoot if im looking at something like a wall/floor etc

GIF - https:///184eaf03fce5e9ee0268e693aa045c8a

As you can see from the gif the line trace doesn’t shoot when looking in the air



void AWeapon_Instants::WeaponFire() {
	Super::WeaponFire();

	if (Character == nullptr) { return; }
	FVector StartTrace = Character->GetFirstPersonCameraComponent()->GetSocketLocation("");
	FVector ForwardTrace = Character->GetFirstPersonCameraComponent()->GetForwardVector();
	FVector EndTrace = StartTrace + ForwardTrace * WeaponInstants.MaxBulletRange;
	FCollisionQueryParams Params;

	UWorld* const World = GetWorld();
	check(World);

	if (World->LineTraceSingleByChannel(Hit, StartTrace, EndTrace, ECC_Visibility, Params)) {
		DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor(255, 0, 0), false, 2.0f, 0.0, 2.0f);
	}
}


The line trace isn’t hitting anything because its based on “WeaponInstants.MaxBulletRange”. Or, the mesh that is of the sky dome doesn’t receive trace hits.

You can do something like:



if (World->LineTraceSingleByChannel(Hit, StartTrace, EndTrace, ECC_Visibility, Params))
{
        // Use the FHitResult "ImpactPoint" as the end, as that is what was hit
	DrawDebugLine(GetWorld(), StartTrace, Hit.ImpactPoint, FColor(255, 0, 0), false, 2.0f, 0.0, 2.0f);
}
else
{
       // The trace failed, use the literal end point of the maximum bullet range as the target
       DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor(255, 0, 0), false, 2.0f, 0.0, 2.0f);
}