Search code examples
listfor-loopgame-maker-studio-2

problem in using list in game maker studio


I am new to the game maker. I created a list and I want to compare all the data in the list with a specific value. I used the following code:

for(var i=0;i<ds_list_size(lst);i++;)
{
    if ds_list_find_value(lst,i)>tmp
       ds_list_replace(lst,i,ds_list_find_value(lst,i)-1);
}

and I face the following error:

Push :: Execution Error - Variable Get -1.lst(100001, -1)
at gml_Object_object0_RightButtonPressed_1 (line 21) - for(var i=0;i

where is my problem?

Thanks all.


Solution

  • if your first for loop i = 0; and when the first entry in the list is smaller than tmp it tries to replace the first place in the list with a not existing one. so you could either check if its the first entry of the list with

    if ( i == 0 ) { }
    

    or your could start the for loop from the second entry with

    for(var i=1;i<ds_list_size(lst);i++;)