1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Solved - Works, but does not?

Discussion in 'Programming' started by Hulle, Nov 6, 2019.

  1. Hulle

    Hulle Member

    Joined:
    Oct 29, 2019
    Posts:
    8
    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: Nov 6, 2019
  2. Catastrophe

    Catastrophe Member

    Joined:
    Sep 22, 2019
    Posts:
    106
    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 likes this.
  3. Hulle

    Hulle Member

    Joined:
    Oct 29, 2019
    Posts:
    8
    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 likes this.
  4. Catastrophe

    Catastrophe Member

    Joined:
    Sep 22, 2019
    Posts:
    106
    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: Nov 7, 2019
  5. Hulle

    Hulle Member

    Joined:
    Oct 29, 2019
    Posts:
    8
    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 :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice