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"

c++ GetOwner and GetActorEyesViewPoint on a weapon class

Hi all!

I got a problem with setting GetActorEyesViewPoint in my SGrenadeLauncher class.

In my SCharacter class I do the following:

 if (TheGrenadeLauncherWeapon)
         TheSpawnedWeapon = GetWorld()->SpawnActor<AActor>(TheGrenadeLauncherWeapon, FVector(0), FRotator(0));
         const USkeletalMeshSocket* WeaponSocket = GetMesh()->GetSocketByName("WeaponSocket");
         WeaponSocket->AttachActor(TheSpawnedWeapon, GetMesh());

In my SGrenadeLauncher.cpp I do this:

 void ASGrenadeLauncher::FireWeapon()
     AActor* MyOwner = GetOwner();
     MyOwner->GetActorEyesViewPoint(EyeLocation, EyeRotation); // On this line the editor crashes

So the editor crashes on GetActorEyesViewPoint of MyOwner. So something is wrong with MyOwner.

I think either the MyOwner does not find the correct SCharacter Actor, or the MyOwner should be a ACharacter or APawn or something for GetActorEyesViewPoint to work.

In SCharacter.cpp I attached the weapon to the socket. So I would think if I do GetOwner in SGrenadeLauncher.cpp that it would return me the SCharacter as an Actor. So any idea why my code doesn't work?

Thanks in advance!

Product Version: UE 4.20
more ▼

asked Oct 15 '18 at 08:50 AM in C++ Programming

avatar image

Rebel Camp Games
20 4 7 11

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

You are trying to access a null pointer on line 4 of the cpp, that is why it is crashing. GetOwner() does not contain anything because you have not set the value of Owner.

So when your character picks up or attaches the weapon, make sure to set the owner to your character.

Also protect that pointer. Right before you try to access MyOwner, make sure you put an if statement and check that MyOwner exists first. That will prevent the editor from crashing if for some reason MyOwner is null.

Also once you have the value of owner set, you can move line 3 to begin play and access MyOwner anywhere you need, and that way you are not setting it every time you shoot. But still make sure to protect the pointer at the beginning of fire.

more ▼

answered Oct 15 '18 at 11:08 AM

avatar image

Steve SybrSyn
1.5k 4 5 10

avatar image Rebel Camp Games Oct 15 '18 at 11:23 AM

Thanks! works like a charm!

avatar image Steve SybrSyn Oct 15 '18 at 11:49 AM

You're welcome! Glad I could help.

(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