Ok, this title might sound a bit weird, but I have to say that this is exactly what I am trying to do.
A bit of context
I recently started working on a project of mine that I've been planning to do for a very long time and that I spent a lot of energy preparing. In fact the only reason Spritefont Deluxe, Skin it and a few other free addons I made even exist. So let's say that I put a lot of thought into this. Way more than the healthy amount.
Anyway, as I started working, me and my friend and collaborator started discussing the technical part of the UI. The "how", now that the "what" was cleared out.
There are tons of ways to make UI elements for your game, but one of my favourite ways is to use a given 3rd party addon pack that makes the whole process of creating UI a lot easier.
That addon pack, as awesome as it is has a few issues that come with its ease of use. When creating a dialog window, you need to set it on its own layer. This is so the addon can know which objects are on the dialog box or not (aka every object from the same layer is on it). This is great, but this means that in a complex UI environment where I need a few dialog boxes I need to add as many layers to every layout that uses that UI.
I don't know you, but adding lots of global overridden layers to every layout that needs my menus on them sounds like boring stuff to me. So boring I'd rather find a solution that might break the engine than actually doing this.
Break the engine?
Yeah don't worry about it.
Basically here's the catch. I'm too lazy to add the layers to every layout, so I'd rather have a system doing that for me. I can't make a system that adds the layer in the editor, and nothing in the SDK indicates that this is even possible.
So it's not possible?
Listen. I'm not adding those layers by hand alright? Who do you think I am? Some sort of hard working indie game developer?
I knew that doing very advanced stuff with the engine was possible thanks to Toby and his amazing Preloader plugin. Follow him on Twitter btw: twitter.com/TobyReeno and check his plugin on the store: scirra.com/store/construct2-plugins/mmpreloader-layout-preloader-3620
His plugin meant that through some unconventionnal use of the available SDK, some deep digging in the runtime code and some patience, it was possible to do some pretty cool stuff.
So I started doing just that. I started reading the engine's code.
It's possible then?
Yes. No. Kind of? I managed to create new empty layers and copy layers from other layouts just fine using this code:
var layout = this.runtime.running_layout;
var layer = this.runtime.layouts_by_index.layers; // whatever method you use to get other layers.
//You can also create a new layer by calling the constructor in cr
layer = new cr.layer(layout, layerData) // With layerData being an array containing all of the layer's base info. You can get a look at what it looks like by logging the variable m in the Layer's constructor in layout.js
layer.number = layout.layers.length;
I talk about this in more details in a dedicated forum topic that I'll keep updated with new stuff I find, but basically, there are issues:
So in the end, can I create layers out of laziness? Yes, in theory.
I know that I can create empty layers and then fill them with already existing objects. This means that depending on how this gets sorted out, I can solve the issues in a few ways.
Worst case scenario
I don't manage to do anything more than what is currently possible. In that case I'll need to put all of my UI on a single global layer that I'll need to add to every layout, and then create all the needed empty layers at runtime and move all of the objects to the right layer. This should work, but I'll need to make sure that C2 doesn't freak out on the 1st frame (when everything is created and set up)
Possible case scenario
I manage to copy a layer's objects as well and have them created, which means that I'll only need to copy all of the UI layers I need during runtime and it should all work perfectly well.
Best case scenario
I can figure out how to use the properties of global layers during runtime and I'll only need to create empty layers named right and C2 will do all of the hard work for me, lowering the chances of me breaking anything. Though this case might be very unlikely to happen.
The morality of all this is. Do not try to put yourself on the path of a man seeking laziness, you might not comprehend the amount of willpower he might be willing to put into not putting his willpower into anything else.