Attach script at runtime

0 favourites
  • 3 posts
  • How i can attach a script in the "Files"-Folder at runtime?

    runOnStartup(async runtime =>
    {
    	// Code to run on the loading screen
    	
    	runtime.addEventListener("beforeprojectstart", () => OnBeforeProjectStart(runtime));
    	
    });
    
    function OnBeforeProjectStart(runtime)
    {
    	// Code to run just before 'On start of layout'
    	// on the first layout. Initial instances are created
    	// and available to use here.
    	runtime.layout.addEventListener("beforelayoutstart",
    								 () => OnBeforeLayoutStart(runtime));
    	
    	runtime.addEventListener("tick", () => Tick(runtime));
    	
    
    }
    
    function OnBeforeLayoutStart(runtime)
    {
    	// Attach the script from the files folder..
    	var filesScript = document.createElement('script');
    	filesScript .setAttribute('src','testscript.js'); // Don't work
    	document.head.appendChild(filesScript );
    	console.log("-------------------------------");
    }
    
    function Tick(runtime)
    {
    	// Code to run every tick
    }
    

    I got the error "Failed to load resource: the server responded with a status of 404 (Not Found)".

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • In preview mode your files aren't uploaded anywhere, so they cannot be fetched normally. This is what the Asset Manager is for: it handles requests for you to make sure they work on all platforms. In this case you need to use something like this in an async function:

    scriptElem.src = await runtime.assets.getProjectFileUrl("myscript.js");
    
  • In preview mode your files aren't uploaded anywhere, so they cannot be fetched normally. This is what the Asset Manager is for: it handles requests for you to make sure they work on all platforms. In this case you need to use something like this in an async function:

    > scriptElem.src = await runtime.assets.getProjectFileUrl("myscript.js");
    

    I think it works. Thank you :)

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