Legacy GM Chain lightning attack script

marasovec

Member
I'm trying to make a script to find objects to hit with a chain attack but it doesn't seem to work

Code:
///find_chain(object, chain_length, max_distance)

var obj = argument0;
var chain_length = argument1;
var max_dist = argument2;

var nearest_obj = id;
var objects = ds_list_create();

var total = 0;
var arr;

for(var i = 0; i < instance_number(obj); i++) { ds_list_add(objects, instance_find(obj, i)); }

repeat(chain_length)
    {
    var nearest_dist = max_dist;
    var nearest_index = 0;
  
    for(var i = 0; i < ds_list_size(objects)-1; i++)
        {
        var current_obj = objects[| i];
        var dist = point_distance(nearest_obj.x, nearest_obj.y, current_obj.x, current_obj.y);
      
        if dist < nearest_dist
            {
            nearest_dist = dist;
            nearest_index = i;
            }
        }
  
    if nearest_dist < max_dist
        {
        nearest_obj = current_obj;
        ds_list_delete(objects, nearest_index);
        arr[total++] = nearest_obj;
        }
    }

ds_list_destroy(objects);
return arr;
 
C

Catastrophe

Guest
[deleted me being dumb]

Side point, one mistake is you want ds_list_size(objects) not ds_list_size(objects)-1. Off by one error yay.

Edit: OH you're doing chain lightning. Whoops, thought you just meant a chain attack xD looking again. It seems like there's only the off by one error above, but I'll let you know if I find anything else. Let us know what is going wrong, though, please.

Also, this:

with (obj) {ds_list_add(objects,id);}

is a much cleaner way of doing this

for(var i = 0; i < instance_number(obj); i++) { ds_list_add(objects, instance_find(obj, i)); }

no "other." necessary as you var'd the variable.
 
Last edited by a moderator:

marasovec

Member
Oh, yeah, right, sorry
As you can see it's returning wrong objects. The problem is that it's not looking for the nearest ones. Distance between these objects are much greater than the max_dist so I have no idea why they were added to the array
 
Last edited:
C

Catastrophe

Guest
Ah, here is your issue:


nearest_obj = current_obj;
ds_list_delete(objects, nearest_index);
arr[total++] = nearest_obj;

->

nearest_obj =objects[| nearest_index];
ds_list_delete(objects, nearest_index);
arr[total++] = nearest_obj;

It was adding the last object in each for loop, not the shortest distance one.
That said, there is also the off by one error to fix if you haven't.
 

marasovec

Member
This script doesn't work on GM: Studio 2
That's because the code above is broken

Here's the fixed version
Code:
///find_chain(object, chain_length, max_distance)

var obj = argument0;
var chain_length = argument1;
var max_dist = argument2;

var nearest_obj = id;
var objects = ds_list_create();

var total = 0;
var arr; arr[0] = noone;

with(obj) { ds_list_add(objects, id); }

repeat(chain_length)
    {
    var nearest_dist = max_dist;
    var nearest_index = 0;
   
    for(var i = 0; i < ds_list_size(objects); i++)
        {
        var current_obj = objects[| i];
        var dist = point_distance(nearest_obj.x, nearest_obj.y, current_obj.x, current_obj.y);
       
        if dist < nearest_dist
            {
            nearest_dist = dist;
            nearest_index = i;
            }
        }
   
    if nearest_dist < max_dist
        {
        nearest_obj = objects[| nearest_index];
        ds_list_delete(objects, nearest_index);
        arr[total++] = nearest_obj;
        }
    }

ds_list_destroy(objects);
return arr;
 
Top