I want to have a tick and OnBeforeLayoutStart on every js page.

  • Currently, there is only one tick and OnBeforeLayoutStart,. I want to put the function into them. I can only go to the first js page and put it in.

    How can I have a tick and OnBeforeLayoutStart, in every page without affecting and covering other pages?

    I've written a lot of methods in class that ready and update, correspond to c3 tick and OnBeforeLayoutStart.

    But there is only one tick and OnBeforeLayoutStart function. This allows me to put all the things I need to call in the only one.

  • 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".

    1. Create a global variable to store the current active layout.
    2. Add an event listener and a corresponding function to every layout.
    3. In the Tick function call your layout Tick functions and check if the current layout is active.
    4. 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){
    
    }
    

    Hope this helps.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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".

    1. Create a global variable to store the current active layout.
    2. Add an event listener and a corresponding function to every layout.
    3. In the Tick function call your layout Tick functions and check if the current layout is active.
    4. 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){
    
    }
    

    Hope this helps.

    This is great!!!!!!!!~~~~~~~~ Thank you!!!!!!!!, brother! That's exactly what I want! Thank you for your careful answer!

  • 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".

    1. Create a global variable to store the current active layout.
    2. Add an event listener and a corresponding function to every layout.
    3. In the Tick function call your layout Tick functions and check if the current layout is active.
    4. 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){
    
    }
    

    Hope this helps.

    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?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)