GML ds_list_delete breaks ds_list order?

Mairoz

Member
I am new to data structures. I tried to make a ds_list that stores all the sockets in my server. Whenever client disconnects from the server I remove the socket from the list using ds_list_find_value. However whenever that happens everything in the list that has higher position than the value removed, changes its position to -1.
Example:
I have ds_list with values:
1,2,3,4,5
I remove "3" with ds_list_delete
The list becomes:
1,2
1 and 2 have normal positions (0 and 1) but 4 and 5 position is changed to -1
What I would like to happen is that the list becomes: 1,2,4,5
If anyone knows how to do this, I would love to know. Thank you
 

Petrik33

Member
That's strange, cause just yesterday I made the code where I delete the [0] element of the list and the other indexes just decrease and everything works fine. Maybe the problem is somewhere else? And have you tried deleting the 0 element? Because if it works fine when you delete it then the problem is here. Otherwise I think it's somewhere else.
 

Mairoz

Member
That's strange, cause just yesterday I made the code where I delete the [0] element of the list and the other indexes just decrease and everything works fine. Maybe the problem is somewhere else? And have you tried deleting the 0 element? Because if it works fine when you delete it then the problem is here. Otherwise I think it's somewhere else.
Thank you for the reply. Yes it happens if I delete the [0] element as well. Because it is on the first position then what it causes is that all the other positions are set to -1. The only times the ds_list is accessed is when something is added to the list with ds_list_add or when something is removed from the list with ds_list_delete or when something is being read from the list with ds_list_find_value so I don't know what could possibly cause this to happen.
 

NightFrost

Member
You're probably handling list delete or access wrong, because ds list normally rearranges index values when something is deleted from the middle. If your list arrangement is { 0 = "A", 1 = "B", 2 = "C" } and you delete index 1 entry, the remaining list will be { 0 = "A", 1 = "C" }.
 

Perseus

Not Medusa
Forum Staff
Moderator
Could it be that ds_list_delete() is not actually being executed only once? Perhaps the function is being executed every successive step until only elements corresponding to indexes lower than one given to it remain. So, for instance, in your example in the first post, you delete the number 3 which is at index 2 -- the function then probably runs multiple times in consecutive steps to remove whatever number replaces the previous one at index 2. Also, please consider posting the exact code you're using if this doesn't help.
 

Roderick

Member
Ok, so let me make sure I have this right.

You start with
Code:
0 - data0
1 - data1
2 - data2
3 - data3
4 - data4
When you ds_list_delete on index 2 you get
Code:
0 - data0
1 - data1
2 - data3
3 - data4
Is this correct? If so, everything is functioning as intended. Deleting an index from a ds_list removes that index and everything shifts down to fill in the gap.

If you want to remove the data, but keep everything where it is, you need to fill in the slot with some sort of null data; an empty string (""), 0 or -1 or some other number that can't normally exist in that field, for example. In this case, just change the value, don't ds_list_delete anything.

Take a look at ds_maps. They may be more akin to what you want. A ds_map can have non-contiguous keys, or even use strings as keys.
 
Last edited:

TheouAegis

Member
Ok, so let me make sure I have this right.

You start with
Code:
0 - data0
1 - data1
2 - data2
3 - data3
4 - data4
When you ds_list_delete on index 2 you get
Code:
0 - data0
1 - data1
2 - data3
3 - data4
Is this correct? If so, everything is functioning as intended. Deleting an index from a ds_list removes that index and everything shifts down to fill in the gap.

If you want to remove the data, but keep everything where it is, you need to fill in the slot with some sort of null data; an empty string (""), 0 or -1 or some other number that can't normally exist in that field, for example. In this case, just change the value, don't ds_list_delete anything.

Take a look at ds_maps. They may be more akin to what you want. A ds_map can have non-contiguous keys, or even use strings as keys.
It sounds more like he is saying it becomes

0 - data0
1 - data1
2 - undefined
3 - undefined
4 - undefined

Which would be in line with Perseus' suggestion, likely due to the condition for deleting position 3 not being cleared immediately (e.g., keyboard_check() instead of keyboard_check_pressed() for the condition).
 
Top