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

Legacy GM [SOLVED]Enemy target list

Magicwaterz

Member
Hello. I am redoing my tower defense game yet again. I am currently having issues with my enemy target list as it as that outlier in the list. What this code does it lists enemies that are within its range, has an effectiveness between the target and the attacker not equal to 0, etc. I am doing this code because I have instances that can hit multiple targets and that a simple 'distance' code would not suffice. This is currently the code.

Code:
///scr_Foe_Detection(Parent Target, Sort Type);
var Target, Sort;
Target = argument0;
Sort = argument1;//Sort Types 0-Nearest, 1-Furthest, 2-Low HP

//Find all enemy instances
for (i = 0; i < instance_number(Target); i++) {
    I_Target[| i] = instance_find(Target, i);
}

//Resize the Grid
ds_grid_resize(T_Info, 4, instance_number(Target));

//Identify ID, distance, current HP, and the target's Attribute
for (i = 0; i < instance_number(Target); i++) {
    T_Info[# 0, i] = I_Target[| i];
    T_Info[# 1, i] = distance_to_object(I_Target[| i]);
    T_Info[# 2, i] = I_Target[| i].HPM;//Change to HP
 
    //Check Attribute Effectiveness
    var At1, At2;
    At1 = ds_list_find_index(DT_Types, Typ); //Own Attribute
    At2 = ds_list_find_index(DT_Types, I_Target[| i].Typ); //Target's Attribute
     
    T_Info[# 3, i] = DT_TypeEff[# At1, At2];// It returns the effectiveness either 1, 0.5, 2, or 0
}

//Sorts the grid depending on the sorting type
switch(Sort) {
    case 0: ds_grid_sort(T_Info, 1, true); break; //targets nearest enemy
    case 1: ds_grid_sort(T_Info, 1, false); break; //targets furthest enemy
    case 2: ds_grid_sort(T_Info, 2, true); break; //targets enemy with low HP
}

//Create finalized list where in it only lists targets within range
//and having an effectiveness not equal to 0
for (i = 0; i < instance_number(Target); i++) {
    if instance_exists(T_Info[# 0, i]) {
        if T_Info[# 1, i] <= Ran {
            if T_Info[# 3, i] != 0 {
                L_Target[| i] = T_Info[# 0, i];
            }
        }
    }
}
Everything is fine when I checked the list of enemies in my prototype but when I added an enemy (a ghost entity) that is within range in the room, it is still listed but without its id and instead is a 0. What I wanted to do is to let it skip this instance and replace it with the next one that is still within range.

EDIT:
I added a screenshot to show what I mean. There are 3 zeroes in the list and I want these not to be listed and be replaced with the next ones within the list.
Screenshot_7.png

EDIT 2:
Nevermind. I solved it now. I simply added ds_list_clear at the beginning then added another block of code by implementing noone into it like this.

Code:
var Target, Sort;
Target = argument0;
Sort = argument1;//Sort Types 0-Nearest, 1-Furthest, 2-Low HP

ds_list_clear(L_Target);

if instance_exists(Target) {
    //Find all enemy instances
    for (i = 0; i < instance_number(Target); i++) {
        I_Target[| i] = instance_find(Target, i);
    }
    
    //Resize the Grid
    ds_grid_resize(T_Info, 4, instance_number(Target));
    
    //Identify ID, distance, current HP, and the target's Attribute
    for (i = 0; i < instance_number(Target); i++) {
        T_Info[# 0, i] = I_Target[| i];
        T_Info[# 1, i] = distance_to_object(I_Target[| i]);
        T_Info[# 2, i] = I_Target[| i].HPM;//Change to HP
        
        //Check Attribute Effectiveness
        var At1, At2;
        At1 = ds_list_find_index(DT_Types, Typ); //Own Attribute
        At2 = ds_list_find_index(DT_Types, I_Target[| i].Typ); //Target's Attribute
            
        T_Info[# 3, i] = DT_TypeEff[# At1, At2];// It returns the effectiveness either 1, 0.5, 2, or 0
    }
    
    //Sorts the grid depending on the sorting type   
    switch(Sort) {
        case 0: ds_grid_sort(T_Info, 1, true); break; //targets nearest enemy
        case 1: ds_grid_sort(T_Info, 1, false); break; //targets furthest enemy
        case 2: ds_grid_sort(T_Info, 2, true); break; //targets enemy with low HP
    }
    
    //Create finalized list where in it only lists targets within range
    //and having an effectiveness not equal to 0
    for (i = 0; i < instance_number(Target); i++) {
        if instance_exists(T_Info[# 0, i]) {
            if T_Info[# 1, i] <= Ran {
                if T_Info[# 3, i] != 0 {
                    L_Target[| i] = T_Info[# 0, i];
                }
                else
                {
                    L_Target[| i] = noone;
                }
            }
        }
    }
    
    for (i = 0; i < ds_list_size(L_Target); i++) {
        if L_Target[| i] == noone {
            ds_list_delete(L_Target, i);
        }
    }
}
 
Last edited:
Top