What's the benefit of LazyValue in AiController?

I honestly don’t understand it.

GuardPosition is used only by AiController itself, when it comes to returning to the original position after chasing a player. There’s no circumstance when another script is accessing it externally without it being called by AiController itself, and no circumstance when it’s accessed right on Start.
On top of that, we are replacing direct access to transform.position in Start via… still accessing transform.position in Start, but through a more convoluted system (because that’s what ForceInit does, in the end).

If I understood the whole data hazard concept well, the thing we should be worried about is that Transform may not be ready to give us correct informations about position, but that’s not something we can change ourself (plus, IIRC, I think I read somewhere in the past that Unity is always initializing the default components before any custom ones added by us, so when it’s time for our custom scripts to do their thing it never happens that Transform doesn’t know where it is, right?) and anyway it doesn’t seem we we fixed it with this refactor.

Am I missing something?

I never put the guard position in a lazy value. If it were being saved by the Saving System, it might make sense then. You can use it or not in this case and it should have no difference… That’s not necessarily true with the other LazyValues, however. Once they start interacting with Saving, keep your eyes out for race conditions.

Thanks! Yes, I understood the reason for using LazyValue in the other classes, but I could not see it specifically for AiController and I wondered if I was missing something.

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

Privacy & Terms