• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED checking a value in a ds_list

RODO

Member
hi guys, i'm having a problem with my game involving my inventory. I'm trying to fix a bug that when I go to craft a new item, in addition to spending the materials, the item is not created because the inventory is full, so to fix this I had an idea, to check if there is space, and if there is, it runs the crafting code but here comes the problem. I made the code like this:
GML:
function scr_check_inv(){
    var list_ = global.invlist;
    var list_size = ds_list_size(list_)
    for(var i = 0; i < list_size; i++)
    {
        var _arr = list_[| i];
        if(_arr != 1)
        {
            //if I'm going through the inventory, and I have
            //everyone busy, i come back true
            return true;
        }
    }

}
the problem with this function I made is that it's not returning correctly, I don't know if that was clear but the problem is that I can't return the inventory value, if there really is an item there or not and that's what I'd like to help, how can i see if there is value there or not? I tried using Ds_list_find_valor() but it gives the same problem. I appreciate any help or tip.
 
Hmm...why not just use something like this:
GML:
function scr_check_inv()
    {
    if ds_list_find_index(global.invlist,0) = -1 return false else return true;
    }
And just replace the check for 0 with whatever your index for an empty slot is. (I use -1 for inventory systems, so my item index can start at 0.
 

RODO

Member
Hmm...why not just use something like this:
GML:
function scr_check_inv()
    {
    if ds_list_find_index(global.invlist,0) = -1 return false else return true;
    }
And just replace the check for 0 with whatever your index for an empty slot is. (I use -1 for inventory systems, so my item index can start at 0.
well, i guess that way it won't work because i need to check each slot, and see if one of them is free and thus return true. And it seems that even using ds_list_find_index it is still not responding right
 

Ommn

Member
You save the values inside the DS List as numbers or an array?
try this:
GML:
function scr_check_inv(){
    var list_ = global.invlist;
    var list_size = ds_list_size(list_)
    for(var i = 0; i < list_size; i++)
    {
        var _arr = list_[| i];
        if(_arr<=0)
        {
            //if I'm going through the inventory, and I have
            //everyone busy, i come back true
            return true;
        }
    }
    return false;
}
 

RODO

Member
You save the values inside the DS List as numbers or an array?
try this:
GML:
function scr_check_inv(){
    var list_ = global.invlist;
    var list_size = ds_list_size(list_)
    for(var i = 0; i < list_size; i++)
    {
        var _arr = list_[| i];
        if(_arr<=0)
        {
            //if I'm going through the inventory, and I have
            //everyone busy, i come back true
            return true;
        }
    }
    return false;
}
in an array. In this case, one value is the item id and the other is its quantity. but I will try to use your suggestion
 

RODO

Member
if it is array
try this:
GML:
if(_arr[1]<=0)
unfortunately these ideas didn't work but they helped me in my tests and I found something strange, I'll share the code to help explain:

GML:
if(keyboard_check_pressed(vk_up))
{
    var list_ = global.invlist;
    var list_size = ds_list_size(list_)
    for(var i = 0; i < list_size; i++)
    {
        var _arr = list_[| i]
        if(_arr != 0)
        {
            show_debug_message(_arr);
        }
        else
        {
            show_debug_message("I didn't find anything");
        }
    }
    
}
The code above has been placed in a step so I can see what's inside my inventory and when it's empty. Well when there's something it returns what's there (item id and quantity) but when it's empty it doesn't show anything, not even the message. I tried using if(_arr == 0), if(_arr == -1) and if( _arr == undefined) but I couldn't get anything, even the message didn't run.
So i can only run my script when there is something, and that is a problem because i want to check when there is nothing or if there is any free space
 
What value do you have, when the position in the list is empty?

GML:
ds_list_find_index(id, val);
Whatever value 'val' has, this is checked for in the list.

If that value isn't found, it returns '-1'

If it is found, it returns the index of the list that has the value.

So: the value you're looking for can't be '-1' since if all the slots are full you won't find the value of "empty", and will get a result of.........'-1' (if I'm figuring this logically) which suggests there's an empty position, but there isn't..........

You could set empty positions to '-4' but that won't work if you are using values that could go negative, and the same with '-1' (regardless of that fact you can't use it for other reasons)

So your empty designator should be a string, such as "empty", and then there can be no mistaking what the value means, or confusion from the function result if it's '-1' (none are empty)

Or you could just delete empty positions from the list.

If the list is smaller than a certain size, and new items are picked up, then you can grow the list. If it's full, then you can't pick up the new item.
 

Ommn

Member
try the code and tell us what happened in debug
GML:
var _arr = list_[| i]
show_debug_message(_arr);
 

gnysek

Member
When using ds_list, I see no sense in keeping "empty" slots in it (array would be enough for that), as thanks to ds_list_delete they fit ideally into flexible size structure. This way, by inventory size you can guess if it's full or not.
But, if you want to keep empty slots too, and empty slot is marked by -1 for example, getting info about empty slots should be easy this way:
GML:
function is_inv_empty() {
    return is_undefined(ds_list_find_value(list_index, -1)) == false; // if position is undefined, then there's no -1 in list
}
As if there's no -1 value in list, it means all slots are taken.

What's important, from what I see in manual, GameMaker by default reserves some slots in DS LIST which are marked 0, so if you're searching for 0 in empty ds_list, it might be found, even if you didn't added any value there yet! https://manual.yoyogames.com/index.htm?#t=GameMaker_Language/GML_Reference/Data_Structures/DS_Lists/ds_list_find_value.htm
 

RODO

Member
try the code and tell us what happened in debug
GML:
var _arr = list_[| i]
show_debug_message(_arr);
Nothing happens, but when I put something the id and the item number appear. This is making me a little confused, Shouldn't it go back -1 ? or something like that because it's empty?
 

RODO

Member
When using ds_list, I see no sense in keeping "empty" slots in it (array would be enough for that), as thanks to ds_list_delete they fit ideally into flexible size structure. This way, by inventory size you can guess if it's full or not.
But, if you want to keep empty slots too, and empty slot is marked by -1 for example, getting info about empty slots should be easy this way:
GML:
function is_inv_empty() {
    return is_undefined(ds_list_find_value(list_index, -1)) == false; // if position is undefined
}
As if there's no -1 value in list, it means all slots are taken.

What's important, from what I see in manual, GameMaker by default reserves some slots in DS LIST which are marked 0, so if you're searching for 0 in empty ds_list, it might be found, even if you didn't added any value there yet! https://manual.yoyogames.com/index.htm?#t=GameMaker_Language/GML_Reference/Data_Structures/DS_Lists/ds_list_find_value.htm
wow thanks for the help. This was a great help and explanatory too
 

gnysek

Member
show_debug_message should print some data in Output window on bottom of IDE, where build log is put.
 

RODO

Member
well, i am able to fix my problem using all the ideas and information you gave me, thanks everyone for helping me. It really helped me a lot and I learned a lot, thank
 
Top