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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Why am I receiving an access violation error when attaching a weapon to player?

Hello guys!

I have this code to attach a weapon to my main character:

 AHavenWeapon::AHavenWeapon(const class FPostConstructInitializeProperties& PCIP)
     : Super(PCIP)
     weaponMesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this,TEXT("weaponMesh"));
     this->RootComponent = weaponMesh;
 void AHavenWeapon::BeginPlay()
     ULocalPlayer *localPlayer = GEngine->GetGamePlayer(GetWorld(), 0);
     AHavenCharacter *character = (AHavenCharacter *)localPlayer;
     this->RootComponent->AttachParent = character->Mesh;

The problem is that as soon as I hit play, Visual Studio comes up and tells me there is an access violation somewhere in the attachment code. Does anyone know why this doesnt work?

Product Version: Not Selected
more ▼

asked May 16 '14 at 04:09 PM in C++ Programming

avatar image

1 3 4 5

avatar image AnxGotta May 16 '14 at 06:27 PM

Well, could you fix your code to check for null values and then see if it still happens?

 ULocalPlayer *localPlayer = GEngine->GetGamePlayer(GetWorld(), 0);
     AHavenCharacter *character = (AHavenCharacter *)localPlayer;
         this->RootComponent->AttachParent = character->Mesh;

Edit: Also, you might want to try some of the internal methods for attaching objects to each other as they do many checks to ensure proper attachment. I think this "brute force" assignment might be a problem.


 weaponMesh->AttachTo(..., ..., ...);

or something of the like and see if that works too.

avatar image Kulomin May 16 '14 at 06:58 PM

I checked the null-values and they are all valid. I also tried the method AttachTo, which gives me an error as well, just in another internal method(its also an access violation)

avatar image AnxGotta May 16 '14 at 07:13 PM

Please post the code from the error.

avatar image Kulomin May 16 '14 at 07:23 PM

The breakpoint happens in the AttachTo-method of the USceneComponent in SceneComponent.cpp. The code is: void USceneComponent::AttachTo(class USceneComponent* Parent, FName InSocketName, EAttachLocation::Type AttachType /= EAttachLocation::KeepRelativeOffset /) { if(Parent != NULL) { if(Parent == this) { FMessageLog("PIE").Warning(FText::Format(LOCTEXT("AttachToSelfWarning", "AttachTo: '{0}' cannot be attached to itself. Aborting."), FText::FromString(GetPathName()))); return; }

         if(Parent->IsAttachedTo(this))  //here visual studio breaks with an access violation
             FMessageLog("PIE").Warning(FText::Format(LOCTEXT("AttachCycleWarning", "AttachTo: '{0}' already attached to '{1}', would form cycle. Aborting."), 
 //here would be much other code...

Here is my updated code after trying some things: enter code here AHavenWeapon::AHavenWeapon(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP) { weaponMesh = PCIP.CreateDefaultSubobject(this,TEXT("weaponMesh")); this->RootComponent = weaponMesh;

 void AHavenWeapon::BeginPlay()
     ULocalPlayer *localPlayer = GEngine->GetGamePlayer(GetWorld(), 0);
     AHavenCharacter *character = (AHavenCharacter *)localPlayer;
     //UE_LOG(LogTemp, Error, TEXT("BEGIN PLAYER C++"));
     //weaponMesh->AttachParent = character->Mesh;
     //weaponMesh->AttachSocketName = FName(TEXT("socket_hand_r"));

Looking forward to your answer :)

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

2 answers: sort voted first

Ok... I'm a bit stumped but I have a hunch.

You are getting the error when trying to reference the AHavenCharacter or Parent variable in the AttachTo method. Also, you are getting the error in the old code when you try to reference the character->mesh. I would think that there is some issue reading the character variable... in other words, trying to access the AHavenCharacter. I have never seen attaching done like this before so again... I'm a bit stumped.

I do have a suggestion though... and equipping things the way the developers of the engine do in their samples might be the way to go. Read on if you are interested in a test that might fix this issue and also help you learn a better way to equip items and weapons at the start of the game.

I'm sorry I don't know why it's currently causing the error for you. Another method of implementing the same thing is not a great fix but it might help =/

I think proper implementation of weapon equipping is needed here. If you have no desire to use this method of equipping items just ignore the rest of this post... but this is the way it's done in all the examples and in 2 of my current projects. Not only is it extendable, but it works perfectly.

You are trying to start the game and have the AHavenWeapon attach itself to a AHavenCharacter you get by querying the UWorld and grabbing a reference to the first ULocalPlayer in the list of GamePlayers and then casting it to AHavenCharacter.

I know nothing of your implementation or other classes but I would suggest this...

Control your weapon attaching from your AHavenCharacter class...

To do this, in your AHavenCharacter class you create a variable that you will assign your weapon Blueprint to in the editor. Then, in your AHavenCharacter class you spawn that weapon from the class variable and assign it to a CurrentWeapon pointer variable. You then call a function in that CurrentWeapon to attach that weapon to yourself, the current AHavenCharacter. This seems to be the default way to equip weapons and it always seems to work.


In your AHavenCharacter class header file, create a variable for the weapon class and the CurrentWeapon in the header file.

 //  weapon class
 UPROPERTY(EditDefaultsOnly, Category = Inventory)
 class AHavenWeapon WeaponClass;

 // weapon
 class AHavenWeapon* CurrentWeapon;

In your AHavenWeapon class create a method for attaching and call it whatever you want... I'll call it TestAttachWeapon.

 void AHavenWeapon::TestAttachWeapon(class AHavenCharacter* MyPawn){
     if(!MyPawn) return;

So, now back to your AHavenCharacterClass. In the BeginPlay method...

 void AHavenCharacter::BeginPlay(){
     // if WeaponClass not null
         // Create some parameters for spawning
         FActorSpawnParameters SpawnInfo;
     SpawnInfo.bNoCollisionFail = true;
         // Spawn the weapon from the class
         CurrentWeapon = GetWorld()->SpawnActor<AHavenWeapon>(WeaponClass, SpawnInfo);
         // if CurrentWeapon not null [spawn successful]
             // Attach it to myself

Again, to do this you will need a Blueprint of your AHavenCharacter and optionally your AHavenWeapon. You just create those Blueprints in the editor from the classes.

In the editor, you simply set the AHavenWeapon WeaponClass variable in the Default values of the Character blueprint.

Picture of Defaults in BP

Now, I have mine set up for a TArray of Weapons. You will have the "Inventory" tab, but you will only see one spot with a drop down to put your AHavenWeapon.

So what you do is select your AHavenWeapon class or AHavenWeapon Blueprint there and it will be used to spawn one in the game and attach it to your character.

attachweaponbp.png (70.2 kB)
more ▼

answered May 16 '14 at 09:08 PM

avatar image

1.6k 61 45 237

avatar image Kulomin May 16 '14 at 11:14 PM

Thank you very much! Got it working now. I still dont know why that my previous code did not work^^

avatar image AnxGotta May 16 '14 at 11:38 PM

I'm not sure either, I'm glad this worked for you though!

avatar image Snipe3000 Aug 02 '14 at 04:30 AM

Hmmm, this method doesnt seem to work for me. I'm having serious trouble trying to create a Weapon class, unless thats a type and you meant function. Im getting an error for trying to include the .generated.h file for the weapons class I created. I think I found a typo: class AHavenWeapon WeaponClass; should be class AHavenWeapon* WeaponClass;

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

I'm not sure, but I think that an Actor must be attached to a USceneComponent, not a Mesh. Try creating a mount point using a USceneComponent in your character and then attach to it.

If it does not work, perhaps you could try using the AttachRootComponentTo (https://docs.unrealengine.com/latest/INT/API/RuntimeModules/Engine/GameFramework/AActor/AttachRootComponentTo/index.html). I'm using it and it works for me. It even has a EAttachLocation::Type parameter to define the offset after attached.

more ▼

answered May 16 '14 at 07:40 PM

avatar image

21 2 6 5

avatar image Kulomin May 16 '14 at 08:51 PM

character->Mesh is a USkeletalMeshComponent which inherits from USceneComponent, so shouldnt be a problem there. I get a problem no matter what function i call. Could it be that somehow the things arent initialized in BeginPlay()?

avatar image Caspila May 16 '14 at 09:06 PM

That's right about the inheritance, my bad.

I removed my previous comment because it made no sense after I read your problem once again. I don't think I can help you on this one.

(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