Hello,
every script you create in the Construct 3 Scripts Folder is accessable everywhere in the runtime. So you could do something like this on your "Startscript".
- Create a global variable to store the current active layout.
- Add an event listener and a corresponding function to every layout.
- In the Tick function call your layout Tick functions and check if the current layout is active.
- The onbeforelayoutstart functions and the specific Tick functions are in their corresponding layout script files.
> let g_currentLayout = "Layout 1";
runOnStartup(async runtime =>
{
runtime.addEventListener("beforeprojectstart", () => OnBeforeProjectStart(runtime));
});
async function OnBeforeProjectStart(runtime)
{
runtime.getLayout("Layout 1").addEventListener("beforelayoutstart", () => onBeforeLayoutStart_Layout1(runtime));
runtime.getLayout("Layout 2").addEventListener("beforelayoutstart", () => onBeforeLayoutStart_Layout2(runtime));
runtime.addEventListener("tick", () => Tick(runtime));
}
function Tick(runtime)
{
if(g_currentLayout == "Layout 1")Tick_layout1(runtime);
if(g_currentLayout == "Layout 2")Tick_layout2(runtime);
}
Here is a possible project structure:
On every layout script page you now have:
> function onBeforeLayoutStart_Layout1(runtime){
}
function Tick_layout1(runtime){
}
Hello! Now we have another problem. If I have several pages, they are all on the same layout. I want to add "start" and "tick" to every page. The above method can satisfy that each page corresponds to one layout, but what about multiple pages corresponding to one layout?
If I use the above method, add a listener to each page, just change the name of the function behind, it feels too troublesome.
Is there a way to declare a layout listener that can be placed on multiple page pages?