1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Question - Code How do I change what layer an instance is on in GML?

Discussion in 'GameMaker Studio 2 Community Tech Support' started by hdarren, Nov 10, 2016.

  1. hdarren

    hdarren Member

    Joined:
    Jun 21, 2016
    Posts:
    280
    When I create an instance using "object = instance_create_layer()" how do I later change what layer that instance is on?
     
  2. Baku

    Baku Member

    Joined:
    Jun 21, 2016
    Posts:
    11
    Change the instance's layer variable directly, or use layer_add_instance()

    Code:
    object.layer = layer_get_id("LayerName");
    // or
    layer_add_instance(layer_get_id("LayerName"), object);
    
    Read the manual, dude ;)
     
  3. hdarren

    hdarren Member

    Joined:
    Jun 21, 2016
    Posts:
    280
    That is good.

    So now I can create a script called instance_create(x,y,object) which creates a instance_create_layer(x,y,default_layer,object) then in the Create event of the object I set layer=layer.

    This means my code is much tidier and if happen to change layer name or what layer object should be on I don't need to change a million bits of code. :)
     
  4. rwkay

    rwkay YoYo Games Staff YYG Staff

    Joined:
    Apr 12, 2016
    Posts:
    1,049
    you could also just use instance_create_depth() as an object no longer has a depth variable so you cannot set the depth of the object and every instance inherits it as that was silly - you now create the instance at a set depth (a layer will be auto created for you at that depth)....

    Personally I would just swap to instance_create_depth() if you are not using layers directly.

    Russell
     
  5. Could the same be done with the layers in the object editor and room editor for specific instances? Let's say I am making a isometric RPG, how would I go about isometric depth? Now that was just an example, for references to asking for help on assigning instances to individual instance layers. ;)
     

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