0
\$\begingroup\$

I am making a GameMaker game that involves the enemies having line of sight so that they don't target the player through a wall. I have this code that uses collision line to detect whether there is a wall between the enemy and the player, then sets CanSee to 1 if true.

if collision_line(x, y, obj_parent.x, obj_parent.y, obj_wall_parent, false, true)
    global.CanSee = 1;

However, this code does not ever affect the value of CanSee. Am I using this code wrong?

Edit: Here is my room setup. The walls all have obj_wall_parentas their parent.

Room Setup

Here are some of the objects:

Objects

\$\endgroup\$
  • \$\begingroup\$ Doesn't seem to be a problem in your code. Maybe you can show your room setup and the objects you defined? Maybe you obj_wall doesn't have obj_wall_parent as parent? \$\endgroup\$ – DH. Oct 31 '16 at 17:11
  • \$\begingroup\$ Now that I think about it, your code will only make CanSee = 1 if the enemy isn't seeing the player. Making an if collision_line will return the id, and anything that is > 1 returns true. So, try adding making if !collision_line(x, y, obj_parent.x, obj_parent.y, obj_wall_parent, false, true) and see if the statement works for you. \$\endgroup\$ – DH. Nov 1 '16 at 16:05
1
\$\begingroup\$
if collision_line(x, y, obj_parent.x, obj_parent.y, obj_wall_parent, false, true)
    global.CanSee = 1;

However, this code does not ever affect the value of CanSee. Am I using this code wrong?

Yes, and this is a common error. Check the GameMaker documentation for the function collision_line and you can see it returns:

Returns: Instance id or noone

At the moment your code assumes a true/false response, but since collision_line doesn't return true/false, this won't work correctly.

I would recommend changing your code to:

if (collision_line(x, y, obj_parent.x, obj_parent.y, obj_wall_parent, false, true) != noone)
global.CanSee = 1;

This will set global.CanSee to 1, when the collision line function returns the id of an object. Remember you'll need to reset the variable this is no longer the case.

Happy coding!

\$\endgroup\$
  • \$\begingroup\$ I ended up actually needing if (collision_line(x, y, obj_parent.x, obj_parent.y, obj_wall_parent, false, true) = noone) CanSee = 1;, but thanks for the help! \$\endgroup\$ – MonacleforSauron Nov 1 '16 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.