script in function gml 2.3

Teach how to rewrite a script?

GML:
///@func    scrItemAdd(inv, key, amount, [slot], [tags])
///@desc    inserts an item into an inventory in the first available slot(s), or in the specified slot.
///@arg        {number} inv - inventory
///@arg        {string} key - the key of the item to insert
///@arg        {number} amount - amount to insert
///@arg        {number} [slot] - (optional) the slot index to insert the item into. Leaving the slot empty or passing -1 as value will insert in the first available slot (or stack)
///@arg        {ds_map|number} [tags] - (optional) a ds_map with the values to insert as tags
///@return    {number} the actual amount of items inserted (same as "amount", given enough space in the inventory or slot)

function scrItemAdd() {

    var _inv = argument[0];
    var _key = argument[1];
    var _amount = argument[2];
    var _slot = argument_count > 3 ? argument[3] : -1; //???
    var _tags = argument_count > 4 ? argument[4] : -1; //???

....
 
Works. But in gml 2.3 no need to write arguments

scrItemAdd(inv, key, amount, slot, tags) {

slot = ( slot != undefined ? slot : noone); // that's right???
tags = ( tags != undefined ? tags : noone);

...

}
 

TailBit

Member
This page explains it better:
GameMaker Studio 2 Manual - argument

The reason I think he used argument array was so that he could have an optional amount of argument, where only the 3 first was needed .. if the argument[n] array isn't used at all then your function will get an yellow error when you try to write it with any other then the amount of arguments provided.

But, you can simply mix both .. just keep the 2 argument_count lines so there won't be any errors
GML:
function scrItemAdd(_inv,_key,_amount,_slot,_tags) {

    _slot = argument_count > 3 ? argument[3] : -1;
    _tags = argument_count > 4 ? argument[4] : -1;

....
The 3 first values are already defined in the function head so var should be unessesary (?)
 

Nidoking

Member
Mixing the two styles is a really bad idea. Just use is_undefined to check the arguments that might not be supplied and give them default values.
 

samspade

Member
Mixing the two styles is a really bad idea. Just use is_undefined to check the arguments that might not be supplied and give them default values.
I'm not sure I agree with that as a whole, but in this case since named arguments that don't have values passed in are undefined by default, I agree that the best solution would be to use the is undefined check. Although I would point out that we don't know what the rest of the function does and the fact that its being set to -1 (presumably some sort of 'nullish' variable) means you might be able to skip it entirely. Just check for undefined where you would be checking for -1. For example doing the below seems a little redundant. But without knowing what the rest of the function does it is hard to say. Maybe it would be necessary.

Edited:
GML:
function scrItemAdd(_inv, _key, _amount, _slot, _tags) {

    _slot = is_undefined(_slot) ? -1 : _slot; //???
    _tags = is_undefined(_tags) ? -1 : _tags; //???

    //more code

}
 

TailBit

Member
Yup, I would avoid mixing it too .. but I tried that in the editor, and it give a yellow error because it expect 5 arguments when I try to use the function, which is not a good idea if it is some code one want to share, the error goes away if the argument array is used ..

So we are missing a way to mark arguments as optional for the editor
 

samspade

Member
Yup, I would avoid mixing it too .. but I tried that in the editor, and it give a yellow error because it expect 5 arguments when I try to use the function, which is not a good idea if it is some code one want to share, the error goes away if the argument array is used ..

So we are missing a way to mark arguments as optional for the editor
This is documented behavior and even suggested by the manual, so I don't think there's anything wrong with it, but you probably would want to make a note in the description of the function. Yellow is just a warning, which you probably want here as you might have forgotten, but it's just GM checking.

What @Nidoking was referring to (I believe) was mixing named arguments and the argument array. That I also disagree and would definitely do when appropriate, but here there isn't a reason to do that as you know the max number of arguments. The better idea as they pointed out was using the fact that gm passes in unused arguments as undefined.
 

Nidoking

Member
What @Nidoking was referring to (I believe) was mixing named arguments and the argument array.
I feel like it's undisciplined to mix styles in the same function call. It really doesn't seem difficult to me to call functions with the correct number of arguments every time, and if I really need something variadic, I'd pass in an array or list that will tell me exactly how many values are in it without me needing to check for undefined. For something like what's described here, I'd just write a quick wrapper function for insert_item_in_first_available_slot that handles finding the first slot and then calls the main function with all of the arguments. I know it's not broken as such, but there are practices that produce perfectly working games that I guarantee will cause headaches someday when you want to change them.
 
Top