This is in Unreal Engine so does use some code from that, but this SHOULD be a standard C++ problem I am having from what I can tell hence why I am posting it here. I know little about pointers so have likely messed something up here.
Writing an automation test, but that shouldn't matter for the most part.
I have some integers, and 2 of them are not keeping the values I store in them. Here is my current code (with extra unneccesary stuff I am using to debug:
UPROPERTY()
ARSRPlayerCharacter* PlayerCharacterX = TestUtils::GetFirstPlayerCharacterServer();
UPROPERTY()
ARSRPlayerCharacter* PlayerCharacterY = TestUtils::GetAdditionalPlayerCharacterServer(2);
UPROPERTY()
int32 StartXPNoBuff = 0;
UPROPERTY()
int32 EndXPNoBuff = 0;
UPROPERTY()
int32 StartXPBuff = 0;
UPROPERTY()
int32 EndXPBuff = 0;
UPROPERTY()
int32 PlayerWoodcuttingXP = 0;
UPROPERTY()
int32* StartXPNoBuffPtr = &StartXPNoBuff;
UPROPERTY()
int32* EndXPNoBuffPtr = &EndXPNoBuff;
UPROPERTY()
int32* StartXPBuffPtr = &StartXPBuff;
UPROPERTY()
int32* EndXPBuffPtr = &EndXPBuff;
PlayerWoodcuttingXP = PlayerCharacterX->SkillsComponent->GetWoodcuttingXP();
ADD_LATENT_AUTOMATION_COMMAND(ChopWood(PlayerCharacterX, PlayerWoodcuttingXP, true, StartXPNoBuff, EndXPNoBuff));
ADD_LATENT_AUTOMATION_COMMAND(Test(&StartXPNoBuff, &EndXPNoBuff));
ADD_LATENT_AUTOMATION_COMMAND(FWaitLatentCommand(5.0f));
ADD_LATENT_AUTOMATION_COMMAND(Test(&StartXPNoBuff, &EndXPNoBuff));
ADD_LATENT_AUTOMATION_COMMAND(TeleportPlayer(PlayerCharacterY, FVector(5499.0f, 2848.0f, 215.0f)));
ADD_LATENT_AUTOMATION_COMMAND(Test(&StartXPNoBuff, &EndXPNoBuff));
ADD_LATENT_AUTOMATION_COMMAND(ChopWood(PlayerCharacterX, PlayerWoodcuttingXP, true, StartXPBuff, EndXPBuff));
ADD_LATENT_AUTOMATION_COMMAND(Test(&StartXPNoBuff, &EndXPNoBuff));
ADD_LATENT_AUTOMATION_COMMAND(CheckXPImproves(this, StartXPNoBuffPtr, EndXPNoBuffPtr, StartXPBuffPtr, EndXPBuffPtr));
OK, so Latent commands are asyncrhonous. They are queued up in order, and the next one will not run until the previous has returned true. I pass as pointers and reference the values inside for all times I use these specific variables, as they are all queued up right away based on their values before the first latent command has been completed.
So the offenders are int32 StartXPNoBuff and EndXPNoBuff. They are intiialised to 0, and set (for the time being, it should be 0 and 34 respectively every time) in the first Latent WoodChop function.
They are set correctly, as the Test() latent command shows. The test latent command outputs "Var1: [StartXPNoBuff], Var2: [EndXPNoBuff]". Here are the values I get out from each time it is run:
Var1: 0, Var2: 34 Var1: 0, Var2: 0 Var1: -2, Var2: 157761904 Var1: -2, Var2: 157761904
I do not use these variables until the final latent command (other tahn my debug commands), so the values should not change.
After taking help from others about, I put UPROPERTY() in front of every variable but that made no difference, have had a pointer variable for each, but this makes no difference, Nothing seems to work.
StartXPBuff and EndXPBuff work exactly the same way, their values are set from within the EXACT same method, however these retain their values and there is no problem with them. Why can I not get this to work for the first 2?
Without the surrounding code, I'm assuming that everything presented is inside of a RunTest() function for either a simple or complex Unreal Automated test.
Passing the pointer to a local variable (&StartXPNoBuff) in the test function to a latent command will have random results. The local variable will go out of scope after the test function completes. When the latent function runs during the next frame, it just has a dangling pointer to work with. It will point at random memory...
If these variable need to be kept between frames, find somewhere to store them: An AActor in the UWorld, a global variable allocated in the test file, etc... Just find something that will last from frame to frame.