Solved - Works, but does not?

Hulle

Member
I want to understand code better, and so, please help me understand why the top code works when running my game, but not the buttom code:

TLDR: I change a local var DSlist. It is destroyed each step it is called/not needed. (top code)
to a DSlist in from create-event, that is cleared when not needed (collidedslist). But it stops adding to hitdslist, when it is created this way.

Code:
STEP EVENT:

if state == "blackflower" //Touch undeads
   {
   if listcreate = false
       {
       hitdslist = ds_list_create(); //This var is initialized in create
       ds_list_clear(hitdslist);
       listcreate = true;
       }
   var collidedslist = ds_list_create();     //Shoutout to Shaun Spalding,
   ds_list_clear(collidedslist);
   var enemy = instance_place_list(x, y, obj_enemy_parent, collidedslist, false); //Find if we collide with an enemy
   if enemy > 0 //If we collide with more than 0
       {
       for (var i = 0; i < enemy; ++i;) //Run a loop through them
           {
           var hit = ds_list_find_value(collidedslist,i) //find current ID
           var pos = ds_list_find_index(hitdslist, hit) //
           if pos == -1 //see if it doesexsist
               { //If it does not:
               ds_list_add(hitdslist, hit); //We add it to the list
               if i == enemy
                   {
                   ds_list_destroy(collidedslist);
                   }
               size = ds_list_size(hitdslist) //The we get the amount of enemies added to the list
               if size >= hituniqueamount
                   {
                   itemname = "blackflower"; //Change my name
                   image_index = 3; //Turn black
                   state = "blackdeath"
                   }
               }
           }
       }
   }
Code:
STEP EVENT:
if state == "blackflower" //Touch undeads
   {
   if listcreate = false
       {
       collidedslist = ds_list_create(); //Create and clear a few lists
       ds_list_clear(collidedslist);
       hitdslist = ds_list_create(); //Theese are both initialized in create
       ds_list_clear(hitdslist);
       listcreate = true;
       }
   var enemy = instance_place_list(x, y, obj_enemy_parent, collidedslist, false); //Find if we collide with an enemy
   if enemy > 0 //If we collide with more than 0
       {
       for (var i = 0; i < enemy; ++i;) //Run a loop through them
           {
           var hit = ds_list_find_value(collidedslist,i) //find current ID
           var pos = ds_list_find_index(hitdslist, hit) //
           if pos == -1 //see if it doesexsist
               { //If it does not:
               ds_list_add(hitdslist, hit); //We add it to the list
               if i == enemy
                   {
                   ds_list_clear(collidedslist);
                   }
               size = ds_list_size(hitdslist) //The we get the amount of enemies added to the list
               if size >= hituniqueamount
                   {
                   itemname = "blackflower"; //Change my name
                   image_index = 3; //Turn black
                   state = "blackdeath"
                   }
               }
           }
       }
   }
 
Last edited:

Catastrophe

Member
Heh, shaun spalding bug in the forum #10000. I don't get how they;re so popular.

The reason is because


var enemy = instance_place_list(x, y, obj_enemy_parent, collidedslist, false); //Find if we collide with an enemy

is adding to the list in every frame, and should only be used with an empty list. Your bottom code does not empty the list beforehand each frame
 

Hulle

Member
I think they are popular because he seems like a nice guy, who teaches a lot a tricks to beginners.
Beginners = mistakes.
And then someone clears it up, and this was you. Thank you for that. Know you are popular with me, how does it feel? :)
 

Catastrophe

Member
Yeah, I think everyone wants to know how to do platformers, too, haha. Anyways, feels good :D Sounds like it's solved, so add a big [solved] before your title :)
 
Last edited:

Hulle

Member
It sure is, will do :)
Allthough I must admit, I do not understand the reason behind why this list could not just be cleared opon creation, and after each use, and still yield the same result.
But I will live with not knowing :)
 
Top