GML For Loop Behaving Inconsistently

Divinik

Member
I'm trying to use for loops to determine the area that the player's companions are able to go to, and for some reason sometimes it will work and shrink if the section is hitting a wall, and other times it won't.

Here's the code:

Code:
if collision_rectangle(bbox_left, character_inUse.bbox_top, bbox_left+(sprite_width/2), character_inUse.bbox_bottom, obj_wall, false, true)
  {
   for (i = bbox_left; i < bbox_left+(sprite_width/2); i++)
     {
      if !collision_line(i, character_inUse.bbox_top, i, character_inUse.bbox_bottom, obj_wall, false, true)
         and collision_line(i-1, character_inUse.bbox_top, i-1, character_inUse.bbox_bottom, obj_wall, false, true)
        {
         bnd_limit_left = i;
        }
     }
  } else {bnd_limit_left = bbox_left};
For my top and bottom section the code works like a charm. But when I tried to program it for the left sections, it would not work sometimes.

Any ideas?
 

YoSniper

Member
Why are the bbox_top and bbox_bottom properties referring to the character_inUse, but not the bbox_left? And why not use bbox_right for the right side argument?
 

Divinik

Member
Why are the bbox_top and bbox_bottom properties referring to the character_inUse, but not the bbox_left? And why not use bbox_right for the right side argument?
Because that's what I need for my game. Not a very helpful or relevant reply
 
Uhhh, it very much could be helpful. You say the top and bottom sections work fine, where you are referencing character_inUse bbox's, then you say that the left and right sections aren't working, where you are referencing simply bbox, instead of character_inUse's bbox. That seems like it could potentially be a clue as to why one section works but the other doesn't. I always find it funny when people are having trouble with something and yet reply with dismissal when someone else asks a question in an effort to help.
 

TheouAegis

Member
Are you supposed to be using sprite_width/2 instead of simply x? Your origin is centered, isn't it? What is the value of sprite_get_bbox_left(sprite_index)? If it's not 0 and your sprite's origin is centered, then bbox_left+sprite_width/2 is too high of a cutoff. So moving left, it should be from bbox_left to x. For moving right, it should be from x to bbox_right.
 

Divinik

Member
Are you supposed to be using sprite_width/2 instead of simply x? Your origin is centered, isn't it? What is the value of sprite_get_bbox_left(sprite_index)? If it's not 0 and your sprite's origin is centered, then bbox_left+sprite_width/2 is too high of a cutoff. So moving left, it should be from bbox_left to x. For moving right, it should be from x to bbox_right.
You may be right, I was trying to do only sections of the sprite as it follows the center of the player character, and is supposed to be the boundary for which the companions can access.
 
Top