GM:S 1.4 problem when loading game

Discussion in 'Programming' started by Anton Provid, Jun 15, 2019.

  1. Anton Provid

    Anton Provid Member

    Joined:
    Jun 11, 2019
    Posts:
    14
    Hi, i got the problem when loading game:
    Code:
    instance_create argument 2 incorrect type (undefined) expecting a Number (YYGF)
     at gml_Script_scrLoad (line 17) -     var Inst = instance_create(SaveGrid[# a, 1],SaveGrid[# a, 2],SaveGrid[# a, 0]); //Create Instance at X/Y
    ############################################################################################
    and here is the scrLoad():
    Code:
    ///scrLoad();
    
    ini_open('Save.ini'); //Open save ini
    var InstNum = ini_read_real( 'Save', '0', 0); //Load number of instances
    
    //Create grid
    globalvar SaveGrid; //Create global variable for grid
    SaveGrid = ds_grid_create(InstNum, 9); //Create grid as wide as building number
    
    //Write saved grid to new grid
    ds_grid_read(SaveGrid, ini_read_string('Save', '1', "")); //Populate grid with x/y/etc.
    
    ini_close(); //Close ini
    
    for(var a = 0; a <= InstNum; a ++){
    
        var Inst = instance_create(SaveGrid[# a, 1],SaveGrid[# a, 2],SaveGrid[# a, 0]); //Create Instance at X/Y
        Inst.Cost = SaveGrid[# a, 3];           //Building cost
        Inst.Occupants = SaveGrid[# a, 4];      //Number of occupants
        Inst.Name = SaveGrid[# a, 5];           //Little Human name variable
        Inst.HP = SaveGrid[# a, 6];             //Little human hp
        Inst.Money = SaveGrid[# a, 7];          //Little human money;
        Inst.image_index = SaveGrid[# a, 8];    //House sprite;
        
        Inst.Placed = 1; //Flag inst placed
        Inst.depth = -y; //Set depth of inst
        
        
    }
    
    Can someone help me with this?

    Thank you in advance.
     
  2. Anton Provid

    Anton Provid Member

    Joined:
    Jun 11, 2019
    Posts:
    14
    with SaveGrid[# a,0] is represent for:
    Code:
    with(oParent_Placed){
        SaveGrid[# a, 0] = object_index; //Object to create
        SaveGrid[# a, 1] = x //Building x
        SaveGrid[# a, 2] = y //Building y
        SaveGrid[# a, 3] = Cost //Building cost
        SaveGrid[# a, 4] = Occupants; //Number of occupants
        SaveGrid[# a, 5] = Name; //Little Human name variable
        SaveGrid[# a, 6] = HP; //Little human hp
        SaveGrid[# a, 7] = Money; //Little human money;
        SaveGrid[# a, 8] = image_index; //House sprite;
        a++;
        
    }
     
  3. obscene

    obscene Member

    Joined:
    Jun 21, 2016
    Posts:
    2,382
    SaveGrid[# a, 0] is returning -4. So check and see if it exists or has a value first before trying to create it.
     
  4. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,453
    Error message says "undefined", which is different from noone, right? "Undefined" values from a ds_grid should mean you're reading from outside of the grid, so maybe you loop "a" too many values?
     
    Anton Provid and obscene like this.
  5. obscene

    obscene Member

    Joined:
    Jun 21, 2016
    Posts:
    2,382
    You're right, been a while since I coded :p
     
  6. Anton Provid

    Anton Provid Member

    Joined:
    Jun 11, 2019
    Posts:
    14
    Thank for your response, so can you help me explain or suggestion: how to solve this problem?
     
  7. Anton Provid

    Anton Provid Member

    Joined:
    Jun 11, 2019
    Posts:
    14
    yes, but i don't know how to do that, sorry i'm newbie
     
  8. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,453
    Check all loops accessing the grid and make sure you don't scroll outside the grid (e.g. using < instead of <= or vice versa changes the loop scope by 1). Or you could use is_undefined on the value you read, and if it is undefined, don't use it (you can use the continue keyword inside a loop to abort the current iteration and go to the next value right away).
     
    Anton Provid likes this.
  9. Anton Provid

    Anton Provid Member

    Joined:
    Jun 11, 2019
    Posts:
    14
    wow, yes, i'm also using <= in loop so maybe it is the cause of problem. i will check it again. Btw, thank for your respond because this helps me so much.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice