Often in many types of games you will find yourself with lots of instances of the same kind of object. It might be tricky to compare them, as you can’t do it manually with individual code you must find a mechanism which can do it for you. This script which I am about to show is customizable so that you may check all different values and sort them in all kinds of ways, which might be very useful in advanced games like RTS and MMORPG where a lot of instances exists.
My example script picks out the ten closest instances of one type of object. This code could for an example be put in a character to sort out which enemies are the closer ones.
<div style="clear: both;">//Initializing
i = 0;
obj_distance[instance_number(object0)] = 0;
//Order by distance
while(i<instance_number(object0)) {
inst = instance_find(object0,i);
dist = distance_to_object(inst);
obj_distance[dist] = inst;
i +=1;
}
//Does a bit intializing
i = 0;
nr = 0;
list[instance_number(object0)];
inst_f = instance_furthest(x,y,object0);
dist_f = distance_to_object(inst_f);
//Arranging it into a list
while (i<dist_f) {
if (obj_distance[i]>0) {
list[nr] = obj_distance[i];
nr +=1;
}
i+=1;
}</div>
Now as you can see this is done in two steps. First arrange them by distance, which will create spaces in the array, and then remove the spaces. The final product is a list kept in the variable array list. The higher the index number, the further the object is. List[0] returns the closest instance of type object0 and list[instance_number(object0)] returns the furthest.
You must understand the code piece by piece, so that you may construct your own conditions next time. To help you out; first code creates an array with the distance linked to the instance ID. It basically looks like this obj_distance[245] = 100034, obj_distance[355] = 100036 etc. now the next loop goes through that array and puts them after size: list[0] = 100034, list[1] = 100036 etc.
You may ask questions by commenting and you may use this code as it is, but we’d be happy if we got credits for it.
.
Users logged in:
Comments
Loading comments...