x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

Product Version: UE 4.10
Tags:
more ▼

asked Jan 31 '16 at 07:06 PM in C++ Programming

avatar image

sulibarri17
71 1 3 7

avatar image sulibarri17 Jan 31 '16 at 07:52 PM

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.

avatar image sulibarri17 Jan 31 '16 at 09:59 PM

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.

alt text

question.png (147.0 kB)
avatar image Shadowriver Feb 01 '16 at 02:12 AM

Can you paste your code?

avatar image sulibarri17 Feb 01 '16 at 07:12 AM
 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;
             }
(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

more ▼

answered Feb 01 '16 at 07:14 AM

avatar image

sulibarri17
71 1 3 7

avatar image Shadowriver Feb 01 '16 at 02:39 PM

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

avatar image sulibarri17 Feb 01 '16 at 04:17 PM

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

avatar image SkaveRat Oct 10 '17 at 01:45 AM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question