Variable <unknown_object>.instance_create() not set before reading it.

A

Arilyn

Guest
Hello,

I'm pretty new to game maker but I've messed around with Unity a lot in the past.

I've hit a roadblock and haven't been able to figure out why I'm getting errors despite researching and reading the documentation, but it's probably because I don't quite understand how GML and scripts work.

I'm creating a card game, and I'm trying to use a DS list to store certain IDs of cards to reference them later. I've created a "DrawCards" function to create instances of the Card object and assign their card_id variable to that of the card drawn.

Here is the code for my playerDeck create event. I add 3 cards to the deck and 3 sprites to a sprite list to keep them lined up with card ids so I can load the front images in the future. After that, I create the hand object.

GML:
global.playerDeck = ds_list_create();
ds_list_add(global.playerDeck, 1, 2, 3);

deckCount = ds_list_size(global.playerDeck);

global.sprList = ds_list_create();
ds_list_add(global.sprList, spr_FrontTest1, spr_FrontTest2, spr_FrontTest3);

global.handObj = instance_create(800, 1400, obj_Hand)
The hand object runs the following code on the create event.

GML:
handCount = 0;
global.hand = ds_list_create();

DrawCards(3);
And then, here's the DrawCards function I was making:

GML:
// Script assets have changed for v2.3.0 see
// https://help.yoyogames.com/hc/en-us/articles/360005277377 for more information


function DrawCards(_count)
{
    for (i = 0; i < _count; i++)
    {
        var _obj = instance_create(500, 500, obj_Card);
 
        //if the first card in the deck exists
        if (!is_undefined(ds_list_find_value(global.playerDeck, 0)))
        {
            //set the card id to that of the first card in the player deck.
            _obj.card_id = ds_list_find_value(global.playerDeck, 0);
            //add the card to the end of the hand.
            ds_list_add(global.hand, ds_list_find_value(global.playerDeck, 0));
            //print the card id
            show_debug_message(_obj.card_id);
            //move all cards up one slot and remove the last card left behind.
            s = ds_list_size(global.playerDeck);
            for (_i = 0; _i < s; _i++)
            {
                if (is_undefined(global.playerDeck, _i + 1))
                {
                    ds_list_delete(global.playerDeck, _i);
                }
                else
                {
                    ds_list_replace(global.playerDeck, _i, ds_list_find_value(global.playerDeck, _i + 1));
                }
            }
        }
        else
        {
            //Reshuffle the deck.
        }
        //repeat until the total number of cards is drawn.
    }
}
So the problem is when it hits the var _obj = instance_create(500, 500, obj_Card); it gets an error but I don't quite understand why. In addition, when I comment out the instance_create line, it gets another error at the if (ds_list_find_value(global.playerDeck, _i + 1) == undefined) that says global variable name 'playerDeck' index (100005) not set before reading it.

Is this something to do with variables in scripts/functions? How can I work around this? Thanks in advance.

edit: fixing formatting
 
Last edited by a moderator:

TsukaYuriko

☄️
Forum Staff
Moderator
instance_create is not a function of GameMaker Studio 2. Did you mean instance_create_layer?

Wherever you got that function name from is likely outdated, as this function existed in older versions of GameMaker, but not in the current one.
 

O.Stogden

Member
Instance_create is no longer a function in GMS 2.3.0.

If it hasn't had a compatibility script made for it, try using instance_create_depth or instance_create_layer.
 
A

Arilyn

Guest
instance_create is not a function of GameMaker Studio 2. Did you mean instance_create_layer?

Wherever you got that function name from is likely outdated, as this function existed in older versions of GameMaker, but not in the current one.
Thank you, that solved the problem. However, I'm still having issues referencing the global.playerDeck variable in the DrawCards function.
 

Nidoking

Member
However, I'm still having issues referencing the global.playerDeck variable in the DrawCards function.
That variable isn't declared until the Create event of your ... playerDeck? Are you trying to use the name of an object in your game as a variable?
 
A

Arilyn

Guest
That variable isn't declared until the Create event of your ... playerDeck? Are you trying to use the name of an object in your game as a variable?
I figured it out. It's because I already had a hand object on the screen before the create event on the deck created one
 
Top