not set before reading it

Hi

I'm writing code to save my game. When it runs the save code I get the error message:

Variable <unknown_object>.sprite_index(25, -2147483648) not set before reading it.
at gml_Script_scr_drac_castle_save (line 20) - ds_map_add(instanceMap, "sprite_index", instance.sprite_index);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_scr_drac_castle_save (line 20)
called from - gml_Script_scr_castles_save (line 3) - scr_drac_castle_save();
called from - gml_Script_scr_save_game (line 10) - scr_castles_save();
called from - gml_Object_SaveCompleteObject_Draw_0 (line 8) - scr_save_game();
?
 

FrostyCat

Redemption Seeker
That just means instance doesn't hold a valid instance ID. Start by looking into where you set that variable and whether there's a chance of something invalid slipping into it.
 
NINJA'd by FrostyCat :)

That error is saying that you haven't defined 'instance'. It doesn't have either a specific id, or object, applied to it.

If you have defined 'instance' - is it within the object that is calling the scripts? If you believe you have defined it before accessing it, then you may need to include the code here, as it seems there is a mistake somewhere.

It's either not defined / is defined but not in the object calling the script / has gotten a value like "noone" or "undefined" that is not recognized (say you were getting instance ids from a list, and looped beyond the last list entry - it would get an unrecognized value but still be trying to treat it as an identifier)
 
Ok.

This is the object it is referencing:

Code:
if(file_exists("drac_castle.sav"))
{
    file_delete("drac_castle.sav");
    
}

var saveFile = file_text_open_write("drac_castle.sav");



        var instance = instance_find(DraculasCastleObject, 1);
        
        //convert instance to a ds_map
        var instanceMap = ds_map_create();
        //var list1 = ds_map_create();
        //
            
        ds_map_add(instanceMap, "sprite_index", instance.sprite_index);
        ds_map_add(instanceMap, "image_speed", instance.image_speed);
        ds_map_add(instanceMap, "x", instance.x);
        ds_map_add(instanceMap, "y", instance.y);
        
        ds_map_add(instanceMap, "global-castle_hp", global.castle_hp);
        ds_map_add(instanceMap, "death_sound_flag", instance.death_sound_flag);
        ds_map_add(instanceMap, "global-non_usable", global.non_usable);
        ds_map_add(instanceMap, "castle_timer", instance.castle_timer);
        ds_map_add(instanceMap, "global-ghost_flag", global.ghost_flag);
        
        ds_map_add(instanceMap, "damage_audio_flag_1", instance.damage_audio_flag_1);
        ds_map_add(instanceMap, "damage_audio_flag_2", instance.damage_audio_flag_2);
        ds_map_add(instanceMap, "damage_audio_flag_3", instance.damage_audio_flag_3);
        ds_map_add(instanceMap, "damage_audio_flag_4", instance.damage_audio_flag_4);
        ds_map_add(instanceMap, "damage_audio_flag_5", instance.damage_audio_flag_5);
        ds_map_add(instanceMap, "damage_audio_flag_6", instance.damage_audio_flag_6);
        
        ds_map_add(instanceMap, "global-you_lose", global.you_lose);
        
        
        

        JSONInstance = json_encode(instanceMap);
        
        file_text_write_string(saveFile, JSONInstance);
        //seperate with pipe character
        file_text_write_string(saveFile, "|");
        
        ds_map_destroy(instanceMap);       
        
        
        

///close the file
file_text_close(saveFile);
 

FrostyCat

Redemption Seeker
That just means you don't have a second instance of DraculasCastleObject to fetch. If you want to fetch the first instance, it should have been 0, not 1.
Code:
var instance = instance_find(DraculasCastleObject, 0);
 
That just means you don't have a second instance of DraculasCastleObject to fetch. If you want to fetch the first instance, it should have been 0, not 1.
Code:
var instance = instance_find(DraculasCastleObject, 0);
[/QUOTE
No. THat's what I had before. I changed it to 1 to see if it made a difference.
So same error with 0.
[EDIT]


___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object SaveCompleteObject:
Variable <unknown_object>.sprite_index(25, -2147483648) not set before reading it.
at gml_Script_scr_drac_castle_save (line 20) - ds_map_add(instanceMap, "sprite_index", instance.sprite_index);
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_scr_drac_castle_save (line 20)
called from - gml_Script_scr_castles_save (line 3) - scr_drac_castle_save();
called from - gml_Script_scr_save_game (line 10) - scr_castles_save();
called from - gml_Object_SaveCompleteObject_Draw_0 (line 8) - scr_save_game();
 

FrostyCat

Redemption Seeker
If you have the same error with 0, then there is no active instance of DraculasCastleObject in the current room for you to work with. Deal with that first before coming back to this piece of code, or check instance_exists(DraculasCastleObject) and skip the saving code if that comes up negative.
 
If you have the same error with 0, then there is no active instance of DraculasCastleObject in the current room for you to work with. Deal with that first before coming back to this piece of code, or check instance_exists(DraculasCastleObject) and skip the saving code if that comes up negative.

OK so:

Code:
        if (!instance_exists(DraculasCastleObject))
        {
            show_debug_message("Draculas Castle Doesn't Exist");   
        }
        var instance = instance_find(DraculasCastleObject, 0);
        
        //convert instance to a ds_map
        var instanceMap = ds_map_create();
        //var list1 = ds_map_create();
        //
            
        ds_map_add(instanceMap, "sprite_index", instance.sprite_index);
        ds_map_add(instanceMap, "image_speed", instance.image_speed);
        ds_map_add(instanceMap, "x", instance.x);
        ds_map_add(instanceMap, "y", instance.y);

It shows up as "Draculas Castle Doesn't Exist"

I don't know how this can be correct when I am sitting here looking at the objecft on my screen.
 

FrostyCat

Redemption Seeker
OK so:

Code:
        if (!instance_exists(DraculasCastleObject))
        {
            show_debug_message("Draculas Castle Doesn't Exist");  
        }
        var instance = instance_find(DraculasCastleObject, 0);
       
        //convert instance to a ds_map
        var instanceMap = ds_map_create();
        //var list1 = ds_map_create();
        //
           
        ds_map_add(instanceMap, "sprite_index", instance.sprite_index);
        ds_map_add(instanceMap, "image_speed", instance.image_speed);
        ds_map_add(instanceMap, "x", instance.x);
        ds_map_add(instanceMap, "y", instance.y);

It shows up as "Draculas Castle Doesn't Exist"

I don't know how this can be correct when I am sitting here looking at the objecft on my screen.
If instance_exists() tells you it doesn't exist, then it doesn't exist. You're likely looking at a residual image or something else that has accidentally acquired the appearance of DraculasCastleObject. Use the debugger's All Instances watch tab to inspect what's really there, don't rely on superficial appearances.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
I have tidied the topic to remove a few posts that were not directly related to the issue at hand. If people want to discuss different learning behaviours and methods, and the pros/cons of asking for help, then make a topic in the Community forum. I would also suggest that if you are tired and grumpy, then maybe reconsider posting until you've had a good night's sleep (you know who you are) ... ;)
 
Top