#global variable not set before reading when it is set?

17 messages · Page 1 of 1 (latest)

modern steppe
#
    inventoryInteraction,
    startOfTurn,
    endOfTurn,
    onHurt,
    onUseItem,
    onPickUpInInventory
}

function item(_name,_sprite,_useFunction,_useTrigger,_description,_types,_space) constructor{
    name=_name
    sprite=_sprite
    useFunction=_useFunction
    useTrigger=_useTrigger
    space=_space
    types=_types
    description=_description
    function New(){
        var _struct={}
        var _names=struct_get_names(self)
        for(var i=0;i<array_length(_names);i++)
        {
            variable_struct_set(_struct,_names[i],variable_struct_get(self,_names[i]))
        }
    }
}

function add_item(_name,_sprite,_useFunction,_useTrigger,_description,_types=[0],_space=new vector2(1,1)){
    var _item=new item(_name,_sprite,_useFunction,_useTrigger,_description,_types,_space)
    
    array_push(global.itemList,_item)
}

function standard_attack(){
    for(var i=0;i<4;i++)
    {
        if(collision_line(position.x,position.y,position.x+lengthdir_x(GRID_SIZE,i*90),position.y+lengthdir_y(GRID_SIZE,i*90),obj_enemy_parent,true,true))
        {
            
        }
    }
}

global.itemTypes=ds_map_create()

ds_map_add(global.itemTypes,0,"Material")

ds_map_add(global.itemTypes,1,"Weapon")

ds_map_add(global.itemTypes,2,"Wooden")

ds_map_add(global.itemTypes,3,"Consumable")

ds_map_add(global.itemTypes,4,"Potion")

ds_map_add(global.itemTypes,5,"Armor")

ds_map_add(global.itemTypes,6,"Magic")

global.itemList=[]

add_item("Stripped Bark",spr_stripped_bark,standard_attack,useTriggers.inventoryInteraction,"A sharp piece of bark stripped from a tree. Deals 2 damage to enemies in front of you.",[1,2],new vector2(1,2))```|

i have all of this code running in a script but for osme reason it says that `global.itemList` isn't set before reading when i'm pretty sure it is. is there anything i'm missing here?
#

here's the error:

global variable name 'itemList' index (100031) not set before reading it.
 at gml_Script_add_item (line 33) -     array_push(global.itemList,_item)
############################################################################################
gml_Script_add_item (line 33)
gml_GlobalScript_enemies (line 33) - add_item("Stripped Bark",spr_stripped_bark,standard_attack,useTriggers.inventoryInteraction,[1,2],new vector2(1,2))
cyan mesa
#

Try declaring it before the function that uses it

modern steppe
# cyan mesa Try declaring it before the function that uses it

did that, that didn't fix the problem ```function item(_name,_sprite,_useFunction,_useTrigger,_description,_types,_space) constructor{
name=_name
sprite=_sprite
useFunction=_useFunction
useTrigger=_useTrigger
space=_space
types=_types
description=_description
function New(){
var _struct={}
var _names=struct_get_names(self)
for(var i=0;i<array_length(_names);i++)
{
variable_struct_set(_struct,_names[i],variable_struct_get(self,_names[i]))
}
}
}

global.itemList=[]

function add_item(_name,_sprite,_useFunction,_useTrigger,_description,_types=[0],_space=new vector2(1,1)){
var _item=new item(_name,_sprite,_useFunction,_useTrigger,_description,_types,_space)

array_push(global.itemList,_item)

}

function standard_attack(){
for(var i=0;i<4;i++)
{
if(collision_line(position.x,position.y,position.x+lengthdir_x(GRID_SIZE,i90),position.y+lengthdir_y(GRID_SIZE,i90),obj_enemy_parent,true,true))
{

    }
}

}

global.itemTypes=ds_map_create()

ds_map_add(global.itemTypes,0,"Material")

ds_map_add(global.itemTypes,1,"Weapon")

ds_map_add(global.itemTypes,2,"Wooden")

ds_map_add(global.itemTypes,3,"Consumable")

ds_map_add(global.itemTypes,4,"Potion")

ds_map_add(global.itemTypes,5,"Armor")

ds_map_add(global.itemTypes,6,"Magic")

add_item("Stripped Bark",spr_stripped_bark,standard_attack,useTriggers.inventoryInteraction,"A sharp piece of bark stripped from a tree. Deals 2 damage to enemies in front of you.",[1,2],new vector2(1,2))```

#

i have literally never had this problem before

cyan mesa
modern steppe
#

yes

#

checked it is the same error

cyan mesa
#

The error could maybe be that you're using keywords (e.g. x, sprite_index), inside of the script and it's trying to reach those variables from vector?

modern steppe
#

that's all there is

cyan mesa
modern steppe
cyan mesa
modern steppe
#

i put vector 2 at the top of the items script and it works