My for loop causes my program to crash, and I don’t know why.
AInventory* inventory = Cast<AInventory>(GetWorld()->GetFirstPlayerController()->GetCharacter());
if (hudState == main)
{
if (inventory->cTowerArray.Num() > 0)
{
for (int c = inventory->cTowerArray.Num() - 1; c >= 0; c--)
{
DrawRect(FLinearColor::Gray, (Canvas->SizeX* 0.735) + 5.f + (20.f * c), Canvas->SizeY * 0.815 + 5.f + (20.f * c), 30.f , 30.f);
inventory->cTowerArray.RemoveAt(c);
}
}
}
My logic is that this loop should run twice because cTowerArray.Num() = 2 and c = 2 - 1 and the loop is supposed to end if c < 0; so it should remove cTowerArray[0] and [1], right?
Also note this is inside a function called DrawMenu and I have DrawMenu in my DrawHUD() function so I understand that it is ticking 60 times per second. How do I get around this?
Where does it crash? Why are you casting a character to an AInventory?
Also, the DrawRekt function might try to draw outside the canvas. Check your limits.
No, I meant at what line does it crash, but you answered this further down. If nothing is in it the loop will not run so naturally it will not crash.
Just because it does crash when the DrawRect is commented out does not mean that DrawRect is not a problem.
Anyways, the loop looks fine. We cant help you much because we dont have the code. You have to write debug logs and test things out. Good luck.
I have a character class called AInventory that has an array with some tower pawns in it. I’m casting to it so I can get a reference to my array. It crashes at the for loop. Everything before then works fine. Hell, the for loop runs if there is nothing in the for loop.
Also I don’t think it’s the DrawRect because I commented that out and tried to run the loop with a debug message and it still crashed. It’s like any time I have anything in the loop the editor crashes when I run it.
I’m essentially doing this so I can draw background squares for icons, and I need the amount of squares to change depending on whatever TowerArray.Num() is so it has to be looped.
Hey Snowl0l-
Can you provide the callstack and log file form the crash for added information? Additionally, if you can reproduce the crash in a new project, please include the steps to setup the code/project that recreate the crash.
I don’t actually know how to provide the callstack or log file from the crash, however I can recreate this in a new project 100%. I know I’m doing something wrong I just don’t know what.
I’ve run into this problem before.
I’ve worked around this problem by manually adding every line I need to like so:
if (hudState == main)
{
DrawRect(FLinearColor::Black, (Canvas->SizeX * 0.735 + Canvas->SizeX * 0.25) - (1.f * (padding + sizeX)), Canvas->SizeY* 0.815 + padding, sizeX, sizeY);
DrawRect(FLinearColor::Black, (Canvas->SizeX * 0.735 + Canvas->SizeX * 0.25) - (2.f * (padding + sizeX)), Canvas->SizeY* 0.815 + padding, sizeX, sizeY);
DrawRect(FLinearColor::Black, (Canvas->SizeX * 0.735 + Canvas->SizeX * 0.25) - (3.f * (padding + sizeX)), Canvas->SizeY* 0.815 + padding, sizeX, sizeY);
DrawRect(FLinearColor::Black, (Canvas->SizeX * 0.735 + Canvas->SizeX * 0.25) - (4.f * (padding + sizeX)), Canvas->SizeY* 0.815 + padding, sizeX, sizeY);
}
I just wish I didn’t have to do that. The problem 100% is from the fact that I am running a tick() with a function in it. I’d be happy to make a video of me making the project and running into the problem. Gimme a sec
The callstack appears in the black crash report client window that shows after the crash occurs. The log files are generated after the crash in the ProjectName/Saved/Logs directory. And, while a video would be nice, attaching the sample project itself would be more helpful.
Ok so in doing all of that I discovered the problem isn’t the actual loop, but accessing the array in the loop makes me crash.
AInventory* inventory = Cast<AInventory>(GetWorld()->GetFirstPlayerController()->GetCharacter());
In this line I’m trying to get a reference to my inventory character class so I can access its arrays. Am I doing this correctly? Apparently the crash is coming from me calling inventory->towerArray.Num() because I didn’t initialize the pointer correctly.
Sorry for the lack of logfiles/crash report. Nothing pops up when my unreal crashes. And I have a .dmp file and a crashcontext file so if you want those I can send them over
But if I’m initializing inventory incorrectly shouldn’t there be an error? Why would it let me initialize it improperly
there are no actual compilation errors
god dammit. it is nullptr. So this was never about the for loop. This was about referencing my character. I had been trying to figure out how to create an instance of my inventory class for days, and I thought I figured it out.
Back to square one. Sorry guys
Have you checked to see if the pointer is valid? That it is not a nullptr?