Declaring objects in a script block so they are accessible from other blocks?

From the Asset Store
Physics Block Puzzle game template for Construct 3
  • So I'm trying to run ffmpegwasm in a construct project for something, but I really don't know what I'm doing so much. I've managed to include it as a 'main script', and initialize it in a script block:

    const { createFFmpeg, fetchFile } = FFmpeg;
    const ffmpeg = createFFmpeg({ log: true });
    

    Then I got it to work by setting up a few variables, and running the following code:

    ffmpeg.FS('writeFile', "video.mp4", await fetchFile(localVars.VideoURL));
     await ffmpeg.run(...parametersArray);
    	 runtime.callFunction("ffmpegComplete", result)
    
    

    This all works wonderfully, as long as it initializes and runs in the same scripting block. But that means every time I want to run ffmpeg, I need to initialize again!

    If I initialize at the start of the layout and then call it later, it says 'ffmpeg is not defined' - seems it doesn't have access to any functions, variables and other stuff that's created on another block.

    I've been reading up on it and I imagine the solution involves imports and exports, which I haven't been able to figure out how to use yet (I know very little javascript). Am I in the right path? Is there a simpler way for me to run that first bit of code so that the 'ffmpeg' object (as well as declared functions, variables etc.) is accessible to other blocks of javascript in my event sheet?

    Thanks in advance!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I have found the solution!

    Apparently, the reason why you can't access things created on other blocks is because each block is a javascript function internally - so anything you declare normally is available only within the function's (or block's) namespace.

    There is a simple workaround, though - instead of declaring variables, functions etc. the normal way, you can set them by the window object -

    window.myVar = 'mystring' 
    // as opposed to: 
    var myVar = 'mystring'.
    

    Applying the same concept to the ffmpeg object worked seamlessly, and to 'copy' a function you use in the block into the window object you can simply encapsulate it like this:

    window.myFunc = function(param) { return actualFunction(param) }
    

    Then, later on, you can access the stuff you put in the window object in any script block like this:

    window.myFunc(param); // runs the actualFunction() as defined in the other script block, forwarding the parameter and returning whatever that function returns.
    alert(window.myVar); // alerts 'myString', as defined in the other script block 
    

    Not super elegant, hehe, but gets the job done!

  • I'm not sure if it's better or worse, but there is also the globalThis top level identifier for things like this also.

    Described in more detail here:

    construct.net/en/make-games/manuals/construct-3/scripting/using-scripting/javascript-construct

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