Help with Ds_Lists

Discussion in 'Programming' started by Rukiri, Jun 17, 2019.

  1. Rukiri

    Rukiri Member

    Joined:
    Sep 25, 2016
    Posts:
    137
    I'm having a strange problem when I'm adding to my group list from my database list.
    Whenever I go to add index 2 for example which is the 3rd character in the database when I do a printout of the group it only seems to add index 0 regardless if that was the character to add..


    Init:
    Code:
    //---------------------------------------------------
    // RPG Actor Database Initialize
    //---------------------------------------------------
    
    // Initialize Variables
    
    party_group = ds_list_create();
    party_database = ds_list_create();
    
    // Party Enums
    
    enum Actor {
        Name,
        Class,
        Health,
        Max_Health,
        Mana,
        Max_Mana,
        Attack,
        Defense,
        Mana_Attack,
        Mana_Defense,
        Vitality,
        Agility,
        Luck,
        Level,
        Experience,
    }
    
    //---------------------------------------------------
    // End Script
    //---------------------------------------------------
    Create Character:
    Code:
    //---------------------------------------------------
    // RPG Actor Create
    //---------------------------------------------------
    
    // Create a data structure for our actor
    var map = ds_map_create();
    
    // Stat Variables
    var name = argument0;
    
    // Now we add it to the database so we don't ever lose it!
    ds_list_add(party_database, map); // Now it's there! Safe!
    
    // Create our Actor
    ds_map_add(map, Actor.Name, string(name)); // Actor Name
    
    // If everything went okay
    return 1;
    
    //---------------------------------------------------
    // End Script
    //---------------------------------------------------

    How I find my indexes.

    For the sake of this thread I'm naming this find_index
    Code:
    //--------------------------------------------------------------------
    // RPG Get Actor Stats
    //--------------------------------------------------------------------
    
    var i, size;
    size = RPG_Party_Database_Count();
    
    for(i = 0; i <= size; i +=1) {
        if(ds_list_find_value(party_database, i) == argument0) {
           // Woo! We found the correct index location
          return i;
        }
    }
    
    //--------------------------------------------------------------------
    // End Script
    //--------------------------------------------------------------------
    so to add I would
    Code:
    ds_list_add(party_group, find_index(argument0)); 
    Am I doing something wrong, this a known issue? I can't seem to find a fault here.
     
  2. 2Dcube

    2Dcube Member

    Joined:
    Jun 23, 2016
    Posts:
    324
    I'm not sure I understand what you're doing completely. When using find_index(), are you comparing map indices?
    At the least you should return something when it's not found. For example, put "return -1;" after the for loop, and don't add anything to party_group when it's not found.

    In stead of comparing ds_ indices, I would give each map an id and search for that, or the actor name or anything.
    ds_ index can change easily since it's just determined by the creation order.
     
  3. Rukiri

    Rukiri Member

    Joined:
    Sep 25, 2016
    Posts:
    137
    I think I may have solved this.

    Ini:
    Code:
    globalvar party, party_database;
    
    party = ds_list_create();
    party_database = ds_list_create();
    
    // Party Enums
    
    enum Actor {
        Name,
        Class,
        Health,
        Max_Health,
        Mana,
        Max_Mana,
        Attack,
        Defense,
        Mana_Attack,
        Mana_Defense,
        Vitality,
        Agility,
        Luck,
        Level,
        Experience,
    }
    Create:
    Code:
    // Stat Variables
    var name = argument0;
    var map = ds_map_create();
    
    // Create our Actor
    ds_map_add(map, Actor.Name, string(name)); // Actor Name
    ds_list_add(party_database, map);
    Test Object - Create:
    Code:
    RPG_Party_Database_Ini();
    
    RPG_Party_Create_Actor("Harold");
    RPG_Party_Create_Actor("Cloud");
    RPG_Party_Create_Actor("Zack");
    
    ds_list_add(party, ds_list_find_value(party_database, 1));
    ds_list_add(party, ds_list_find_value(party_database, 2));
    ds_list_add(party, ds_list_find_value(party_database, 0));

    Screen Caps (Too Large for forum):
    [​IMG]
    And I can add in any order without issue, also added extra characters and picked at random.
    [​IMG]

    I think my issue was I was adding to the database before adding the actors data as now I can freely access that data between the party and database no problem.
     
  4. 2Dcube

    2Dcube Member

    Joined:
    Jun 23, 2016
    Posts:
    324
    Good to hear! Still, make sure you do a check before adding something to a list. It will make it easier to catch bugs in the future.
     
  5. Rukiri

    Rukiri Member

    Joined:
    Sep 25, 2016
    Posts:
    137
    The funny thing is, this is code I wrote way back in the GM7/8 days and since honestly not much has changed I just kept using it without thinking to check to see if bugs... Before I could just add to the list before doing ds_map_add guess ya can't now...

    Also, I can access other stats from the map so I think we're in the clear.
    Gave the characters funny class names.
    [​IMG]
     

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