How can I attach a blueprint to player during runtime?

UPDATE!

I solved it with what I see as a work-around (or maybe it’s intended?) below. See the answer.


My first time posting here, so if I break etiquette in my question or if more information is needed please let me know in a comment so that I can improve my questions in the future.

  • The goal of this is to have the sword
    attach to the player, and begin
    moving along with it, as if you have
    picked it up.

I currently have a StaticMesh rendered in the world through the editor, through a blueprint. (Just a basic sword model I found online.) I am casting a ray out from my character and when it hits this sword, I am attempting to have the sword snap and attach to the player. However, it doesn’t seem to be working.

Everything works fine, the raycast, the detection of the raycast smacking the sword, and even if I use a Destroy() on the sword with the raycast, it does great! Just not this attachment situation.

Also something weird: If I attempt to attach the PLAYER to the SWORD… it works. Any ideas? I will share my full code below that I think is relevant (the ‘OnFire’ function from my C++ that happens when you left click.)

void ATheCharacter::OnFire()
{
	FHitResult hitresult(ForceInit);

	//Raycast, feeding the results to the memory location of 'hitresult'
	Raycast(&hitresult);	//player
	obj = hitresult.GetActor();

	//Make sure we are hitting an actor before going any further to avoid null
	if (hitresult.GetActor())
	{
		//Display on screen to make sure the right things are being used
		GEngine->AddOnScreenDebugMessage(-10, 1.f, FColor::Yellow, obj->GetName());

		//Keep from doing work on unintended actors
		if (hitresult.GetActor()->ActorHasTag("NotWorld"))
		{
			//obj->Destroy(); //WORKING!
			obj->AttachRootComponentTo(this->GetRootComponent()); //NOT WORKING!
		}
	}
	else
	{
		GEngine->AddOnScreenDebugMessage(-5, 1.f, FColor::Red, "No Actor Detected");
	}
}

Below, I have also added my Raycast function, in case that is relevant to what’s making this not work.

void ATheCharacter::Raycast(FHitResult* hit)
{
	//If we aren't possessing something, then stop, because we can't get a viewpoint or raycast from null
	if (Controller == NULL) return;

	//Camera position, Camera rotation, Raycast start position, Raycast end position
	FVector		CameraLoc;
	FRotator	CameraRot;
	FVector		StartPos;
	FVector		EndPos;

	//EndPos and StartPos for the trace, just ahead of the actor/camera
	EndPos.Set(500.0f, 500.0f, 500.0f);
	StartPos.Set(200.0f, 200.0f, 200.0f);

	//Load the controlled actor's forward direction and rotation into CameraLoc and CameraRot
	GetActorEyesViewPoint(CameraLoc, CameraRot);

	//Setting up the start and end of the raycast
	FVector Start = CameraLoc + (CameraRot.Vector() * StartPos);
	FVector End = CameraLoc + (CameraRot.Vector() * EndPos);

	//Do the raycast
	GetWorld()->LineTraceSingle(*hit, Start, End, ECC_MAX, 0);
	DrawDebugLine(GetWorld(), Start, End, FColor(255, 0, 0), true, 10, 0, 3);
}

Could me->GetRootComponent() be null?

@Dieselhead No, because a test of “GEngine->AddOnScreenDebugMessage(-10, 30.f, FColor::Yellow, me->GetRootComponent()->GetName());” outputs “CollisionCylinder” as the name.

UPDATE!

I have discovered that if I turn off “Simulate Physics” for the sword, then my code works just fine, and it snaps and attaches to the actor. Unless someone else knows a more efficient, or proper way to do this, I think my only solution right now is to:

1. Raycast, and detect the sword.

2. On detection, remove physics simulation from the sword.

3. After physics have been turned off, THEN attach it to my character actor.

I’m not sure what the best way to go about this is, so I assume I will mark this as solved by myself, if I can figure out how (new to these forums here.) If anyone comes up with a better solution than the one I have above, or even if this is just the working intended for the Engine, let me know as I’m interested in learning the ins-and-outs of Unreal Engine 4 fully.

Thanks guys!

I think that accepting your own answer as solution is perfectly fine as long as the problem is well defined and solution really is a solution to the issue.

Other people in the future will use search function if they run into similar issue, and also quickly get solution.

@Ardivaba That’s why I did it. I hope in the future this can help someone with the same problem figure it out quickly without the headache I had!