Compiling the same code twice, different results?

I spent days trying to track some arbitrary bugs in my game (some collisions would stop working on some actors). But I failed to track down the bugs, the code seems fine, but somehow it happens differently in the game.

Eventually I found that: 1) the bug only happens on PIE, if I package the game, the packaged build will work perfectly. 2) if press CTRL+F5 in VS to run without debugger and then the bug happens in PIE, all I have to do is close the editor -> run with a debugger (F5 in VS), close the editor again -> now press CTRL+F5 to run the game again. And now it will work perfectly!

I just compiled the same code. Twice. The first time it didn’t work, the second time it did. From time to time the bug will happen again. And all I have to do is repeat the same dance: close, F5, close, CTRL F5, fixed.

Why does this happen? How do I even begin to debug this? Does it mean it’s not a problem in my code? Or is it?

I’m using UE4.25 with VS2019. The bug happens when I Run with “Development Editor” build configuration. I already tried: “Refresh visual studio 2019 project” and tried deleting Intermediate and Saved folders. Bug continued.

Are you using hot reload? hot reload is somewhat buggy, most people recommend to switch unreal editor to live coding.

I typically compiled 2x in a row to get hot reload to work, sometimes it doesn’t pick up your changes the 1st compile.

No hot reload. I just CTRL F5 in Visual Studio with the editor closed. That will compile and open the editor with a version of the code that doesn’t work.

I stopped using hot reload exactly because of the double compile problem. That was a different problem tho. With hot reload, when it didn’t work it was because it just didn’t hot reload at all, so it was using the older version of my code before newer changes. Which sucked, but at least I could understand what was going on. But now without hot reload I have no idea what the double compile problem even is. When the bug happens, it’s not an older version of my code. It still has the recent changes I just made. Except some completely arbitrary bugs show up. And then they disappear when I re-compile… Why would that possibly happen? I’m so confused ><

So unreal engine uses serialization for everything when you do ufunctions or uproperty in the header files. When you make too many changes, or delete something from the header, this can cause the serialization to mess up.

So sometimes you will see weird things where your c++ code is working, but then something you just added or deleted, or even changed doesn’t get picked up.
It gets tricky and sometimes you have to close the editor and open it back up. sometimes more than once + multiple re compiles. other times you have to force delete your ufunction or uproperty, recompiled then add it back in etc.

Are you certain that you are saving edited code files before you compile? Some VS add-ons like Visual Assist do this for you automatically, but by default visual studio doesn’t do that so you’ll have a file open and edited, but that’s not the version of the file that gets compiled.

I’m not using any extensions, but I’m pretty sure it does save on compile. Just did a super quick test, added a new log on some begin play, pressed CTRL F5 without saving. The asterisk showing the file is unsaved is immediately removed when I press CTRL F5. And then the log line does show up fine in PIE, so it did use the latest changes when compiled.

Anyway, as a work around I’ve been just using the “DebugGame Editor” build config instead of “Development Editor”. And so far the bug hasn’t happened for a whole day. So I guess the problem is just with the Development Build somehow? So I can just trade off longer compile times vs never seeing that bug again. Oh well…

DebugGame Editor vs DevelopmentEditor will build different binaries so maybe that’s the issue. If you build ‘DebugGame Editor’ for example, then open the editor separately, it will still be running the old out-of-date Development Editor binaries.

If you want to Debug anything properly you have to be running in “DebugGame Editor” with VS attached anyway. Development isn’t all that useful for debugging due to generating optimized code etc etc.

Yeah that could be it

I usually do run with Debugger attached when I do find a bug I need to track down. But when doing regular development and not looking for any specific bug, then I’ll close the debugger and just run with Development build because it’s faster to iterate on.

Isn’t that the normal way most people work in UE4? Or do you always use DebugGame all the time any way?

This

So many times I’ve had gotcha moments where I forgot that it does that.

Personally I always use DebugGame just because 95% of the time I’m coding anyway, or working from a source build where switching can trigger a longer build time… but yeah I do the same as you sometimes too - just have to make sure the Dev Editor binaries are up-to-date also!