Help with enemies's position

D

DarlesLSF

Guest
How can I make the cursor jump to the next enemy checking if the next enemy its alive or not?
I saw a stream where the guy used ds_list to do that I want, but doesnt explain how this works :/

 

Slyddar

Member
You could just use the with function, and a hp check. I don't know how your cursor selects an enemy, but however you do it, within the with, assign the cursor to that enemy if hp > 0.
 

CloseRange

Member
I'd just use an array.
in your controller object, or some player object, or an object that's in the combat room that isn't an enemy:

Code:
enemys = [];
enemy_length = 0;
with(obj_enemy) {
    other.enemys[enemy_length++] = id;
}
cursor = 0;
then when you want the cursor to go up and down:

Code:
/// move up
var found = false;
while(!found) {
    cursor--;
    if(cursor < 0) cursor = enemy_length-1;
    if(enemys[cursor].hp > 0) found = true;
}

// down
var found = false;
while(!found) {
    cursor++;
    if(cursor >= enemy_length) cursor = 0;
    if(enemys[cursor].hp > 0) found = true;
}
 
D

DarlesLSF

Guest
I'd just use an array.
in your controller object, or some player object, or an object that's in the combat room that isn't an enemy:

Code:
enemys = [];
enemy_length = 0;
with(obj_enemy) {
    other.enemys[enemy_length++] = id;
}
cursor = 0;
In that case, my enemy's objects call obj_inimigo1, obj_inimigo2 and obj_inimigo3. I only need to change the word "enemy" with the word "inimigo"?
 
Store the ID's of an enemy in a ds_list and then when you go to select a new target just search through the list and check the hp of each instance stored, only picking an enemy that has hp above 0.
 
D

DarlesLSF

Guest
Store the ID's of an enemy in a ds_list and then when you go to select a new target just search through the list and check the hp of each instance stored, only picking an enemy that has hp above 0.
How I do that? I already stored the ID's of the enemies, but I dont know how to search these ID's in the ds_list.
 

hippyman

Member
ds_list_find_index would be a way to find a value's position.

But I'm assuming you have the cursor pointing at a certain instance based on an index. Could you not just increment the index and it would point to the next enemy in the list?
 
D

DarlesLSF

Guest
ds_list_find_index would be a way to find a value's position.

But I'm assuming you have the cursor pointing at a certain instance based on an index. Could you not just increment the index and it would point to the next enemy in the list?
I just set the position of the cursor based in the x,y of the enemies's sprite.
that's my code to create the enemies (inimigo = enemy):
Code:
if (global.num_inimigos == 1)
{
    instance_create(384,192, obj_inimigo1);
    instance_destroy();
} else
if (global.num_inimigos == 2)
{
    instance_create(384,128, obj_inimigo1);
    instance_create(391,251, obj_inimigo2);
        instance_destroy();
} else
if (global.num_inimigos == 3)
{
    instance_create(358, 129, obj_inimigo1);
    instance_create(424, 197, obj_inimigo2);
    instance_create(343,267, obj_inimigo3);
        instance_destroy();
}
then I made an array to the menu options:
Code:
mBatalha[0] = "atacar"; (attack)
mBatalha[1] = "magia"; (spells)
mBatalha[2] = "itens"; (items)
mBatalha[3] = "fugir"; (escape)
and here its the code to position the cursor (its an object too) in front of the first enemy starting from the top:
Code:
if (keyboard_check_pressed(ord("F"))) && (mBatalha == 0) // atacar (attack option)
{   
    audio_play_sound(escolher_menu, 1, false)
    if (global.num_inimigos == 1) && (global.hp_inimigo1 > 0) // 1 inimigo (1 enemy)
    {
        obj_cursorbatalha.x = obj_inimigo1.x - 70;
        obj_cursorbatalha.y = obj_inimigo1.y - 10;
        inimigo_atual = obj_inimigo1;
        escolhendoinimigo = true;
        menubatalha = false;
    } 
   
    if (global.num_inimigos == 2) // 2 inimigos (2 enemies)
    {
        if (global.hp_inimigo1 > 0) 
        {
        obj_cursorbatalha.x = obj_inimigo1.x - 70;
        obj_cursorbatalha.y = obj_inimigo1.y - 10;
        inimigo_atual = obj_inimigo1;
        escolhendoinimigo = true;
        menubatalha = false;
        }
        else
        {
        if (global.hp_inimigo2 >0)
        obj_cursorbatalha.x = obj_inimigo2.x - 70;
        obj_cursorbatalha.y = obj_inimigo2.y - 10;
        inimigo_atual = obj_inimigo2;
        escolhendoinimigo = true;
        menubatalha = false;
        }
    }
    if (global.num_inimigos == 3) // 3 inimigos (3 enemies)
    {
        if (global.hp_inimigo1 > 0)
        {
        obj_cursorbatalha.x = obj_inimigo1.x - 70;
        obj_cursorbatalha.y = obj_inimigo1.y - 10;
        inimigo_atual = obj_inimigo1;
        escolhendoinimigo = true;
        menubatalha = false;
        }
        else
        {
        if (global.hp_inimigo2 >0)
        {
        obj_cursorbatalha.x = obj_inimigo2.x - 70;
        obj_cursorbatalha.y = obj_inimigo2.y - 10;
        inimigo_atual = obj_inimigo2;
        escolhendoinimigo = true;
        menubatalha = false;
        }
        else
        {
        if (global.hp_inimigo3 >0)
        obj_cursorbatalha.x = obj_inimigo3.x - 70;
        obj_cursorbatalha.y = obj_inimigo3.y - 10;
        inimigo_atual = obj_inimigo3;
        escolhendoinimigo = true;
        menubatalha = false;
        }
    }
I know I can check everytime the hp of the enemies, but compared with the method using arrays or ds_lists its too much work. And will be good to learn this method, because I'm starting now on GM haha
 
Top