Defactoring

Hi, I understand why we dafactored the 2 methods GetControlledTank and GetPlayerTank. What I don’t get (and Ben says he’ll explain why, but he doesn’t until the end of the video) is why assigning them to local variables every Tick.

I created 2 instance variables in tank.h and I assign them the values just once, in BeginPlay(), and it works correctly for me. Is there something I’m not getting?

1 Like

Hi,

I’m doing the same thing aswel. It will however mean its slightly harder to go over to multi player if you ever do decide to do that :slight_smile:.

In the case of Multi-player you’ll probably want to create an algorithm that searches for the closest (player/other teams AI) tank.

1 Like

Oh, and respawning of the player tank (for instance after death etc). could also be an issue.

1 Like

Thanks! So, I suggest Ben to explain this if he gets the chance to work on this lesson again, or maybe people could think that the AI tank “loses” this information every single frame :slight_smile:

1 Like

He does this because once you start moving around your location changes. I guess it doesn’t matter yet but I think that was the reasoning.

No, that’s not the reason. Once you get a reference to an object, it’s still the same even if the object changes the value of its properties (i.e., position). So it’s theoretically enough to get the reference once, and use it everytime you need it. The only possible reasons for getting it every frame are the ones in the comments above :slight_smile:

2 Likes

Privacy & Terms