• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code Layer instance getting issues

gnysek

Member
I was reading manual searching for new functions, and I found layer_set_target_room() and layer_has_instance() which interested me. So, I've started to play with them, to check that they are working as I think.

I've created a new game, two rooms, one object and placed instance in second room. Then, in creation code of first room, I've tried to search for instance in another room. Documentation says, that I should be able to create/modify/delete instances in another room thanks to it. I saw a warning saying that I cannot create instances (so only sprites then?), but I've tried my luck.

First thing I've noticed, is that you cannot check layer type. You can check type of element on layer, but this requires to get all elements and is much slower. But since it is possible, I was still investigating. So I've come to a moment, when I can check with layer_has_instance() that instance exists on given layer. Since I have only one instance, it's ID is 100000 (Argument0 in below example).
Code:
    var i, j = room_last, k, l;
   
   show_debug_message("----\r\nsearching for " + string(argument0));
   
   for(i=0; i<=j; i++) {
       show_debug_message(room_get_name(i));
       layer_set_target_room(i);
       
       var _layers = layer_get_all();
       
       for(k=0; k<array_length_1d(_layers); k++) {
           show_debug_message( layer_get_name( _layers[k] ) );
           
           show_debug_message(layer_has_instance(_layers[k], argument0));
       }
   }
   layer_reset_target_room();
So, for all checking I've got -1, which mean there's no instance on this layer.
But documentation says, I can also use object index to check, so I've tried this wishing for luck:
Code:
show_debug_message(layer_has_instance(_layers[k], object_0));
All went OK except for layer on which this instance really is. Game just crashed, without even showing:
Code:
Runner.exe exited with non-zero status (-1073741819) elapsed time 00:00:06.9797795s
FAILED : Run Program Complete
So, we have one bug here.
But there was another function in docs, which was looking promising - layer_instance_get_instance() - which takes element id as argument. So all what I should do was to get elements for layer, iterate one after one and check. In normal game this may be slow, but since I have only one instance - who cares. Of course since to skip error, I shouldn't use this function on sprites etc., so I've added checking on first element on layer - if it's type isn't instance, then break for-loop (that's why I said it's stupid you cannot check layer type directly, case we cannot have mixed types here).
Code:
    var i, j = room_last, k, l;
   
   show_debug_message("----\r\nsearching for " + string(argument0));
   
   for(i=0; i<=j; i++) {
       show_debug_message(room_get_name(i));
       layer_set_target_room(i);
       
       var _layers = layer_get_all();
       
       for(k=0; k<array_length_1d(_layers); k++) {
           show_debug_message( layer_get_name( _layers[k] ) );
           
           var _el = layer_get_all_elements(_layers[k]);
           for (l=0; l<array_length_1d(_el); l++) {
               if layer_get_element_type(_el[l]) != layerelementtype_instance {
                   break;
               }
               show_debug_message("get instance");
               var _inst = layer_instance_get_instance(_el[l]);
               if (_inst == argument0) {
                   show_debug_message("found");
                   show_debug_message(_inst);
                   show_debug_message(_inst.x);
               }
           }
       }
   }
   layer_reset_target_room();
OK, so what happens here ? It founds this instance, it even prints 100000 in console, but of course when asking for x position - game breaks.
Code:
Unable to find any instance for object index '100000' name '<undefined>'
 at gml_Script_find_instance (line 29) -                                    show_debug_message(_inst.x);
If that would be a sprite, I could easily get it's position with layer_sprite_get_x(), but for instances - there's no solution.

When writing this post I understand that to solve my issue with getting instance X/Y I can just put sprite on another layer and hide this layer, and use it's name to get this value. So I've put sprite in room, it got name "graphic_4F4E96F3", but then another issue came in - I cannot use this value in code, as graphic_XXXXXXX ids aren't converted to constants same as inst_XXXXXXXX which can be used in code. But, with some luck I found, that if it's used as string, in layer_sprite_get_id() it actually works! So, would be nice to have this also working for instances, and I'm wondering is it a bug or not in fact...

Code:
    var i, j = room_last, k, l;
   
   show_debug_message("----\r\nsearching for " + string(argument0));

   for(i=0; i<=j; i++) {
       show_debug_message(room_get_name(i));
       layer_set_target_room(i);
       
       var _layers = layer_get_all();
       
       for(k=0; k<array_length_1d(_layers); k++) {
           show_debug_message( layer_get_name( _layers[k] ) );
           
           var _el = layer_get_all_elements(_layers[k]);
           for (l=0; l<array_length_1d(_el); l++) {
           
               if (layer_get_element_type(_el[l]) != layerelementtype_sprite) {
                   break;
               }
               var _e = layer_sprite_get_id(_layers[k], string(argument0));
               if (_e > 0) {
                   show_debug_message(layer_sprite_get_x(_e));
               }
           }
       }
   }
   layer_reset_target_room();
Anyone came to similar issues?
 
Top