Hello Unreal-ites HAPPY NEW YEAR!!! Thank you for clicking on this post.
I will try to keep this as short as possible.
In C++ BatteryCollector Tutorial https://www.youtube.com/watch?v=1tqT…dGiM4&index=12
In BatteryCollectorCharacter.cpp , a function called ColectPickups() is implemented. The order of operation in the for loop:
- Test overlapped actors as type APickup. If true then
- Run Func TestPickup->WasCollected() <- if this is a type of ABatteryPickup (which they are in the video), the override found in ABatteryPickup.cpp has an instruction Destroy()
- Test if the item picked up is a type of ABatteryPickup, if true
- Add the ABatteryPickup.BatteryPower to an accumulator.
- Set the ABatteryPickup.bIsActive = false
- After the loop, update the character’s total power with that of the the accumulator
At the end of the posted video, Character’s CurrentPower attribute updatesr.
On my end, as it should, the program crashes because the line: if(BatteryTest) should cause some kind of exception accessing a null ptr. Why? ABatteryPickup instances are destroyed BEFORE number 3 in the order of ops. If I comment out the line Destroy() in the function ABatteryPickup::WasCollected , it works correctly.
Furthermore, if i change the line from if(BatteryTest) to if(IsValid(BatteryTest)) the program continues to run, batteries disappear because they are destroyed, but the character’s power does not change.
if I perform the test: if(!IsValid(TestBattery)) this enters showing that the pointer is no longer valid AND/OR it has a PendingKill() true.
Please help me understand if I am doing something wrong, or, the video is just lucky somehow with what appears to be a race condition (perhaps the Destroy() carry out is deffered until after the frame in the 4.9 version, where in, at least, 4.22, the work is done right there, or perhaps there are arguments that were implied in 4.9, where I need to be explicit in 4.22 to match the behavior? Can someone explain how the code in that video actually works without crashing the editor like it should? It feels like a lucky race condition, no?
I will be happy to post any code you wish.
-Erol