GetActorLocation() fed to SpawnActor() not working

Hello all,

So I’m trying to spawn a weapon actor when my character drops a weapon, so it looks like the weapon is being dropped. When I call SpawnActor and feed it a location like FVector(0,0,0), it spawns the actor at the position I give it. But when I give SpawnActor something like GetActorLocation(), it doesn’t appear to spawn the actor. I’m calling this from within my character’s class.

Does SpawnActor() not accept GetActorLocation() as a parameter? Does my character have to have a particular root component selected? I’ve even tried feeding SpawnActor the following: GetMesh()->GetComponentLocation() and it still doesn’t seem to spawn the actor.

Thanks for your help.

I should also mention that I tried spawning the actor with FVector(0,0,0) as the location parameter and on the next line used SetActorLocation() using GetActorLocation as the location and it still didn’t spawn. So I think the problem may be my use of GetActorLocation(). GetActorLocation() works when I call it from an enemy character class but it doesn’t seem to work from within the character class, at least when I feed it to SpawnActor or SetActorLocation.

Also I don’t know if it’s not spawning or if it’s spawning and immediately being destroyed or what.

Another thing I might add is that from blueprints, when I spawn an actor triggered from a key press (I chose F) it works, but with a blueprint implementable event called from c++, nothing happens.

Can you paste your code?

if (World)
{
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;

				FVector SpawnLocation = GetActorLocation();
				FRotator SpawnRotation(0, 0, 0);

				AMeleeWeapon* thrownWeapon = World->SpawnActor<AMeleeWeapon>(BPMeleeWeapon, SpawnLocation, SpawnRotation, SpawnParams);
				thrownWeapon->PickupWeaponState = EWeaponStateMelee::THROWN;
			}

So I got it to work. The thing is this. For each weapon in my game, I have two different classes and hence two different blueprints. One is the Pickup version, and it hovers in the world waiting for the character to pick it up. Then there is the Weapon version, which is what is spawned and attached to a socket on the character. The Pickup class contains a UClass* pointer to the Weapon blueprint. When I try to spawn the pickup version, it doesn’t accept character->GetActorLocation() as a location, but when I spawn the Weapon version, it works just fine. I don’t know if this is a bug, or what.

GetActorLocation won’t work if there is no root component in actor

Ah that explains it! My Pickup class has no root component, but my Weapon class does. Thank you for clarifying!

Another note: you also have to set the RootComponent in the constructor. During Runtime/BeginPlay doesn’t work