C++ socket error

Hi, I’ve another problem with the “Learning C++” book…



void AWardCharacterCharacter::PostInitializeComponents()
{
	Super::PostInitializeComponents();
	// instantiate the melee weapon if a bp was selected
	if (BPMeleeWeapon)
	{
		MeleeWeapon = GetWorld()->SpawnActor<AWardSword>(BPMeleeWeapon, FVector(), FRotator());
		if (MeleeWeapon)
		{
			const USkeletalMeshSocket* socket = GetMesh()->GetSocketByName("RightHandSocket");
			// be sure to use correct
			// socket name!
			socket->AttachActor(MeleeWeapon, GetMesh());
		}
	}
}


the error is at “socket->AttachActor…”

How can I resolve??

Remove the ‘const’. It means that ‘socket’ cannot be modified, but AttachActor is non-const and wants to modify ‘socket’.

Just tryied…it says:

C2440 ‘initialization’ : impossible to convert from ‘const USkeletalMeshSocket *’ to ‘USkeletalMeshSocket *’

how can I resolve?

Ah sorry, I didn’t take the time to read what you were actually trying to do. Yes, GetSocketByName() will return a const socket, so you can’t use that.

I think what you’re trying to do is this:



MeleeWeapon->AttachToActor(this, FAttachmentTransformRules::KeepRelativeTransform, "RightHandSocket");


Well…I’m in the character class and I want attach the actor “MeleeWeapon” to the character socket, but I’ve errors!!

now it tell me:
C2039 ‘AttachToActor’: isn’t a member of ‘AActor’
C2653 ‘FAttachmentTransformRules’ isn’t the name of a class or of a space of names

Ah, it looks like AttachToActor() is new in 4.12
Please try K2_AttachToActor() instead, for example:



MeleeWeapon->K2_AttachToActor(this, "RightHandSocket", EAttachmentRule::KeepRelative, EAttachmentRule::KeepRelative, EAttachmentRule::KeepRelative, true);


Well…I’m using the 4.10.4 version…It doesn’t compile :cry:
Nothing yet…

errors:
C2653: ‘EAttachmentRule’ isn’t the name of a class or of a space of names
C2039: ‘K2_AttachToActor’: isn’t a member of ‘AActor’
C2065: ‘KeepRelative’: identificator not declared

what can I do now? Isn’t the method K2_AttachRootComponentTo () ???

Ok, so you’re using an even older Engine version. It looks like this API has changed a lot recently. Please always include the Engine version in your post.

You can try K2_AttachRootComponentToActor

ok, I’m trying this:

error: C2664 Impossible to convert argument 1 from ‘AWardCharacterCharacter * const’ to ‘USceneComponent *’

So, what should I do?

Yeeeeee!! I’ve done it!!

I’ve used the AttachRootComponentToActor!!

Just so you know, this has changed again in 4.12. You need to use ‘AttachToComponent()’ in future versions.