GMS 2.3+ Dynamic ds_list with a switch statement

Stratos.la

Member
Hello yet again! im trying to make a dynamic ds_list with elements being added when conditions are met. i have a list with 3 initial elements and would like to add more after a level of when the character levels up. i have created a global variable and tried it with an if statement in the object that holds the list create event, but no luck, when i do it like this it overwrites the previous entries, which i understand since I'm changing the list. Any suggestions on how i should handle something like this is there a way to have a fixed number of elements in the list but without a value and set the value when the conditions are met?
GML:
elements = ds_list_create();
if global.cardHoldings = 0
{
    ds_list_add(elements, sCardArcher);
    ds_list_add(elements, sCardMage);
    ds_list_add(elements, CardsIndex);
}
else if global.cardHoldings = 1
{
    ds_list_add(elements, sCardArcher);
}
so since i don't know my lists that well, am looking for a way to keep the last elements and add more when the global variable changes.
 

SnoutUp

Member
Move ds_list_create() somewhere, where it wouldn't be called every time your character levels up. Alternatively you can re-create elements list fully, but make sure to destroy previous list to avoid memory leaks.
GML:
elements = ds_list_create(); 

switch(global.cardHoldings) {
    case 1:
        ds_list_add(elements, sCardArcher);
    case 0:
        ds_list_add(elements, sCardArcher);
        ds_list_add(elements, sCardMage);
        ds_list_add(elements, CardsIndex);   
    break;
}
 

Stratos.la

Member
i see, thanks , but i dont really mind on when the player levels up. the cards will only be available in mid game. so for instance after level 3 is finished a new card will be available to use and on level 4 the player will have 4 cards instead of 3. the global variable would just be called on different places and add +1 to the list every time
 
Every time you create a list, the old list still exists. You need to either destroy the old list or reuse the same one with ds_list_clear. Or you could add to the pre-existing list with ds_list_add. Just initialize the list in a create event and reference it from then on.
 
Top