(solved)getting index from ds_list

S

Smenkare

Guest
Create event

exampleList = ds_list_create();
ds_list_add(exampleList,1);
ds_list_add(exampleList,10);
ds_list_add(exampleList,10);
ds_list_add(exampleList,30);


choice = 0;
weight_sum = 0;
for(var i=0; i<ds_list_size(exampleList);i++)
{weight_sum += exampleList[|i]}

Draw event

if (keyboard_check_released(vk_left))
{
draw_text(500, 300, choice);
}

release key event


for(var t = 0,ix =-1, r = random(weight_sum); t < r ; t+=exampleList[|ix])
{ix+=1};
if(ix>-1)
{choice= exampleList[|ix]} ;


And the problem is: i dont want to get value in choice but i want to get index of picked value from ds_list. I cant find any information how to get int from ds_list indexes. I think im just stupid. Is making ds_map of ds_list the only way to acces indexes?
 

chamaeleon

Member
Create event

exampleList = ds_list_create();
ds_list_add(exampleList,1);
ds_list_add(exampleList,10);
ds_list_add(exampleList,10);
ds_list_add(exampleList,30);


choice = 0;
weight_sum = 0;
for(var i=0; i<ds_list_size(exampleList);i++)
{weight_sum += exampleList[|i]}

Draw event

if (keyboard_check_released(vk_left))
{
draw_text(500, 300, choice);
}

release key event


for(var t = 0,ix =-1, r = random(weight_sum); t < r ; t+=exampleList[|ix])
{ix+=1};
if(ix>-1)
{choice= exampleList[|ix]} ;


And the problem is: i dont want to get value in choice but i want to get index of picked value from ds_list. I cant find any information how to get int from ds_list indexes. I think im just stupid. Is making ds_map of ds_list the only way to acces indexes?
Seems like you have the index... You call it ix..
Code:
choice = ix;
Unless you mean something completely different from most other people when you use the word index.
 
S

Smenkare

Guest
the index for me is the numer of the entry in ds_list not the value of this entry
 
H

Homunculus

Guest
That’s what your variable ix is. The value would be exampleList[| ix], but the index is just ix
 
S

Smenkare

Guest
So to acces it i have to just write choice = exampleList[ix]? Without | ?
 
H

Homunculus

Guest
No, you always have to use | with ds_lists to the value (unless you use ds_list_find_value, which is equivalent).
 
S

Smenkare

Guest
Ok then when i get 30 i dont want to get 30. I want choice to be 4 cause 30 is 4th entry on ds_list. And how to get it?
 
H

Homunculus

Guest
I don’t know how to explain it at this point. The value you are looking for is just “ix”. If you want 30, you use exampleList[| ix], if you want 4, just use ix.

By the way, 30 is 3, not 4, since arrays and ds_lists are 0 indexed (the first index is 0, not 1)
 
S

Smenkare

Guest
Ok. Haha. Im laughing so hard. How can i be so stupid...is it possible to delete topic? XD
 
H

Homunculus

Guest
You can delete this topic, but Cthulhu knows. And will never forget.
 
Top