BaconMuncher
Member
Okay, i'm working on a game and all I want to do is sort ten valued items quickly.
so I already have ten items in my game sorted in the variable Item_Name
Item_Name[1] = "Jeans"
Item_Name[2] = "Shirt"
Item_Name[3] = "Wool Gloves"
etc up thru 10 items
and then I already have ten values that change but Item_Cost reflects the cost of each item...
Item_Cost[1] = 55;
Item_Cost[1] = 40;
Item_Cost[3] = 25;
etc up thru 10...
Obviously Item_Cost[1] is the cost of the Item_Name[1} (Jeans), Item_Cost[2] refers to the cost of the shirt, 3 the cost of the wool gloves.
Because these costs go up and down based on supply and demand in my game, which I've already got working.. now all I want to do is be able to list these items by their cost with the top item being the most expensive item at the time, Item 2 on the list being the 2nd most expensive, etc.
I understand I can do this with an array and making and deleting a DS list and sorting it but I can't get it to work.
I found this array sorting script: https://www.gmlscripts.com/script/array_sort
But I keep getting an error saying argument 2 incorrect type (array), expecting a number.
Here's how I have that same script in my code:
var array = Item_Cost;
var ascend = Item_Name;
var list = ds_list_create();
var count = array_length_1d(array);
for (var i=0; i<count; i++) list[| i] = array;
ds_list_sort(list, ascend);
for (i=0; i<count; i++) array = list[| i];
ds_list_destroy(list);
return array;
I feel dumb but I guess that I've never really grasped arrays at all. I'm sure it's simple. If someone could please show me in code how I can do what I want to do and draw these item names and their values which change often in my game... I'd greatly appreciate it and I think it would also help me understand arrays better.
so I already have ten items in my game sorted in the variable Item_Name
Item_Name[1] = "Jeans"
Item_Name[2] = "Shirt"
Item_Name[3] = "Wool Gloves"
etc up thru 10 items
and then I already have ten values that change but Item_Cost reflects the cost of each item...
Item_Cost[1] = 55;
Item_Cost[1] = 40;
Item_Cost[3] = 25;
etc up thru 10...
Obviously Item_Cost[1] is the cost of the Item_Name[1} (Jeans), Item_Cost[2] refers to the cost of the shirt, 3 the cost of the wool gloves.
Because these costs go up and down based on supply and demand in my game, which I've already got working.. now all I want to do is be able to list these items by their cost with the top item being the most expensive item at the time, Item 2 on the list being the 2nd most expensive, etc.
I understand I can do this with an array and making and deleting a DS list and sorting it but I can't get it to work.
I found this array sorting script: https://www.gmlscripts.com/script/array_sort
But I keep getting an error saying argument 2 incorrect type (array), expecting a number.
Here's how I have that same script in my code:
var array = Item_Cost;
var ascend = Item_Name;
var list = ds_list_create();
var count = array_length_1d(array);
for (var i=0; i<count; i++) list[| i] = array;
ds_list_sort(list, ascend);
for (i=0; i<count; i++) array = list[| i];
ds_list_destroy(list);
return array;
I feel dumb but I guess that I've never really grasped arrays at all. I'm sure it's simple. If someone could please show me in code how I can do what I want to do and draw these item names and their values which change often in my game... I'd greatly appreciate it and I think it would also help me understand arrays better.