• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM [SOLVED] collision_line question

Dr_Nomz

Member
I've always used this when checking if an enemy can see the player, but I don't really understand it.
Code:
if collision_line(x,y,obj_Character.x,obj_Character.y,obj_Wall,true,true) <0
Why does it need to be under 0 for the enemy to see the player? If it's over 0, would they see the player regardless of the walls in front of them?
 
It doesn't return a specific number (well, it does, but only because GMS doesn't have true booleans). It returns a boolean value. Either true or false, which breaks down into either 1 or 0 in GMS behind the scenes. However, you should always be referencing booleans as the keywords true or false rather than 1 or 0 or < 0.5, etc. If the line collides with an obj_Wall, then the collision line returns as boolean true. If it doesn't collide with an obj_Wall, then it returns boolean false.

So that line should be
Code:
if (collision_line(x,y,obj_Character.x,obj_Character.y,obj_Wall,true,true) == false) {
Or even better
Code:
if (collision_line(x,y,obj_Character.x,obj_Character.y,obj_Wall,true,true)) { // This is true
if (!collision_line(x,y,obj_Character.x,obj_Character.y,obj_Wall,true,true)) { // This is false
 

FrostyCat

Redemption Seeker
Why does it need to be under 0 for the enemy to see the player? If it's over 0, would they see the player regardless of the walls in front of them?
Read the Manual entry for collision_line():
The Manual entry for collision_line() said:
Returns: Instance id or noone
If that function doesn't see a wall in the way, then it returns noone (-4), which is less than 0. If it does see a wall in the way, then it returns the instance ID of any one of them, which will always be much greater than 0.

It doesn't return a specific number (well, it does, but only because GMS doesn't have true booleans). It returns a boolean value.
Not this particular function. collision_line() returns an instance ID for a collision, or noone for no collision. It has a straight-forward cast to a truth value, but is not a genuine Boolean value, so direct comparisons to true and false will fail while the comparison-free equivalents still work.
Code:
// This works
if (collision_line(x1, y1, x2, y2, obj, prec, notme)) {
  ...
}
// This NEVER works
if (collision_line(x1, y1, x2, y2, obj, prec, notme) == true) {
  ...
}
The existence of "soft Boolean" functions like this is part of the why making comparisons against true and false is a bad rookie habit.
 
Eeek, that's what I get for commenting from memory without checking -_-;

(In my defense, I just woke up and the coffee is still heating up =P)
 

Dr_Nomz

Member
Eeek, that's what I get for commenting from memory without checking -_-;

(In my defense, I just woke up and the coffee is still heating up =P)
Thanks for the help everyone, that all makes a lot of sense. :D

And don't worry Towel, I appreciate the help. ^_^
 
Top