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"

Tarray Break

 void AHermesCharacter::PickUp(TSubclassOf<class AWeapon> spawn)
 {
     AWeapon* point = GetWorld()->SpawnActor<AWeapon>(spawn, this->GetActorLocation(), this->GetActorRotation());
     point->SetVisibility(false);
     point->Initialize(Mesh1P->GetAnimInstance());
     Inventory.Add(point);
     point->AttachRootComponentTo(Mesh1P, FName(TEXT("WeaponPoint")), EAttachLocation::SnapToTarget);
     if (CurrentWeapon == NULL)
     {
         CurrentWeapon = point;
         CurrentWeapon->WearWeapon(true);
     }
 }
 
 
 
 void AHermesCharacter::NextWeapon()
 {    
     Inventory[0]->WearWeapon(true);
 }


Inventory[0] is breaking my game what am i doing wrong :X? Pointers gonna kill me ;d

Product Version: Not Selected
Tags:
more ▼

asked Aug 28 '14 at 01:39 PM in C++ Programming

avatar image

Virusik
68 12 16 17

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

3 answers: sort voted first

If Inventory[0] breaks your game, it's probably a nullptr. You're calling the function NextWeapon while the inventory is empty. This is most likely a problem caused somewhere else in your code.

more ▼

answered Aug 28 '14 at 03:34 PM

avatar image

Shammah
380 23 18 144

avatar image Virusik Aug 29 '14 at 05:59 AM

It's not empty i checked by num sie and is valid.

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

First, when accessing an array you should always check whether the index is actually valid. Your NextWeapon() function will fail if the array is empty (i.e. no weapon was picked up, but the player is trying to switch weapons). You can use the Inventory.IsValidIndex(..) function, or you can check whether Inventory.Num() is greater than the index you're trying to access.

Second, since your array is holding actors, you need to make sure that the Inventory field is a UProperty. Otherwise the values added to this array will be garbage collected (meaning: destroyed), because the Engine has no way of detecting that you are still planning to use them. You can make the Inventory array a UProperty by marking it up with the UPROPERTY() macro in your class declaration for AHermesCharacter.

You should also include the actual error message in your questions. That will make it easier for us to determine what exactly is going wrong, thanks!

more ▼

answered Aug 28 '14 at 03:37 PM

avatar image Virusik Aug 29 '14 at 05:40 AM

I know i should but that was just a test code i stared writing line by line and checking if everything work, There is no null pointer exepction index is valid and when it comes too that variable it just crashes My variable declarations are:

 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Gameplay)
     TArray<class AWeapon*> Inventory;



 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = Gameplay)
 class AWeapon* CurrentWeapon;

 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay)
 UAnimInstance* AnimInstance;

wear weapon:

 void AWeapon::WearWeapon(bool wear)
 {
     if (PickUpAnimation != NULL)
     {
         // Get the animation object for the arms mesh
         if (AnimInstance != NULL)
         {
             AnimInstance->Montage_Play(PickUpAnimation, 1.f);
         }
     }
     this->SetVisibility(wear);
 }

animinstance initialization:

 void AWeapon::Initialize(UAnimInstance* AnimInstance)
 {
     this->AnimInstance = AnimInstance;
 }


Error code : First-chance exception at 0x000007FB8918B1D0 (UE4Editor-Hermes-3607.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x0000000000000358.

If there is a handler for this exception, the program may be safely continued.

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

Solved, The reason Of Crash was that array in bp had 2 empty elements god bless me.

more ▼

answered Aug 29 '14 at 09:04 AM

avatar image

Virusik
68 12 16 17

(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