Hex96
Hex96

Reputation: 1

GMS2 - Why am I getting this syntax error?

I made an inventory system(other is the drop) and here is the pickup code

    // pickup

    found_in_inv = false;

    for(var i = 0; i < ds_list_size(global.inv); i++){
        if(global.inv[| i][| 0] == other.object_index){ // error line
            global.inv[| i][| 1]++;
            found_in_inv = true;
            break;
        }
    }

    if(found_in_inv){
        instance_destroy(other);
    }else{
        for(var i = 0; i < ds_list_size(global.inv); i++){
            if(global.inv[| i][| 0] == noone){
                global.inv[| i][| 0] = other.object_index;
                global.inv[| i][| 1] = 1;
                break;
                instance_destroy(other);
            }
        }
    }

I am getting a syntax error where "[|" found, ")" expected. I don't know how to fix this, please help.

Upvotes: 0

Views: 554

Answers (1)

YellowAfterlife
YellowAfterlife

Reputation: 3192

Chained accessors (a[i][k], or a[|i][|k] in your case) are only supported in version >= 2.3 (as of writing this, is in beta).

Assign the first retrieved item into a variable to get around the fact.
Perhaps also take an opportunity to not do more reads than you need.

    for(var i = 0; i < ds_list_size(global.inv); i++){
        var item = global.inv[| i];
        if(item[| 0] == other.object_index){ // error line
            item[| 1]++;
            found_in_inv = true;
            break;
        }
    }

Upvotes: 2

Related Questions