[UE5] GetSocketLocation returning origin or local position instead of world position

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???

1 Like

Try using UStaticMeshComponent::GetSocketTransform instead, as you can specify a world space with it.

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();
	}
1 Like

I think I’m going to need to see an expanded section of code, perhaps the function you’ve written this in - This doesn’t make any sense.

/// <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
}

which is called on fire input

PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &APlayerCharacter::Fire);
1 Like

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.

From what I could recall it was the same error and I tried a few times with the same result. I can try again though to check.

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

[2022.02.01-17.26.34:067][756]LogWindows: Error: === Critical error: ===
[2022.02.01-17.26.34:067][756]LogWindows: Error:
[2022.02.01-17.26.34:067][756]LogWindows: Error: Fatal error!
[2022.02.01-17.26.34:067][756]LogWindows: Error:
[2022.02.01-17.26.34:067][756]LogWindows: Error: Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000000
[2022.02.01-17.26.34:067][756]LogWindows: Error:
[2022.02.01-17.26.34:067][756]LogWindows: Error: [Callstack] 0x00007ff87441594d UnrealEditor-GunslingerUE5.dll!ABaseWeapon::Fire() [C:[redacted]\GunslingerUnreal\Source\GunslingerUE5\Private\BaseWeapon.cpp:80]
[2022.02.01-17.26.34:067][756]LogWindows: Error: [Callstack] 0x00007ff87441bd22 UnrealEditor-GunslingerUE5.dll!APlayerCharacter::Fire() [C:[Redacted]\GunslingerUnreal\Source\GunslingerUE5\PlayerCharacter.cpp:143]
[2022.02.01-17.26.34:067][756]LogWindows: Error: [Callstack] 0x00007ff887c3ae2a UnrealEditor-Engine.dll!UnknownFunction []
[2022.02.01-17.26.34:067][756]LogWindows: Error: [Callstack] 0x00007ff887c6c10e UnrealEditor-Engine.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff8876e6f6b UnrealEditor-Engine.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff8876fbd61 UnrealEditor-Engine.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff8876e5249 UnrealEditor-Engine.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff8876fac0d UnrealEditor-Engine.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff886506826 UnrealEditor-Engine.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff887b0a11e UnrealEditor-Engine.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff887b1189f UnrealEditor-Engine.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff8bacc15ea UnrealEditor-Core.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff8bacc1a6e UnrealEditor-Core.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff8bacd3d43 UnrealEditor-Core.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff887b2fd11 UnrealEditor-Engine.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff887b3647d UnrealEditor-Engine.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff886f2895f UnrealEditor-Engine.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff886f32447 UnrealEditor-Engine.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff8a2fce9c0 UnrealEditor-UnrealEd.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff8a38ecc16 UnrealEditor-UnrealEd.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff732f48930 UnrealEditor.exe!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff732f60b2c UnrealEditor.exe!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff732f60c0a UnrealEditor.exe!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff732f7573d UnrealEditor.exe!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff732f786ba UnrealEditor.exe!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff90c767034 KERNEL32.DLL!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error: [Callstack] 0x00007ff90cfc2651 ntdll.dll!UnknownFunction []
[2022.02.01-17.26.34:069][756]LogWindows: Error:
[2022.02.01-17.26.34:093][756]LogThreadingWindows: Error: Runnable thread StallDetectorThread crashed.
[2022.02.01-17.26.34:274][756]LogWindows: Error: HandleError re-entered.
[2022.02.01-17.26.34:274][756]LogWindows: FPlatformMisc::RequestExit(1)
[2022.02.01-17.26.34:274][756]LogCore: Engine exit requested (reason: Win RequestExit)
[2022.02.01-17.26.34:497][756]Log file closed, 02/01/22 12:26:34

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.