I get an exception error when trying to add one element in an TArray declared on header, declaring the TArray inside de function on the cpp file and adding a new element works, but I get an exception when declared in the header.
I think that declaring the TArray on the header file somehow don’t initialize it, so when trying to find the array to add the element it goes null ptr because it’s not initialized, my guess, but i’m really lost here.
The only 2 references to that TArray is the declaration on the header, and the Add function, so the array is not being nulled or reseted.
I replicate that function in Blueprint only and it works fine.
The “AddProgressEvent” is not marked as UFUNCTION, the array was tested as UPROPERTY, same result.
The USTRUCT is declared in other header file and include in the header of the game mode.
My USTRUCT has a constructor, setting the default values of to nullptr and 0 (I’ll add that to the question). But even so, I should be able to use one Structure nullptr and 0 as a member of the array, and the array has no member, as adding one causes the exception and crash.
I’ll keep trying to make it work. thanks for the help, with one example working I’ll get there. Many thanks.
Irrational crashes usually stem from problems elsewhere in the project. Maybe try a reference search to see if nothing is messing with the values along the way?
Calling the function from blueprints works just fine, calling the function using c++ results in the crash, tried using the function from other actor and it crashs anyway.
The actors are already on the level, on begin play each actor individually uses a Function Library to get a ref to GameMode and from that they call the AddProgressEvent, passing “this” and GetActorLocation().X.
I tried setting a timer on the begin play, so that the functions is called only 3seconds after, but it crashs anyway, same result.
At the begin play of each actor I use the GetGModeRef and call the function.
I think the GameMode beginplay is called before the other AActors, not?
I alter the code to check if the gmode ref is valid before calling the AddProgressEvent, and the function don’t get called. So is that it? the GMode ref is the problem?
void AProgressEvent::SendEventToGMode()
{
if (UFnLib_Main::GetGModeRef())
{
UFnLib_Main::GetGModeRef()->AddProgressEvent(this, GetActorLocation().X);
}
}
I already tried on the Gamo Mode Begin Play get all actors of class, and using a loop to add each actor to the array, so that everything happens on the Game Mode, but even so it results in the exception. I’ll try do it again.
the class AGameModeBase that you derive you game mode from does not have a BeginPlay to override. I’m guessing it’s not being called and your passed on reference is not being set.
Put a breakpoint there and run the project and see if it fires.
I’m guessing you might need to replace that functionality with the InitGame method override
Ok. You are calling beginplay on the derived class AActor that AGameModeBase inherits from. I’m not sure of the order in which it is called in that case. (maybe it’s random)
I would strongly suggest to use the InitGame function in place of beginplay
Information from within the gamemodebase class:
/**
* Initialize the game.
* The GameMode's InitGame() event is called before any other functions (including PreInitializeComponents() )
* and is used by the GameMode to initialize parameters and spawn its helper classes.
* @warning: this is called before actors' PreInitializeComponents.
*/
You will be sure it is called before any of your characters
Man I can’t thank you enough, I’m too tired now to try it, sorry, thanks for your time, I’m stuck trying to solve this since 6pm now it’s 8am, I’ll get some rest,.
Didn’t know about the InitGame function, and I bet it will solve my problems, and spare me from many others. I’ll bring the results ASAP. and again thank you.
That was it! It solved the problem, the function was being called before the game mode reference was set, so there’s no valid game mode resulting in the engine to crash.
Thank you 3dRaven to take the time and step through the problem with me, even replicating my code to test. I really can’t thank you enough.
No problem. Best of luck on your project.
It’s best to get over these types of obstacles as fast as possible and get back to the fun part of developing
A last update: Instead of cheking every frame, something I try my best to avoid, I came up with another solution, setting a timer for every event, with the initial delay being the event location/speed, as the speed is constant setting a speed on the level start allows me to use it to calc the initial delay.
I needed to change the way I was moving the actors, instead of a value/frame, I needed to make it frame independent, so I set a value in UnrealUnits/Second (300), and multiplied it by the tick DeltaS.