Tarray of struct access violation when adding a normal ustruct?
I tried searching and found https://answers.unrealengine.com/questions/361743/violation-error-on-add-to-tarray.html but this seems to not apply here? I am getting super confused here.
So I have "ScoreStructItem.h" #include "CoreMinimal.h" #include "ScoreStructItem.generated.h"
And I have a class extending "AGameModeBase":
And finally I have "SaveScore.h" which has
In my "SaveScore.cc" I have
But whenever I call "AddScore(10)", it crashes with the following message.
Access violation - code c0000005 (first/second chance not available)
Which points to my "RecentScores.Add(ScoreStruct);" as the cause.
I am very confused because I don't even have pointers anywhere and as far as I understand, structs I can just pass by value in Unreal and it will create a copy for me that doesn't go out of scope?
Any help I will be super grateful, thanks!
Edit Here is the full stack
I don't think it's ScoreInstance is null because this is the code I have and check where the log doesn't crash the engine:
Where LoadScore is
Okay I found out the REAL reason as to why this was happening.
Turns out it wasn't a problem of GameState or GameMode. Which makes sense since this is all local so one shouldn't technically cause problem where clients don't have access to gamemode.
The real reason behind it was because I was exposing my pointer via a UPROPERTY. What happens with this I think is that when the object is being initialized, it copies over the Blueprint defaults which is None, hence resetting my pointer. The reason why this didn't happen when I switched over to GameState was because I made blueprint of GameMode and GameState was still in pure CPP format, meaning Blueprint defaults were not overriding anything.
I got around this via exposing the pointer via a function and not exposing it as a visible property elsewhere.
For more information, they go through it in more lengthy detail here: https://forums.unrealengine.com/development-discussion/c-gameplay-programming/28537-uproperty-member-vars-reset-to-null-by-objectinitializer?57727-UPROPERTY-member-vars-reset-to-NULL-by-ObjectInitializer=
--------------------------------- Wrong explanation from11/28 - keeping for context
Okay, so turned out this had nothing to do with TArray of structs... Writing my response here in case anyone has same problem in the future.
I still don't really understand what's going on but here are my findings.
It turns out that the GameMode that is constructed at the beginning which logs the construction success is NOT THE SAME GameMode that I get via
from my Pawn.
I solved this by creating my GameState that has the exact same fields, constructor, and functions.
From what I understand GameMode is only available on Server side. However I am not sure how this applies here... but everything is fixed with GameState in my case.
Follow this question
Once you sign in you will be able to subscribe for any updates here