• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

(solved)getting index from ds_list

Smenkare

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?
 

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.
 

Homunculus

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

Smenkare

Member
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?
 

Homunculus

Member
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)
 
Top