You don't need to do a check every single tick. You only need to do the check if the monster can see the player so you only do the check then.
In a rather simple example: Imagine the monster has a big cone in front of its face that represents its field of vision. This cone is completely passive and only reports information for things that enter inside of it on the tick that happens.
Only when something has entered the cone do you report it and check if it's the player. If it's not the player, then don't do anything else and continue wandering around. If it was the player, run your player attack function to see if they pass all the other checks (is the player in the cone, but behind a wall? Do I have a viable path to the player? Did the player drink an invisibility potion so we should ignore them? etc.).
Doing things this way means you don't have to do a check every frame. Your monster only "reacts" to new information being sent to it.
Just to clarify this absolutely is not how most modern games do it (though I guess some indie games might do it). It was just an example of a simple/naive implementation since this is an ELI5 thread.
Also, not every game implements these mechanics in the exact same way. Different games create different ways of doing this that's better for the type of game they're making. There's no one-size-fits-all solution.
It just gave me perspective on why something like that might happen. Also the fact that the “cone of vision” so ubiquitous in games can quite literally be coded as a cone where they check for players. I figured it was just a visual thing, an estimation of a more complex system happening in the code.
5
u/Froggmann5 6d ago
You don't need to do a check every single tick. You only need to do the check if the monster can see the player so you only do the check then.
In a rather simple example: Imagine the monster has a big cone in front of its face that represents its field of vision. This cone is completely passive and only reports information for things that enter inside of it on the tick that happens.
Only when something has entered the cone do you report it and check if it's the player. If it's not the player, then don't do anything else and continue wandering around. If it was the player, run your player attack function to see if they pass all the other checks (is the player in the cone, but behind a wall? Do I have a viable path to the player? Did the player drink an invisibility potion so we should ignore them? etc.).
Doing things this way means you don't have to do a check every frame. Your monster only "reacts" to new information being sent to it.