GameMaker and coding noob here, need help on something maybe trivial for some of you.
I'm working on the map part of a game. I have a map with spots on it, each spot is an instance of the same object.
I need to draw lines connecting the current spot and the close ones given a maximum distance (100 pixel radius). Also, I need to remove those lines that cross each other, and to keep track of which lines I already traveled.
I made an object ctr_obj
that moves (on mouse click) from one spot to another if they are distant less than 100 pixels.
Create Event code :
smp = instance_nearest(x,y,map_spot)
x = smp.x;
y = smp.y;
Draw Event code :
for (i=0; i<instance_number(map_spot); i+=1)
{
mapSpot[i] = instance_find(map_spot,i);
near[i] = instance_nearest(x,y,map_spot)
if point_distance(mapSpot[i].x,mapSpot[i].y,near[i].x,near[i].y)<100
{
draw_line(mapSpot[i].x,mapSpot[i].y,near[i].x,near[i].y)
}
}
This part works as intended, I got the line drawn between the spot object I'm currently in and the other closest spot.
Ideally, I need to have all the lines of the map drawn all the time, not only when my ctr_obj
is on a spot, and to keep track of the already traveled line (by changing its color, for example).
I tried to do something like that directly on the draw event of the map_spot
object.
Draw Event code :
for (i=0; i<maxnumber; i+=1)
{
inst[i]= collision_circle(x,y,100,map_spot,false,false);
if inst[i] !=noone
{
inst[i].x +=1000000;
}
}
for (i=0; i<maxnumber; i+=1)
{
if inst[i] !=noone
{
inst[i].x-=1000000;
}
}
for (i=0; i<maxnumber; i+=1)
{
draw_set_color(c_gray);
draw_line(x, y, inst[i].x, inst[i].y)
if done=1 && inst[i].done=1
{
draw_set_color(c_lime)
draw_set_alpha(0.8)
draw_line(x, y, inst[i].x, inst[i].y)
}
}
It was the only way I found to be able to get the instance_ID of multiple instances of the same object around a point. But as the event run on all map_spot
I can't find a way to keep track of everything.
done
is a variable to check if I already got on a particular spot. It doesn't really work as intended as if there are three map spots close enough each other I get a green triangle and not just the path used. And some paths don't get draw at all.
Can someone give me an hint?
EDIT 1 : So I try the solution, but something is not working. based on the solution provided, this is what I've done :
the script i use :
///scr_find_near(dist,object)
var list = ds_list_create();
with (argument1)
{
if (point_distance(x,y,other.x,other.y) < argument0)
ds_list_add(list,other.id);
}
return list;
the draw_event code of the map_spot object :
closeList = scr_find_near(300,map_spot);
nb_near= ds_list_size(closeList)-1;
for (i = 0; i < nb_near; i += 1)
{
mp[i] = ds_list_find_value(closeList,i);
draw_set_alpha(1);
draw_set_color(c_gray);
draw_line(x,y,mp[i].x,mp[i].y);
info = string("nb of close point"+string(nb_near));
}
the "nb of close point" is just used as a debug for now (i have an "info" tool-tip for debug on all my objects). i try at first with mp as a "normal" variable, not an array, but actually nothing get draw. i can see that each point got the good number of "nb of close point" with the tool-tip, so the ds_list got the good number of info stored in.
but i don't have any line between the map_spot. I'm pretty sure i missed something obvious at this point, i spend too much time on that and can can't see clearly what i need to do. the script you give me work perfectly, but i think i made a mistake in my way of trying to use it to draw the line...
EDIT 2 I continue my trial and error, with the code below on the map_spot draw_event :
with (map_spot){
if point_distance(x,y,other.x,other.y)<300
{
draw_set_color(c_gray)
draw_line(x,y,other.x,other.y);
if (done=1 && other.done=1)
{
draw_set_color(c_lime);
draw_line(x,y,other.x,other.y);
} } }
All the line between close spot are now correctly draw. That's a win. But i got my initial issue with keeping track of witch line have been traveled thru. Obviously, with the code above, all the line between map_spot that have been visited are now green, not just the one i really "used".
the red number are the order of the map_spot i click on the map. the orange X mark the line that i don't want to be green.
I need to try with a global variable to keep track of the number of movement (i will need to keep track of that for other reason) maybe it can help me draw only the green line i want.
already thx for the help, i'm closer than before to make it work !
EDIT 3 :
I manage to get it to work, not with a DS_grid, i use a simple way (for me at least). I already had an invisible ctr_object that move when i clic on a node where i can go (checking distance) this object "ctr_map_position" had multiple usefull variable for my game map ; the ctr_map_position step event :
if (x != xprevious && y != yprevious)
{
moveCount+=1;
parSec += 0.1*point_distance(x,y,xprevious,yprevious)
}
with that i keep track of the number of move(moveCount) and the total distance traveled in space (parSec). at each map_spot, i transfert the current moveCount value to a variable named "place" on the map spot, witch tell me the position of the map spot in the journey. (1,2,3,4 etc..) Finally I draw green line only between map_spot with variable 'place' = n and n+1
everything is now working as i intended to, so thx a lot for the help. obviously a ds_grid or an array like you say may be more optimized but i'm still learning with this project, and optimization is not currently my priority ! (even if i try to do think the best way possible)