GML Creating sprite from surface on a certain layer

I'll try to explain what I want to do the best I can.

I've been looking into the function sprite_create_from_surface, so I can save a sprite from whatever is drawn in a certain spot on my screen.

What I need for my design is actually to create this sprite "behind the curtains". The player will be seeing whatever I want him to see, but on deeper layers I'll be spawning some sprites to create a random character, and the player can't see that yet. How to use sprite_create_from_surface, and specify that I want to use the surface at the right layer?


Not 100% on this because I keep forgetting to study this aspect of GMS2, but you can specify what surface you want a layer to draw to, similar to how in GM8 you could specify a surface for a view to draw to (maybe you can still do that in GMS2).

Basically, just create two scripts: one that simply sets the surface target (and probably clears it), and the other that resets the surface target. Get your layer ID and then in the Room Start event run two lines:

layer_script_begin( your layer ID, the script that sets the surface target );
layer_script_end( your layer ID, the script that resets the surface target );

And that's about it. Just be aware it runs for each draw event, so the manual suggests you consider checking the event_type in case you do anything strenuous or in case you want some events to render to the application surface.

I also don't know if you can force a layer to draw itself in multiple events. So yeah, can't help you much more beyond that right now, if it even helps at all.
My understanding is that once you create the Sprite it doesn't automatically appear, you have to then draw the Sprite separately. So you create the Sprite from surface then separately you draw the new Sprite. You will need to draw the Sprite from an object at the depth you want it to appear. So create Sprite, then create a new empty object at the depth you want then in that object draw your Sprite.