Here's something I whipped up a while ago. It gives an event (like a guard) a line of sight (i.e. 1-5 squares in front of itself) and it detects the player if they walk into the event's line of sight. The result can be modified from there. This even works if the event is moving, but it requires some finesse to keep it from looking through walls, etc. This can be done by adjusting its motion path or setting a particular case if the event as at a certain problem location. You can download the project at:
http://www.4shared.com/file/129240096/94caef4a/Guard_Test.html.
Basically I set Hero X and Y variables and Event X and Y variables. I put all of the coding inside the guard so it's easy to keep track of, and you can set its variables more easily (This Event's...). Here's the technical stuff:
Alternatively, here's a snippet of the code:
This will make the guard notice the player if they are within 5 tiles of the guard. The reason for setting the checks is due to the fact that you can't check differences, only one variable. To solve this, I made Check variables that check the player and event's current locations, subtract, and see if the result is within a range of 5. IT IS IMPORTANT to make these variables separate from the Hero X/Y and Event X/Y variables because every guard event that you have running on the map needs to run this check, and they can't all be subtracting and testing the same variables.
Using this system of checks, you can give your guards any motion path you wish, but you will have logic problems if there is an object between the guard and the player, because the guard will not detect the object. To get around this you can set your guards' motion paths so that they will not be in a position to look through objects, you can adjust the ranges if you know a stationary guard will be looking through an object in one direction, or you can set for a special check if the guard is at Position X/Y.
If you would like to change the guard's vision range, simply adjust the final check,
If Event X Check <= 5(Less or Equal).
If you wanted the guard to see 10 tiles ahead, change 5 to 10. If you'd only like the guard to see 3 tiles ahead, change 5 to 3, etc.
If anyone notices any problems with my code, please let me know. It worked fine when I tested it, but I'm not perfect. Good luck with your projects!
http://www.4shared.com/file/129240096/94caef4a/Guard_Test.html.
Basically I set Hero X and Y variables and Event X and Y variables. I put all of the coding inside the guard so it's easy to keep track of, and you can set its variables more easily (This Event's...). Here's the technical stuff:
If facing left...
If Player Y == Event Y
If Player X < Event X
Set Hero X Check = Player's Map X
Set Event X Check = Event's Map X
Event X Check - Hero X Check
If Event X Check >= 1(Greater or Equal)
If Event X Check <= 5(Less or Equal)
Text: I see you!
Endif (Branch End)
Endif
Endif (Player X < Event X)
Endif (Player Y == Event Y)
Endif (facing left)
If Player Y == Event Y
If Player X < Event X
Set Hero X Check = Player's Map X
Set Event X Check = Event's Map X
Event X Check - Hero X Check
If Event X Check >= 1(Greater or Equal)
If Event X Check <= 5(Less or Equal)
Text: I see you!
Endif (Branch End)
Endif
Endif (Player X < Event X)
Endif (Player Y == Event Y)
Endif (facing left)
This will make the guard notice the player if they are within 5 tiles of the guard. The reason for setting the checks is due to the fact that you can't check differences, only one variable. To solve this, I made Check variables that check the player and event's current locations, subtract, and see if the result is within a range of 5. IT IS IMPORTANT to make these variables separate from the Hero X/Y and Event X/Y variables because every guard event that you have running on the map needs to run this check, and they can't all be subtracting and testing the same variables.
Using this system of checks, you can give your guards any motion path you wish, but you will have logic problems if there is an object between the guard and the player, because the guard will not detect the object. To get around this you can set your guards' motion paths so that they will not be in a position to look through objects, you can adjust the ranges if you know a stationary guard will be looking through an object in one direction, or you can set for a special check if the guard is at Position X/Y.
If you would like to change the guard's vision range, simply adjust the final check,
If Event X Check <= 5(Less or Equal).
If you wanted the guard to see 10 tiles ahead, change 5 to 10. If you'd only like the guard to see 3 tiles ahead, change 5 to 3, etc.
If anyone notices any problems with my code, please let me know. It worked fine when I tested it, but I'm not perfect. Good luck with your projects!