Windows Need advice - Line of sight check via collision_line_list & for loop

K

koffe

Guest
I'm trying to create a simple LOS check inside of my enemy objects. With the code below, I create a list of all collisions along a line each frame and search through looking for matches with the variable 'tallObject =1 '.

I'm printing 'viewIsBlocked' in the console and I can see the LOS line updating with '1' or '0' on collisions, however the code isn't discrimating between tallObjects 1 or 0. Even if I break LOS with an object which has 'tallObject = 0', viewIsBlocked will return = 1

I'm sure it's a small error but I'm really struggling to conceptualize what's going wrong. Is this a junk system entirely or can someone see a simple fix?

var _num = collision_line_list(x, y, obj_player.x, obj_player.y, all, false, true, global.LOS_list, false);

if _num > 0 {

for (var i = 0; i < _num; ++i;){

if global.LOS_list[| i].tallObject == 1 {
var losBlocker = 1;
}}}

switch (losBlocker){
case 0:
viewIsBlocked = 0;
break;

case 1:
viewIsBlocked = 1;
break;

}

 
Last edited by a moderator:

Nidoking

Member
Why are you using a local variable and a switch? Do you plan on having multiple values of tallObject in the future? Just set viewIsBlocked to 0 before the loop, and if you find a tall object in the list, set it to 1 and break out of the list.
 
K

koffe

Guest
var _num = collision_line_list(x, y, obj_player.x, obj_player.y, all, false, true, global.LOS_list, false);

if _num > 0 {
for (var i = 0; i < _num; ++i;){

if global.LOS_list[| i].tallObject == 1 {
viewIsBlocked = 1;
ds_list_clear(global.LOS_list)
break;
}}}

if _num == 0 {
viewIsBlocked = 0;}


This is cleaner but it's still broken.

The code now correctly recognizes small objects and doesn't trigger 'viewIsBlocked', but only once. If I collide with a small object (tallObject = 0) the line stop working?

You can see in this gif what I mean. The cupboard correctly triggers it, but after I've collided with the small desk once, the line doesn't detect the cupboard again

https://gfycat.com/EnlightenedCreamyBarasinga
 
Last edited by a moderator:
K

koffe

Guest
var _num = collision_line_list(x, y, obj_player.x, obj_player.y, all, false, true, global.LOS_list, false);

if _num > 0 {
viewIsBlocked = 0;
for (var i = 0; i < _num; ++i;){

if global.LOS_list[| i].tallObject == 1 {
viewIsBlocked = 1;
ds_list_clear(global.LOS_list)
break;
} }}

if _num == 0 {
viewIsBlocked = 0;}


Thanks, I'll tweak that now. This code causes the same issue as in the gif above. Once the line has collided with a single tallObject = 0 object, it stops detecting tallObjects
 
K

koffe

Guest
Making the list local appears to have fixed it. Thanks alot!
 
Top