GML argument incorrect type

UberhenriK

Member
Hello there again,
I have a problem with scripts.

I've been solving this problem for an hour now.

GML:
function Item_use(){
    var _Script = (ds_grid_get(PlayerInventory, 4, ItemSelected));
    script_execute(_Script);
    Item_delete();
    
}
This code located in script file.

Can i even do like this? ds_grid_get(PlayerInventory, 4, ItemSelected) gets script from item.
But every time i open inventory i get this error:
script_execute argument 1 incorrect type (undefined) expecting a Number (YYGI32)
at gml_Script_Item_use (line 43) - script_execute(_Script);

How can i solve it?
Thanks in advance.
 

Yellowhite

Member
That showing the "PlayerInventory" in ds_grid_get showed a eror.

Do you have definied
PlayerInventory how "ds_grid_create"?

or like

GML:
globalvar PlayerInventory;
PlayerInventory = ds_list_create();
 

UberhenriK

Member
That showing the "PlayerInventory" in ds_grid_get showed a eror.

Do you have definied
PlayerInventory how "ds_grid_create"?

or like

GML:
globalvar PlayerInventory;
PlayerInventory = ds_list_create();
Yes, i've defined it like this:

GML:
globalvar PlayerInventory, PlayerInventoryWidth;

PlayerInventoryWidth = 5;
PlayerInventory = ds_grid_create(PlayerInventoryWidth, 1);
 

saffeine

Member
where do you define what script is in that inventory slot? seems to be that you're getting a value that hasn't been set yet, which is returning undefined.
edit: i don't actually use script_execute, but if argument 1 is zero-indexed which i'd assume it is, it might also mean that your script is expecting an argument that you haven't provided.
 
Last edited:

UberhenriK

Member
where do you define what script is in that inventory slot? seems to be that you're getting a value that hasn't been set yet, which is returning undefined.
unfortunately not, PlayerInventory defined by Obj_inventory that is already located in first room (This error appears when i trying to open inventory, which content is already exists)
 

Nidoking

Member
This means that the grid has no value stored in cell (4, ItemSelected). You have to put the script ID there first. And you should really be using functions rather than script IDs.
 

saffeine

Member
something is calling Item_use() when the inventory is opened, and when it does it's either trying to execute a script that doesn't exist, or it's trying to execute a script that requires an argument that you haven't provided.
make sure you don't have the 'use item' button on the same button as the 'open inventory' button, or that both events aren't fired from the same button press.
furthermore, you're probably going to want to exit out of the Item_use() function if it doesn't find a script, otherwise empty slots / items without a use will also cause your game to crash if you attempt to use them.
 

Yal

šŸ§ *penguin noises*
GMC Elder
You really should learn about accessors and function variables, you'll have a lot less typing to do:
GML:
function Item_use(){
    var _Script = PlayerInventory[# 4, ItemSelected];
    _Script();
    Item_delete();
}
script_execute in particular only executes the "main" function (function with the same name as the script) according to the manual, so if you have scripts with multiple functions, it might run the wrong function.

Some other pointers:
  • You don't check if the item exists before reading its script
  • You don't check if the script exists before running it
Since 0 is a valid asset ID it's possible you're executing a random script if you try to use a nonexistant item, and you probably want a case for if the player tries using an item that can't be used that just plays a buzzer sound and doesn't consume the item (e.g. if they try using a letter or a key you don't want them to eat it and be permanently screwed from progressing)
 

UberhenriK

Member
something is calling Item_use() when the inventory is opened, and when it does it's either trying to execute a script that doesn't exist, or it's trying to execute a script that requires an argument that you haven't provided.
make sure you don't have the 'use item' button on the same button as the 'open inventory' button, or that both events aren't fired from the same button press.
furthermore, you're probably going to want to exit out of the Item_use() function if it doesn't find a script, otherwise empty slots / items without a use will also cause your game to crash if you attempt to use them.
the problem was in totally bugged button which called Item_use() in step that was created, now that's works fine) without your reply i'd look for this problem much longer
 

UberhenriK

Member
You really should learn about accessors and function variables, you'll have a lot less typing to do:
GML:
function Item_use(){
    var _Script = PlayerInventory[# 4, ItemSelected];
    _Script();
    Item_delete();
}
script_execute in particular only executes the "main" function (function with the same name as the script) according to the manual, so if you have scripts with multiple functions, it might run the wrong function.

Some other pointers:
  • You don't check if the item exists before reading its script
  • You don't check if the script exists before running it
Since 0 is a valid asset ID it's possible you're executing a random script if you try to use a nonexistant item, and you probably want a case for if the player tries using an item that can't be used that just plays a buzzer sound and doesn't consume the item (e.g. if they try using a letter or a key you don't want them to eat it and be permanently screwed from progressing)
Thanks for useful tips!)
 
Top