SOLVED Any function or code that works like "draw a line for the instance whose variable is x?

Misael644

Member
Hello!
So, basically I want to make a code that is like: "draw a line for the instance whose variable is x".
I thought about using instance_find to do this, but it only works like "I will only draw a line if the instance id is x".
I wanted to use such a function to make "line graphics", which could be used for debugging or visual effects. If I want to destroy a single instance (dot) to avoid low FPS in the game, the lines will not draw and the line graph will be broken.
Is there a function or code that works the way I described?
 

Mr Magnus

Viking King
GML:
with(object){
    if(variable == x){
        ///draw line here
    }
}

With will loop over every instance whose id or object_id (or children if a parent object_id is mentioned) and execute the code for that instance. Here you loop over all the objects, and if 'variable' in that object is equal to somv value x then it will do whatever you place instead of ///draw line here
 

chamaeleon

Member
There already is a loop over all instances. The game frame loop.
Draw event
GML:
if (somevar == some value) {
    // draw line
}
 

Misael644

Member
Well, that code seems to work, but that's not what I meant. I don't think you really understood what I meant (or maybe I didn't). So, here's the explanation of my original code:

GML:
//obj_dot draw event:

var Inst1 = instance_find(obj_dot, DotID);
var Inst2 = instance_find(obj_dot, DotID+1);

if DotID > 0 {
    draw_line_colour(Inst1.x,Inst1.y,Inst2.x,Inst2.y,c_lime,c_lime);
};
This is how the obj_dot position is organized:

GML:
//obj_dot create event:

y = room_height/2 - (obj_ship.speed*10);
hspeed = -1;
DotID is defined by these obj_game events:

GML:
//obj_game create event:

alarm[0] = room_speed;
DotCounter = 1;

var Inst = instance_create(640, room_height/2, obj_dot);
Inst.DotID = 0;
GML:
//obj_game alarm[0] event:

var Inst = instance_create(640, room_height/2, obj_dot);
Inst.DotID = DotCounter;

DotCounter += 1;

alarm[0] = room_speed;
What would be the most appropriate code for my explanation?
 

chamaeleon

Member
I'd just stuff obj_dot instances in an array (global variable or an instance variable of some instance there is only one of in the room) and store the index of an instance's position in this array in the instance. That way you can directly look up each instance by index, and get the next instance by adding one to the index of some given instance, etc.
 

Misael644

Member
I was already thinking about using arrays at the beginning of this project, the problem is that I was not able to program it properly. Now, after some research, I finally managed to program an array system to work with the line graphs that I talked about.

Here is the code I wrote:
GML:
//obj_game create event:

Count = 0;
yPos[] = 0;
SpawnRate = 1;
alarm[0] = SpawnRate+1;
alarm[1] = SpawnRate;

instance_create(room_width,room_height-32,obj_dot);

//obj_game alarm[0] event:

Count += 1;
alarm[0] = SpawnRate;

//obj_game alarm[1] event:

instance_create(room_width,room_height-32,obj_dot);
alarm[1] = SpawnRate;

//obj_game step event:

yPos[Count] = obj_ship.speed;

GML:
//obj_dot create event:

SpawnRate = obj_game.SpawnRate;
yPos = obj_game.yPos;
Count = obj_game.Count;

//obj_dot step event:

x -= 1;
if x < 0 {instance_destroy()};

//obj_dot draw event:

if sprite_index == spr_dot{draw_self()};

if Count > 0 {
    draw_line_colour(x-SpawnRate,y-(yPos[Count-1]*10),x,y-(yPos[Count]*10),c_red,c_red);
};
Everything working fine, without any error and with stable FPS. Thanks a lot for the help! :D
 
Top