GMS 2 Getting the names of the objects of all instances in a layer?

G

greggman

Guest
I'm trying to iterate through layers and then all the elements in each layer and print out the names. I figured out how to do this for sprites but not for instances

The short version is I call object_get_name but I'm guessing I need to do something else.

Code:
      var __inst = layer_instance_get_instance(__id);
      var __name = object_get_name(__inst);   // <==== this just returns <undefined>
For sprites this code works though

Code:
      var __spr = layer_sprite_get_sprite(__id);
      var __name = sprite_get_name(__spr);

What should I do instead?

Here's the entire loop

Code:
var __layers = layer_get_all();
var __numlayers = array_length_1d(__layers);

var __i;
for(__i = 0; __i < __numlayers; __i++)
{
  var __layer_id = __layers[__i];
  var __els = layer_get_all_elements(__layer_id);
  var __numels = array_length_1d(__els);
  show_debug_message("layer: " + string(__i) + " " + layer_get_name(__layer_id) + "[" + string(__numels) + "]");

  var __j;
  for(__j = 0; __j < __numels; __j++)
  {
    var __id = __els[__j];
    var __eltype = layer_get_element_type(__id);
    if (__eltype == layerelementtype_instance)
    {
      var __inst = layer_instance_get_instance(__id);
      var __name = object_get_name(__inst);     // <==== this just returns <undefined>
      show_debug_message("  instance:" + string(__j) + ": " + __name);
    }
    else if (__eltype == layerelementtype_sprite)
    {
      var __spr = layer_sprite_get_sprite(__id);
      var __name = sprite_get_name(__spr);
      show_debug_message("  sprite: " + string(__j) + ": " + __name);
    }
    else if (__eltype == layerelementtype_background)
    {
      show_debug_message("  background: " + string(__j));
    }
    else if (__eltype == layerelementtype_tilemap)
    {
      show_debug_message("  tilemap: " + string(__j));
    }
    else if (__eltype == layerelementtype_particlesystem)
    {
      show_debug_message("  particlesystem: " + string(__j));
    }
    else if (__eltype == layerelementtype_tile)
    {
      show_debug_message("  tile: " + string(__j));
    }
    else
    {
      show_debug_message("  unknown: " + string(__j));
    }
  }
}
I have a feeling those instance ids are not what I need to get a name.

Would also like to be able to get names of tiles as well or rather the name of the tileset they came from and all the other objects as well but for now just being able to get instance names would be useful.
 
Use __inst.object_index as the parameter for object_get_name()

object_get_name() requires an object_index, you are passing it an instance id at the moment.
 

TheouAegis

Member
For tilesets, what about sprite_get_name(tilemap_get_tileset(layer))? The manual said they're classified as a special kind of sprite...
 
Top