• 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!

Need help with modifying a code.

A

AUGE

Guest
Okay, so I have some codes that are working good as long there is no "height" variable integrated there. Getting coordinates to the nearest object as laser beam hits them and then drawing that laser is what I'm trying to achieve. The code works great when there is not height in it, so I'm gonna post stock code here. I need your help in integration of a "height" variable so that it would only check the objects that are at a certain height. The problem why I need your help is because for a modification I'm using "with" function and only allow these objects to work on that are are at a certain "height". I don't know why, but it draws a laser to only one object in the whole map. It's like it's working only on one object and ignore others.
So, here is the code:

Player object's step event:
Code:
direction = point_direction(x,y,mouse_x,mouse_y);
dist = range_finder(x,y,direction,max_laser,obj_collision,false,false);
Player object's draw_event:
Code:
draw_line_width_color(x,y,x + lengthdir_x(dist, direction),y + lengthdir_y(dist, direction),2,c_red,c_yellow);
draw_self();
collision_line_first script:
Code:
{
    var ox,oy,dx,dy,object,prec,notme,sx,sy,inst,i;
    ox = argument0;
    oy = argument1;
    dx = argument2;
    dy = argument3;
    object = argument4;
    prec = argument5;
    notme = argument6;
    sx = dx - ox;
    sy = dy - oy;
    inst = collision_line(ox,oy,dx,dy,object,prec,notme);
    if (inst != noone) {
        while ((abs(sx) >= 1) || (abs(sy) >= 1)) {
            sx /= 2;
            sy /= 2;
            i = collision_line(ox,oy,dx,dy,object,prec,notme);
            if (i) {
                dx -= sx;
                dy -= sy;
                inst = i;
            }else{
                dx += sx;
                dy += sy;
            }
        }
    }
    return inst;
}
range_finder script:
Code:
/// range_finder(x,y,dir,range,object,prec,notme)
//
//  Returns the exact distance to the nearest instance of an object in a
//  given direction from a given point, or noone if no instance is found.
//  The solution is found in log2(range) collision checks.
//
//      x,y         position in room, real
//      dir         direction to look in degrees, real
//      range       the greatest distance to look in pixels, real
//      object      which objects to look for (or all), real
//      prec        true to use precise collision checking, bool
//      notme       true to ignore the calling instance, bool
//
/// GMLscripts.com/license
{
    var ox,oy,dir,range,object,prec,notme,dx,dy,sx,sy,distance;
    ox = argument0;
    oy = argument1;
    dir = argument2;
    range = argument3;
    object = argument4;
    prec = argument5;
    notme = argument6;
    sx = lengthdir_x(range,dir);
    sy = lengthdir_y(range,dir);
    dx = ox + sx;
    dy = oy + sy;
    if (collision_line(ox,oy,dx,dy,object,prec,notme) < 0) {
        distance = -1;
    }else{
        while ((abs(sx) >= 1) || (abs(sy) >= 1)) {
            sx /= 2;
            sy /= 2;
            if (collision_line(ox,oy,dx,dy,object,prec,notme) < 0) {
                dx += sx;
                dy += sy;
            }else{
                dx -= sx;
                dy -= sy;
            }
        }
        distance = point_distance(ox,oy,dx,dy);
    }
    return distance;
}
 

obscene

Member
Should be as simple as...

if (inst != noone && int.depth < desired_height) {

Make sure you have short-circuit evalution enabled or that will cause an error.
 
A

AUGE

Guest
Should be as simple as...

if (inst != noone && int.depth < desired_height) {

Make sure you have short-circuit evalution enabled or that will cause an error.
What is that evolution thing? And where to add this code?
 

obscene

Member
A) That's a modified line from your own code it should be obvious to see where it goes.

B) What are you talking about... "evolution?"
 
A

AUGE

Guest
I don't understand where to put that code. Because there are many possibilities, like in script, in object or from all the objects...
 

obscene

Member
I took a line from the code YOU shared... and changed it.

You must not have written this code yourself if you don't recognize immediately what I changed.
 
Top