Update on my combat system

flyinian

Member
To update everyone...

Here is the code I've been working on.


It appears to work as intended.


I am able to:


1. Have it randomly select a target from a grid.

2. Apply damage to target.

3. Continue applying damage to targets until all enemies are dead or the damage is used up.

4. Probably break on me after I make this post. :)


If you see any issues with the code below, let me know.


Thank you.

GML:
if(keyboard_check_pressed(ord("T")))        // Used for debugging

{

    attack = true;

}

else

{

    attack = false;

};





if(attack)        // Used for debugging

{



    show_debug_message("Total Health begin: " + string(ds_grid_get(enemy,6,11)));    // Used for debugging

    while (damage > 0 && ds_grid_get_sum(enemy,1,1,1,10) > 0)        // While there is damage and health

    {

    

        show_debug_message(" ");                            // Used for debugging

        show_debug_message("***!Attack Begins!***");        // Used for debugging

        show_debug_message(" ");                            // Used for debugging





        var range = irandom_range(1,10);            // sets the range for target selection

        var target = ds_grid_get(enemy,1,range);    // Establishes the selected target





        show_debug_message("Beginning Damage: " + string(damage));            // Used for debugging

        show_debug_message("Grid Number Rolled: " + string(range));            // Used for debugging

        show_debug_message("Target Found(Health): " + string(target));        // Used for debugging





        if (target >= damage)   

        {   

            show_debug_message("__Target Has **More** Health Than Damage__");        // Used for debugging

        

            target -= damage;                        // Applies the damage to the target's health.

            damage -= damage;                        // Forces the damage to be 0 because all of it has been used.

            ds_grid_set(enemy,1,range,target);        // Updates the grid with the new values



            show_debug_message("Target's Health After Damage Applied: " + string(target));        // Used for debugging

        }

        else

        {

            show_debug_message(" ");                                                // Used for debugging

            show_debug_message("__Target Has **Less** Health Than Damage__");        // Used for debugging

            show_debug_message("The Equation: Beginning Damage - Target's Health = Difference. Difference - Beginning Damage = Temporary Damage. Apply Temporary Damage To Target's Health. New Damage = Difference");    // Used for debugging





            difference = damage - target;            // Finds the difference.

            tempdamage = damage - difference;        // Find the damage to be applied to target's health without going over.

            damage = difference;                    // Sets damage to the difference to account for damage usage on target.

            target -= tempdamage;                    // Subtracts the temporary damagefrom the target's health

            ds_grid_set(enemy,1,range,target);        // Updates target's health within the grid.

        

        

            show_debug_message("Difference: " + string(difference));            // Used for debugging

            show_debug_message("Temporary Damage: " + string(tempdamage));        // Used for debugging

            show_debug_message("Target's Health: " + string(target));            // Used for debugging

            show_debug_message("New Damage: " + string(damage));                // Used for debugging

        

            

        

        };

    

        if (damage <= 0)

        {

            show_debug_message("out of damage");        // Used for debugging

        };

    

    

        // setting or "updating" the total health region, prevents an infinite loop. This occurs when there is more health than damage. Has to do with how the damage and health respond to each other.

        ds_grid_set(enemy,6,11,ds_grid_get_sum(enemy,1,1,1,10));        // Updates the total health in the given region. used to prevent an infinite while loop when total health exceeds total damage.

        attack_count += 1;                                                // Keeps tracks of number of attacks performed

    

    

        show_debug_message("Total Health Sum: " + string(ds_grid_get_sum(enemy,1,1,1,10)));        // Used for debugging

        show_debug_message("attack count: " + string(attack_count));                            // Used for debugging

        show_debug_message(" ");                                                                // Used for debugging

        show_debug_message("***!Attack End!***");                                                // Used for debugging

        show_debug_message(" ");                                                                // Used for debugging

    };

};



I am planning on taking this step by step and getting the code to work before moving to the next section. Hopefully, this helps to break it down so, it's easier to understand what is going on.


I am currently trying to have the list to check if it has any dead enemies and if it does, add those dead enemies to the "deadenemylist" and delete it from the "enemylist". it runs but, I don't think it returns the correct values.

I am starting with this part of the code because from my understanding, it would be best to check if any enemies are alive before doing anything else.

I'm not using instances for the enemies. I have a ds map that holds the information of the enemy.

GML:
// Enemy exists variables
var enemylist = ds_list_create();                                    // creates the given ds list, list for living enemies
ds_list_add(enemylist,"Count 1", "Count 2", "Count 3");                // add to the ds list
var find_enemy = ds_list_find_value(enemylist, i);                    // finds the value for the given ds list position
var enemy_target = EnemyMap[? find_enemy];                            // allows the ds map to be used/changed
var no_enemies = ds_list_empty(enemylist);                            // creates the control variable for enemy existance
var list_size = ds_list_size(enemylist) - 1;                        // gets the list size, and minuses one - as indexes start at zero
var list_random_pos = irandom_range(0, list_size);                    // sets the random range to between zero and end size
var enemy_Count = ds_list_find_value(enemylist, list_random_pos);    // finds the value at that index position (the returned number of list_random_pos)
var dead_enemy = ds_list_find_index(enemylist,0);                    // holds the dead enemy value

// Enemy doesn't exist variables
var deadenemylist = ds_list_create();                                            // creates the given ds list, list for dead enemies
var dead_list_size = ds_list_size(deadenemylist) - 1;                            // gets the list size, and minuses one - as indexes start at zero
var dead_enemy_count = ds_list_find_value(deadenemylist, dead_list_size);        // finds the value at that index position (the returned number of dead_list_size )


// here is the logic I created for the code. i'll be changing it overtime.

//        1. Need to initialize the lists.    Check.
//        2. Need to create the variables for controlling the list selection and such.        May need work.
//        3. Need to check to see if the EnemyList has any enemies. If it does, it needs to check to see if any of them holds a health of zero or less.
//        4. If any enemy in enemylist has a health of zero or less, delete it from enemylist and add it to deadenemylist.
//        5. Need to check to see if the deadenemylist has any enemies with a health of 1 or more.
//        6. If any enemy in deadenemylist has health 1 or more, add it to the enemylist and delete it from deadenemylist.
//        7. Once the updated Enemylist has been established, randomize the list and select an enemy.
//        8. Once an enemy has been selected, run the check to see if the damage is more than the enemy health.
//        9. if the damage is more than the enemy health, apply what is needed to kill the enemy and save the remaining damage.
//        10. run through the randomizing and selection process again to obtain a new enemy, check if the damage is greater than enemy health, if not apply the damage. if it was, repeat step 7.
//        11. If there are no enemies to apply damage to, delete the damage and display a no enemies to attack message.


// below, I am checking "enemylist" for any dead enemies. if any exists it will add them to "deadenemylist" and deletes them from "enemylist".
{
do                                                                                    // do the following
{
if(ds_list_find_index(enemylist,dead_enemy))                                        // if an enemy with a health of 0 is found in enemy list
{
ds_list_add(deadenemylist,dead_enemy);                                                // add the dead enemy to the deadenemylist
ds_list_delete(enemylist,dead_enemy);                                                // delete the dead enemy from the enemylist
}
}
until dead_enemy == -1;                                                                // until all dead enemies have been removed from enemylist and added to deadenemylist

show_debug_message("dead_enemy: " + string(dead_enemy));                            // returns -1, this is correct since it at the end of the code so, it'll return -1
show_debug_message("enemylist size: " + string(list_size));                            // returns 2, this should be 0 because, enemies start with 0 health so every enemy is dead.
show_debug_message("deadenemylist: " + string(ds_list_size(deadenemylist)));        // returns 0, this should be 3 because, there should be 3 dead enemies.

};







----------------------------------------------------------------------------------





After going through tutorials and forum threads, I managed to make some progress on my combat system.

I have the randomization working so far with high hopes that it won't break or become useless to me because it doesn't do what I want it to.

Now, I need to check the ds_list "enemylist" if any of the enemies have a value of 0 which means they are dead or have no units to fight. This is where I need the help.

if an enemy returns a value of "0", then it will be deleted from the "enemeylist" for now. later, I'll be adding another list "deadenemylist" to store dead enemies just in case they gain health/units once again.


I've tried using ds_list_find_value/index in an if statement and haven't had any luck.

As i was creating this post, I thought about making the "deadenemylist" and if a position has a value greater than 0, then add that position to "enemylist". I guess in other words, making the "deadenemylist" a controler list that dictates if the enemy is added to the "enemylist" or not. Would this work?

NOTE: EnemyList[? enemy_key1] is a ds map while enemylist is a list.

GML:
// randomization combat

// creation of ds lists
var enemylist;
var deadenemylist
enemylist = ds_list_create();
deadenemylist = ds_list_create();
ds_list_add(enemylist,"Count 1", "Count 2", "Count 3");
var i = 0;
var enemy_key1 = ds_list_find_value(enemylist, i);
var enemy1 = EnemyList[? enemy_key1];
var noenemy = false;


var size = ds_list_size(enemylist) - 1;
var list_range = irandom_range(0, size);
var value = ds_list_find_value(enemylist, list_range);
ds_list_add(deadenemylist, value);



//go through living enemy and find a target
for ( i = 0; i < size; ++i)
{


if(ds_list_size(enemylist >= 0)) // if enemy are in the list
{
    list_range = irandom_range(0, size); //randomizes the enemy list
    value = ds_list_find_value(enemylist, list_range);    // find a target in the list

    show_debug_message("target found");
    show_debug_message("value: " +string(value));
    enemy_key1 = value;            // sets the selected target as the target for damage to be applied to.
    show_debug_message("enemy_key1: " +string(enemy_key1));
    show_debug_message("target set: " + string (enemy_key1));

/*if(ds_list_find_value(enemylist,value) < ds_list_find_index(enemylist,0))   // i've done this and various versions of this and only get errors.
{
ds_list_delete(enemylist, value);
};*/

};

};


//if no enemies
if(ds_list_empty(enemylist))
{

    noenemy = true;
    show_debug_message("Enemy list is empty");

};


if(noenemy = true){
show_debug_message("no enemy numbers to deduct");
exit;
}
else
{
show_debug_message("Enemy exists");

enemy1 -= EnemyList[? "totalcountdifference"];
EnemyList[? enemy_key1] = enemy1;
show_debug_message("target attacked: " + string (enemy_key1));
show_debug_message("Enemy Count adjusted");

};
};
};
-------------------------------

So, I have cleaned up my code and continued developing it. Made little progress.

I am still looking for help with the code to get it working how I'd like it to.

There is some code not shown and it is used for calculating troops numbers(health) and their stats. that code appears to work fine.

so far I've managed to get working:

1. I've managed to get it to randomly select an enemy.
2. I believe I got it to add an enemy if it has a value of zero health to the "deadenemylist".(died)
3. I believe I got it to add an enemy to the "enemylist" if it has more than zero health.(revived)


Issues I currently have with my combat system:

1. I have an issue where it appears that it still subtracts health from an enemy count even though that enemy has no health. This may be due to the code not shown. Or, it randomly selects a dead enemy and still subtracts health from it which effects my other variables that keep track of the combined health across all enemies.

2. In the debugger, I have the "enemylist" and "deadenemylist" show their count and it continuously increases as enemies die/revive. Would this be an issue or is that just how it calculates it? There are only 3 enemies, so that number should be split between "enemylist" and "deadenemylist" and should not exceed(?) 3 but, it'll continuously increase over 3. I usually see it increase by 2's.

3. if the enemy health is less than the damage applied, i want it to apply the remaining damage to a randomly selected enemy. This may be done using my non shown code and used a if statement?..


NOTE: EnemyList[? enemy_key1] is a ds map while enemylist is a list.

GML:
//enemies exist variables
var enemylist = ds_list_create();                      
ds_list_add(enemylist,"Count 1", "Count 2", "Count 3");  
var i = 0;                                             
var enemy_key1 = ds_list_find_value(enemylist, i);      
var enemy1 = EnemyList[? enemy_key1];                  
var noenemy = ds_list_empty(enemylist);                 
var size = ds_list_size(enemylist) - 1;                 
var list_range = irandom_range(0, size);             
var value = ds_list_find_value(enemylist, list_range); 



// no enemy exist variables
var deadenemylist = ds_list_create();                
var zz = 0;                                              
var dsize = ds_list_size(deadenemylist) - 1;        
var dvalue = ds_list_find_value(deadenemylist, dsize);  




if(!noenemy)    //there are enemies in the list
{

    for ( i = 0; i < size;  i++)
    {
        list_range = irandom_range(0, size);                         
        value = ds_list_find_value(enemylist, list_range);             
  


    if(ds_list_find_index(enemylist,value) <= 0)
    {
    ds_list_add(deadenemylist, 0);
    ds_list_delete(enemylist, 0);


    };

  
        enemy_key1 = value;                                              
  

        enemy1 -= EnemyList[? "totalcountdifference"];                
        EnemyList[? enemy_key1] = enemy1;                            

 
    
    }
}
else                                                                       //there aren't any enemies in the list
    {
        for ( zz = 0; zz < dsize;  zz++)
        {
            dvalue = ds_list_find_value(deadenemylist, dsize);            
    
            if(ds_list_find_index(deadenemylist,dvalue) > 0)
            {
            ds_list_add(enemylist,dvalue);
            ds_list_delete(deadenemylist,dvalue);
            };
        }
    };
        };
            };
 
Last edited:

ophelius

Member
There's definitely some code improvements I can suggest first before analyzing the logic:

You can declare a variable while assigning it, this cleans up code a bit:
Code:
var enemylist = ds_list_create();
var deadenemylist = ds_list_create();
This part here:
Code:
//go through living enemy and find a target
for ( i = 0; i < size; ++i)
{
   
   if(ds_list_size(enemylist >= 0)) // if enemy are in the list
   {
   ...
should be set with the if statement on the outside like below, because you don't need to go through the for loop if there aren't any enemies.
And you can just use the ds_list_empty(enemylist) once to set your noenemy variable, then check that variable instead of calling the function multiple times, also attaching an else for all the code that would apply if it's true:
Code:
var noenemy = ds_list_empty(enemylist);
if( !noenemy){ //there are enemies in the list

     for ( i = 0; i < size;  i++) //it should probably be i++, not ++i
     {
     ...
}
else{  //there aren't enemies in the list

}
It's a start, and maybe if you stare at cleaner more optimized code the solution might come to you.
Feel free to clean it and repost it, removing all that junk code. It's easier to analyze for us.
 

flyinian

Member
There's definitely some code improvements I can suggest first before analyzing the logic:

You can declare a variable while assigning it, this cleans up code a bit:
Code:
var enemylist = ds_list_create();
var deadenemylist = ds_list_create();
This part here:
Code:
//go through living enemy and find a target
for ( i = 0; i < size; ++i)
{
  
   if(ds_list_size(enemylist >= 0)) // if enemy are in the list
   {
   ...
should be set with the if statement on the outside like below, because you don't need to go through the for loop if there aren't any enemies.
And you can just use the ds_list_empty(enemylist) once to set your noenemy variable, then check that variable instead of calling the function multiple times, also attaching an else for all the code that would apply if it's true:
Code:
var noenemy = ds_list_empty(enemylist);
if( !noenemy){ //there are enemies in the list

     for ( i = 0; i < size;  i++) //it should probably be i++, not ++i
     {
     ...
}
else{  //there aren't enemies in the list

}
It's a start, and maybe if you stare at cleaner more optimized code the solution might come to you.
Feel free to clean it and repost it, removing all that junk code. It's easier to analyze for us.

Thanks for the information. It helped.

I have updated my post.
 

ophelius

Member
Thanks, that's a bit more readable.
Though the names you give your variables can be confusing to figure out:

list_range = irandom_range(0, size);
Maybe:
ListPos = irandom_range(0, size);
Because you're picking a random position in the list. list_range sounds like you're picking a range

value = ds_list_find_value(enemylist, list_range);
Maybe:
EnemyHealth = ds_list_find_value(enemylist, ListPos);
Because those are all enemy health values, correct?

What do those zz variables mean?

But those are just my suggestions, it's what's easier for you to understand.

Try and put as many comments as you can so we can follow your logic in english, guessing based on your variable names and code structure it's a bit harder.

Now all this code here looks fishy:
Code:
        //you're picking a random position in the list, let's say position 2
        list_range = irandom_range(0, size);

        //you're getting the value stored at position 2, let's say it's a 4
        value = ds_list_find_value(enemylist, list_range);          

       //now the If will evaluate ds_list_find_index(enemylist, value) first, which returns the position of that 4 if it finds a 4 in the list, otherwise it returns -1
      //so your following line translates to:
      // IF that position where a 4 is located happens to be at position 0 OR less( which means no position, -1) then add the value 0 to deadenemylist and delete the value at position 0 of enemylist
       if(ds_list_find_index(enemylist,value) <= 0)
       {
           ds_list_add(deadenemylist, 0);
           ds_list_delete(enemylist, 0);
If this is correct then let me know, otherwise you may need to study how ds_list_find_value and ds_list_find_index work.

I'm just going by a hunch, maybe this is exactly how you need it, I'll need more info maybe

Edit: Another confusing variable, you have a list called enemylist, and a grid called EnemyList. Call it EnemyGrid since it's not a list

Edit 2: I modified the end of my comment for that last If evaluation, please re-read if you haven't done so.
 
Last edited:

Nidoking

Member
value = ds_list_find_value(enemylist, list_range);

if(ds_list_find_index(enemylist,value) <= 0)
Are these two lines really consecutive? How do you expect to get a negative value when you search a list for a value that you just pulled out of that same list?

list_range = irandom_range(0, size);
You haven't updated size, so if you WERE ever deleting things from the list, you'd be reading off the end of the list and crashing your game.

var deadenemylist = ds_list_create();
var zz = 0;
var dsize = ds_list_size(deadenemylist) - 1;
var dvalue = ds_list_find_value(deadenemylist, dsize);
I don't know what you're expecting to get here either, since you've just created an empty list, and now you're trying to get a value from it.

I think you may need to take a step back from coding and actually plan what you're doing. Maybe draw some diagrams of where data comes from and where it goes. This may be one of those times when the fastest way to get a working method is to delete everything you have and start over.
 

flyinian

Member
@ophelius & @Nidoking , Thank you for the help. so, I've taken your advice and cleaned the code up and restarted.


I am planning on taking this step by step and getting the code to work before moving to the next section. Hopefully, this helps to break it down so, it's easier to understand what is going on.


I am currently trying to have the list to check if it has any dead enemies and if it does, add those dead enemies to the "deadenemylist" and delete it from the "enemylist". it runs but, I don't think it returns the correct values.

I am starting with this part of the code because from my understanding, it would be best to check if any enemies are alive before doing anything else.

I'm not using instances for the enemies. I have a ds map that holds the information of the enemy.

GML:
// Enemy exists variables
var enemylist = ds_list_create();                                    // creates the given ds list, list for living enemies
ds_list_add(enemylist,"Count 1", "Count 2", "Count 3");                // add to the ds list
var find_enemy = ds_list_find_value(enemylist, i);                    // finds the value for the given ds list position
var enemy_target = EnemyMap[? find_enemy];                            // allows the ds map to be used/changed
var no_enemies = ds_list_empty(enemylist);                            // creates the control variable for enemy existance
var list_size = ds_list_size(enemylist) - 1;                        // gets the list size, and minuses one - as indexes start at zero
var list_random_pos = irandom_range(0, list_size);                    // sets the random range to between zero and end size
var enemy_Count = ds_list_find_value(enemylist, list_random_pos);    // finds the value at that index position (the returned number of list_random_pos)
var dead_enemy = ds_list_find_index(enemylist,0);                    // holds the dead enemy value

// Enemy doesn't exist variables
var deadenemylist = ds_list_create();                                            // creates the given ds list, list for dead enemies
var dead_list_size = ds_list_size(deadenemylist) - 1;                            // gets the list size, and minuses one - as indexes start at zero
var dead_enemy_count = ds_list_find_value(deadenemylist, dead_list_size);        // finds the value at that index position (the returned number of dead_list_size )


// here is the logic I created for the code. i'll be changing it overtime.

//        1. Need to initialize the lists.    Check.
//        2. Need to create the variables for controlling the list selection and such.        May need work.
//        3. Need to check to see if the EnemyList has any enemies. If it does, it needs to check to see if any of them holds a health of zero or less.
//        4. If any enemy in enemylist has a health of zero or less, delete it from enemylist and add it to deadenemylist.
//        5. Need to check to see if the deadenemylist has any enemies with a health of 1 or more.
//        6. If any enemy in deadenemylist has health 1 or more, add it to the enemylist and delete it from deadenemylist.
//        7. Once the updated Enemylist has been established, randomize the list and select an enemy.
//        8. Once an enemy has been selected, run the check to see if the damage is more than the enemy health.
//        9. if the damage is more than the enemy health, apply what is needed to kill the enemy and save the remaining damage.
//        10. run through the randomizing and selection process again to obtain a new enemy, check if the damage is greater than enemy health, if not apply the damage. if it was, repeat step 7.
//        11. If there are no enemies to apply damage to, delete the damage and display a no enemies to attack message.


// below, I am checking "enemylist" for any dead enemies. if any exists it will add them to "deadenemylist" and deletes them from "enemylist".
{
do                                                                                    // do the following
{
if(ds_list_find_index(enemylist,dead_enemy))                                        // if an enemy with a health of 0 is found in enemy list
{
ds_list_add(deadenemylist,dead_enemy);                                                // add the dead enemy to the deadenemylist
ds_list_delete(enemylist,dead_enemy);                                                // delete the dead enemy from the enemylist
}
}
until dead_enemy == -1;                                                                // until all dead enemies have been removed from enemylist and added to deadenemylist

show_debug_message("dead_enemy: " + string(dead_enemy));                            // returns -1, this is correct since it at the end of the code so, it'll return -1
show_debug_message("enemylist size: " + string(list_size));                            // returns 2, this should be 0 because, enemies start with 0 health so every enemy is dead.
show_debug_message("deadenemylist: " + string(ds_list_size(deadenemylist)));        // returns 0, this should be 3 because, there should be 3 dead enemies.

};
 
Last edited:

ophelius

Member
Great, this makes it a lot more clear exactly what you're trying to achieve.
I'm assuming the variable declarations at the top are just for notes? Because you have stuff like:
var find_enemy = ds_list_find_value(enemylist, i);
but i isn't defined yet, but I know it'll be going in a for loop later.
But those step by step notes will help, if anyone doesn't help by tomorrow, I'll take another look and come up with some code examples. I'm too full of beer right now to think straight, haha
 

Nidoking

Member
The thing you're missing is a way to determine, given a list of... what are those strings supposed to represent anyway? ... you don't know how to tell whether an entry in that list corresponds to a dead enemy. At least, nothing in this post does that. ds_list_find_index will just tell you the position of that thing in the list, so... this will always kill the first enemy in the list, regardless of health. I'm really hoping you realized that you would need to actually refer to health at some point in all of this.

Instead of placeholder strings, you need to put something in the list that identifies the actual enemies. Every instance has an id variable that serves exactly that purpose.

Having put all of your enemies into the list, you can now loop over the list and figure out which ones are dead, using the method you'll have determined by this point. (Why are dead enemies in your enemy list to begin with? Shouldn't you have removed them or changed them into something else by this point?) Then... hang on... check if there are any dead enemies with health 1 or more? Do you have revival effects now? Why did you put the dead enemy in the dead enemy list if it wasn't dead? Is this a Monty Python skit? "I'm not dead! I feel fine!"

If you manage to end up with a list of all the living enemies, just shuffle the list once and go down the line applying damage until you're done. Randomization is expensive.

And for the love of memory, destroy the lists when you're done with them.

Got all that? Good. Because if the rest of your game is managed in a sensible fashion, you can probably eliminate all of it, everything you have there, and populate your enemylist with a with loop instead of whatever you're doing.
 

flyinian

Member
The thing you're missing is a way to determine, given a list of... what are those strings supposed to represent anyway? ... you don't know how to tell whether an entry in that list corresponds to a dead enemy. At least, nothing in this post does that. ds_list_find_index will just tell you the position of that thing in the list, so... this will always kill the first enemy in the list, regardless of health. I'm really hoping you realized that you would need to actually refer to health at some point in all of this.

Instead of placeholder strings, you need to put something in the list that identifies the actual enemies. Every instance has an id variable that serves exactly that purpose.

Having put all of your enemies into the list, you can now loop over the list and figure out which ones are dead, using the method you'll have determined by this point. (Why are dead enemies in your enemy list to begin with? Shouldn't you have removed them or changed them into something else by this point?) Then... hang on... check if there are any dead enemies with health 1 or more? Do you have revival effects now? Why did you put the dead enemy in the dead enemy list if it wasn't dead? Is this a Monty Python skit? "I'm not dead! I feel fine!"

If you manage to end up with a list of all the living enemies, just shuffle the list once and go down the line applying damage until you're done. Randomization is expensive.

And for the love of memory, destroy the lists when you're done with them.

Got all that? Good. Because if the rest of your game is managed in a sensible fashion, you can probably eliminate all of it, everything you have there, and populate your enemylist with a with loop instead of whatever you're doing.

My combat system allows for reinforcements which means if the player/enemy has a dead unit, it can be revived mid fight.

I have a map that holds the enemy stats. so, when the enemy is out of combat their stats will change along with their health, so there could be dead enemies at the start of a battle. To prevent wasting damage on dead enemies, I want to eliminate the dead enemies from the enemylist and store them in the deadenemylist in case they are revived. The player/enemies are able to add health and take away health during the fight. (health are the number of units, for clarification on that last part).

As for the loop, that sounds awesome. Hopefully I can get there sooner rather than later.

Thank you for the information, one day i'll be back here and be like, "that's so easy to do".
 

Nidoking

Member
Mid-fight, fine. Mid-step? Unlikely. The enemy won't be reviving in the middle of your loop. And you made local variables for both lists, so they'll be lost once this event or script ends. If you want to store them long-term, you'll have to put them in an instance somewhere.
 

flyinian

Member
Mid-fight, fine. Mid-step? Unlikely. The enemy won't be reviving in the middle of your loop. And you made local variables for both lists, so they'll be lost once this event or script ends. If you want to store them long-term, you'll have to put them in an instance somewhere.

okay, I was keeping it local for testing purposes and keeping things simple.


so, I was coding my for loop and ran the project and it crashed by computer and appears it corrupted the object that housed the code.... \(0_0)/
 

flyinian

Member
I had a file corruption so i restarted again and trying another approach.

I don't know where the code is breaking so step 2 doesn't show.



GML:
// Enemy list creation
enemy_list = ds_list_create();

// Create enemy 1 map
var enemy_map = ds_map_create();

// Store enemy 1 map values
enemy_map[? "health"] = 0;
// Add enemy 1 map to enemy list
ds_list_add(enemy_list,enemy_map);
ds_list_mark_as_map(enemy_list,-1);

// Create enemy 2 map
var enemy_map = ds_map_create();

// Store enemy 2 map values
enemy_map[? "health"] = 0;
// Add enemy 2 map to enemy list
ds_list_add(enemy_list,enemy_map);
ds_list_mark_as_map(enemy_list,-1);

// Create enemy 3 map
var enemy_map = ds_map_create();

// Store enemy 3 map values
enemy_map[? "health"] = 0;
// Add enemy 3 map to enemy list
ds_list_add(enemy_list,enemy_map);
ds_list_mark_as_map(enemy_list,-1);

GML:
var i = 0;
var enemy_map = enemy_list[| i];
var hp = enemy_map[? "health"];
show_debug_message("running");
for (i = 0; i < list_size; i++)
{show_debug_message("step 1");
    if(ds_map_find_value(enemy_map,hp) <= 0)
    {show_debug_message("step 2");
        ds_list_delete(enemy_list,i);
        show_debug_message("list size" + string(ds_list_size(enemy_list)));
    }
    show_debug_message("step 3");
    };
 

Nidoking

Member
OK, look, it's clear you just don't understand what any of these functions do. Otherwise, you wouldn't do things like
ds_list_mark_as_map(enemy_list,-1);
or
var hp = enemy_map[? "health"];
followed by
ds_map_find_value(enemy_map,hp)
Stop writing code immediately. Read the Manual page for every function you've ever used. Then read it again. Understand what the functions do and how to make them do that thing. Then you may write code again.
 
Top