Performance consideration question: AActor* in Tick

From lecture:

In lecture we obtain pointer to player every tick.

In my version of code I create an AActor* and set it once in BeginPlay. This is how I typically do things in C# and Unity, however over here in Unreal and C++ land - is there something I should watch out for in doing that or is there a reason why we get Actor pointers in tick or is it really something that doesn’t matter much either way?

1 Like

Sorry I missed this question originally. I haven’t benchmarked it at all but looking at the code I doubt it’ll be that noticeable as it would be effectively indexing the first element of an array.

FYI, looking at UE code, the pawn is a member variable of the PlayerController, so that part it free. The harder part is getting the controller. And for that, UE is only iterating through a couple arrays of player controllers, in the worst case, and usually it is the first item of the first array.

I didn’t benchmark either, but short of nasty surprises, it would require calling GetPlayerPawn several thousands of time per tick, if not millions, for it to become a problem.

I wouldn’t even be surprised if calling GetPlayerPawn was faster than having a member variable, just because that PlayerController array might stay hot in the CPU cache (since it’s regularly needed by various different objects), while the AIController would get cold (since it’s only need when processing that AI) and would need to be fetched again.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms