I am trying to spawn a bullet at the world location of a socket, but the bullet is spawning at the map origin. I have always tick pose and refresh bones on both the player and the gun. I’m not sure what else I am missing.
if (m_MeshRef != nullptr && m_MeshRef->DoesSocketExist("BulletSocket"))//just in case
{
spawnLocation = m_MeshRef->GetSocketLocation("BulletSocket");
}
else
spawnLocation = GetActorLocation(); // this does work but spawns at the blueprint location, this line is not reached because the above if statement is true
I know the bullet spawns because I am currently spawning the bullet from the gun’s location (handle) while I do other things and try to figure out why my socket is not working. I did attempt to check if GetSocketLocation does the same in blueprints by printing the location on screen but all I was able to do was crash unreal. I am a beginner with unreal since I am a new user from C# programming in unity.
Also forum bug, 5-0EA does not work as a version tag???
I did try using this function but for some reason using the GetLocation() from the FTransform causes unreal to crash with Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000010 ?
FTransform spawnTransform = FTransform();
if (m_MeshRef != nullptr && m_MeshRef->DoesSocketExist("BulletSocket")) //this does not work?????
{
spawnTransform = m_MeshRef->GetSocketTransform("BulletSocket", RTS_World);
//spawnLocation = m_MeshRef->GetSocketLocation("BulletSocket");
spawnLocation = spawnTransform.GetLocation(); //crashes here
}
else
spawnLocation = GetActorLocation();
Am I missing something important?
and then another crash at the previous line when I tried adding isValid: Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000000
FTransform spawnTransform = FTransform();
if (m_MeshRef != nullptr && m_MeshRef->DoesSocketExist("BulletSocket")) //this does not work?????
{
spawnTransform = m_MeshRef->GetSocketTransform("BulletSocket", RTS_World);//crashed here
if(spawnTransform.IsValid())
spawnLocation = spawnTransform.GetLocation();
}
/// <summary>
/// Shoot weapon
/// </summary>
bool ABaseWeapon::Fire(FVector AimDirection)
{
//get bullet from bullet pool
ABaseBullet* bullet = GetBulletFromPool();
if (bullet == nullptr)
{
//error no bullets
return false;
}
bullet->SetBulletInfo(m_BulletInfo);
//give it a vector and rotate only the X so that it only moves on the YZ plane
FVector spawnLocation = GetActorLocation();
FRotator spawnRotation;
FTransform spawnTransform = FTransform();
if (m_MeshRef != nullptr && m_MeshRef->DoesSocketExist("BulletSocket"))
{
spawnTransform = m_MeshRef->GetSocketTransform("BulletSocket", RTS_World);
//spawnLocation = m_MeshRef->GetSocketLocation("BulletSocket");
if(spawnTransform.IsValid())
spawnLocation = spawnTransform.GetLocation();
}
spawnRotation = AimDirection.Rotation();
//tell gameinstance to place a bullet at socket location
bullet->FireBullet(spawnLocation, spawnRotation);
return true;
}
which is called by player
void APlayerCharacter::Fire()
{
if (!CanFire)//player may be in fire animation
return;
CanFire = false;//player is firing
GetAimFrame();//get firing angle
//shoot bullet from weapon
m_SpawnedWeapon->Fire(AimVector());
//animate player firing
}
This is genuinely bizarre - I have no idea how this wouldn’t work - You said when you tried to use GetSocketLocation in blueprints, it crashed?
Do you have the crash log for that?
GetSocketLocation returns a world-space Location by default.
However the crash seems to indicate this is an issue with something else. Make sure you are running in DebugGame Editor in Visual Studio, NOT Development Editor or you won’t break in the correct place and debug information will be useless.
For some reason GetSocketLocation is not returning the expected location. I assume the socket that I put inside the gun skeleton does not detatch? At least I don’t touch it in code after I added it. when the player walks around with the gun, the bullets still come from the origin and do not change position at all.
I will check the Visual Studio thing and see if I can get better debug information.
Well it was on development editor so I switched it and tried again, but I got the same crash Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000000
I found the crash log though so here is the full error
Well I solved it. The issue was the pointer to the skeletal mesh was incorrect. I thought that because the is valid and has socket if statement was working that the skeletal mesh was correct.
Turns out I was getting “this” as skeletal and not the skeletal component itself, but for some reason that is valid and has the socket? very confusing.