Why it say array out of bounds in BP?

// Fill out your copyright notice in the Description page of Project Settings.


#include "Panal.h"


// Sets default values
APanal::APanal()
{
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;

	Root = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Root"));
	Zero = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Zero"));
	One = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("One"));
	Two = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Two"));
	Three = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Three"));
	Four = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Four"));
	Five = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Five"));
	Six = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Six"));
	Seven = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Seven"));
	Eight = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Eight"));



	RootComponent = Root;

	Zero->SetupAttachment(Root);
	One->SetupAttachment(Root);
	Two->SetupAttachment(Root);
	Three->SetupAttachment(Root);
	Four->SetupAttachment(Root);
	Five->SetupAttachment(Root);
	Six->SetupAttachment(Root);
	Seven->SetupAttachment(Root);
	Eight->SetupAttachment(Root);
	Zero->Rename(TEXT("apple"));

	
	ConstructorHelpers::FObjectFinder<UStaticMesh>MeshAsset(TEXT("/Script/Engine.StaticMesh'/Game/Mesh/BinaryTree/ControlPanal/domofon__1__B1.domofon__1__B1'"));
	
	Zero->SetStaticMesh(MeshAsset.Object);
	
	Zero->SetNotifyRigidBodyCollision(true);
	Zero->SetCollisionProfileName(TEXT("BlockAll"));  // Устанавливаем профиль коллизии
	Zero->SetGenerateOverlapEvents(true);  
	
}

// Called when the game starts or when spawned
void APanal::BeginPlay()
{
	Super::BeginPlay();
	//RightAnswers.Add("apple");
}



void APanal::Interact(UPrimitiveComponent* GetComponent, ABaseFirstPersonCharacter* SelfCharacter)
{
	if (GetComponent == Zero)
	{
		if (RightAnswers[AnswerN] == Zero->GetMaterials()[0]->GetName())
		 {
			RAnswers-=-1;
		 }
	}
	if (GetComponent == One)
	{
		UE_LOG(LogTemp, Warning, TEXT("BananaI"));
	}

	if (AnswerN==3)
	{
		if (RAnswers==3)
		{
			UE_LOG(LogTemp, Warning, TEXT("You win"));
		}
		else
		{
			UE_LOG(LogTemp, Warning, TEXT("You lose"));
		}
	}
}

void APanal::PanalI(FString a, FString b, FString c)
{
	//RightAnswers.Empty();
	RightAnswers.Add(a);
	RightAnswers.Add(b);
	IBinaryFruit::PanalI(a, b, c);
}



Wtf

So I am guessing that it is in your APanal::Interact() that this error is being thrown?

where is AnswerN being set? is this a public member or inherited member?

try logging out the Indexes you are attempting to output:

UE_LOG(LogTemp, Warning, TEXT("RightAnswers.Num: %s, AnswerN: %s"), *FString::FromInt(RightAnswers.Num()), *FString::FromInt(AnswerN));

if the AnswerN is greater or equal to the .Num() then it will be an out of bounds exception.
if there is still a crash the debug statement should flush to the Log file before the engine actually crashes. though you can for example in Visual studio try attaching the Visual Studio Debugger see if the crash still triggers, and poke around at the local values being worked with.

you might want to add a check on the

if ( GetComponent == Zero )
// to become
if (GetComponent == Zero && AnswerN < RightAnswers.Num())

to enforce that AnswerN should not cause an out of bounds.
the other possibility is that there is no Material in the Materials array on your Zero member (this should be a Null Reference or Null Access Violation instead of an out of bounds though)

if neither of these help then could your provide more details, like text from the Crash report (just the top lines of the call stack will point out most issues)

I’m guessing these are your definitions in the header

	UPROPERTY();
	TMap<int,FString> RightAnswers;

	UPROPERTY();
	int AnswerN;

	UPROPERTY();
	int RAnswers;

Before checking a TMap directly by it’s key, you first need to see if the map contains the key. Otherwise you are accessing the internal key array with a bad index causing an out of bounds error


void APanal::Interact(UPrimitiveComponent* GetComponent, ABaseFirstPersonCharacter* SelfCharacter)
{
	if (GetComponent == Zero)
	{
		if (RightAnswers.Contains(AnswerN))   // <-- this is important!!!
		{
			if (RightAnswers[AnswerN] == Zero->GetMaterials()[0]->GetName())
			{
				RAnswers -= -1;
			}
		}
	}
	if (GetComponent == One)
	{
		UE_LOG(LogTemp, Warning, TEXT("BananaI"));
	}

	if (AnswerN == 3)
	{
		if (RAnswers == 3)
		{
			UE_LOG(LogTemp, Warning, TEXT("You win"));
		}
		else
		{
			UE_LOG(LogTemp, Warning, TEXT("You lose"));
		}
	}
}

Nope. TMap will trigger a nullptr check if key not found, it won’t throw any “out of bounds” errors
Either way I believe OP just uses a TArray that is accessed by wrong index

Missed the apple add in begin play. :slight_smile:
The check on the number of elements should be enough.

Sorry for waiting and poor description of the problem. When i create BP and interact with it all in normal but when i restart unreal my BP broken(I try recreate) i can t go to the scene and open it i have a crash.
I try to add in APanal::APanal()

	RightAnswers.Add("apple");

And do this

if (RightAnswers.Num()>=1 and RightAnswers[AnswerN] == Zero->GetMaterials()[0]->GetName())

But i still have a crash

When i commet all with aray i still have a crash

You need to add in the part gardian206 was talking about:
AnswerN < RightAnswers.Num()
also use && in place of and

if (AnswerN < RightAnswers.Num() && RightAnswers.Num()>=1 && RightAnswers[AnswerN] == Zero->GetMaterials()[0]->GetName())
{
// code here
}

I still get crash. Now i use c++ version and all normal. BP i use only to set up Meshs now i set up it in c++

Ok not all when i try open the level i have crash

what is in the crash report? (the error being thrown at at least the top few lines of the call stack)
there are many reasons the Editor/Engine can crash, and each of them have different solutions.

for things dealing with Content browser try to do those things in blueprints, as they are more suited for it, otherwise you end up with Runtime-Resolved-Hard-References that can silently fail because something got renamed or moved, or spelling error.

With chat GPT i find this. For RAnswers and AnswerN int16 and don t use UPROPERTY(EditAnywhere, BlueprintReadOnly)
Now I have other crash)
In some reason

Assertion failed: Package [File:D:\build\++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectGlobals.cpp] [Line: 2155]

But i think i can fix this

If anyone want know i do this


Everyone THX

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.