GMS 2 questions about collision_line

Maxandkon

Member
now I use this command in this way
if !collision_line (enemy_obj.x, enemy_obj.y, object_player.x, object_player.y, object_wall, true, false) {
further a long code so that it follows the player
}
it only works on 1 enemy, only 1 enemy reacts to me, and thus, when he sees me, he launches a function for everyone, but can it be done separately for each and and if this particular enemy saw me, only he reacted to me, or should they be created as separate objects and each should be assigned such a function?
 

FrostyCat

Member
Learn the difference between an object and an instance and how to use with blocks.

If your code is from enemy_obj, skip the enemy_obj. prefix on your variables. Never refer to an instance's own variables by the object ID.
Code:
if (!collision_line(x, y, object_player.x, object_player.y, object_wall, true, false)) {
  ...
}
If your code is being called from somewhere else, use a with block.
Code:
with (enemy_obj) {
  if (!collision_line(x, y, object_player.x, object_player.y, object_wall, true, false)) {
    ...
  }
}
 

kupo15

Member
now I use this command in this way
if !collision_line (enemy_obj.x, enemy_obj.y, object_player.x, object_player.y, object_wall, true, false) {
further a long code so that it follows the player
}
it only works on 1 enemy, only 1 enemy reacts to me, and thus, when he sees me, he launches a function for everyone, but can it be done separately for each and and if this particular enemy saw me, only he reacted to me, or should they be created as separate objects and each should be assigned such a function?
It would be nice to see your code to see where the error is haha but I suspect its because you are using enemy_obj in that detection code. Instead you should do

var enemy = !collision_line (enemy_obj.x, enemy_obj.y, object_player.x, object_player.y, object_wall, true, false)

with enemy
{
long code that would be nice to see
}

Edit: Yeah the above works too
 

Maxandkon

Member
Code:
if (!collision_line(x, y, object_player.x, object_player.y, object_wall, true, false)) {
  ...
}
yes, it didn’t work since I substituted object_enemy. (x / y) instead of just x and y. Now it works correctly, thank you

Honestly, I just started to understand coding, so I don’t really understand why this command is and how to apply it correctly
 

kupo15

Member
Honestly, I just started to understand coding, so I don’t really understand why this command is and how to apply it correctly
You probably wouldn't want it unless you want enemies to see through other enemies. Collision line only returns one enemy in the line of sight where as the _list version returns all enemies that collide with that line. This way you can have multiple enemies spot you in the same collision check

However, collision line picks an enemy that falls on the line and returns one. The _list version I believe you can specify which enemy you want to react (ie the closest one)
 
Top