Runtime is not defined error

0 favourites
  • 3 posts
From the Asset Store
7 Errors
$15 USD
7 Errors is a game where you have to find the 7 mistakes before time runs out. Can you find all 7? Have fun looking.
  • Hi,

    I am trying to create a script called modules.js, but I can't use runtime anywhere, because it shows as undefined. Here is my code, attempting to do what Ashley suggests:

    // Import any other script files here, e.g.:
    // import * as myModule from "./mymodule.js";
    
    function getRuntime(runtime) {
    	return runtime;
    }
    
    import animate_playbackState from "./animate.js";
    import animate_directionalState from "./animate.js";
    
    // Global variables
    var playerInstance = getRuntime(runtime).objects.iPlayer.getFirstInstance();
    
    runOnStartup(async runtime =>
    {
    	// Code to run on the loading screen.
    	// Note layouts, objects etc. are not yet available.
    	
    	runtime.addEventListener("beforeprojectstart", () => OnBeforeProjectStart(runtime));
    });
    
    async function OnBeforeProjectStart(runtime)
    {
    	// Code to run just before 'On start of layout' on
    	// the first layout. Loading has finished and initial
    	// instances are created and available to use here.
    	
    	runtime.addEventListener("tick", () => Tick(runtime));
    }
    
    function Tick(runtime) {
    	// Animate.js
    	if (animate_playbackState) {
    		switch (animate_directionalState) {
    			case 'up':
    				playerInstance.angle = 270;
    			case 'down':
    				playerInstance.angle = 90;
    			case 'left':
    				playerInstance.angle = 180;
    			case 'right':
    				playerInstance.angle = 0;
    		}
    	}
    }
    
  • function getRuntime(runtime) {
    	return runtime;
    }

    This function is pointless - it just returns its parameter.

    var playerInstance = getRuntime(runtime).objects.iPlayer.getFirstInstance();
    

    This line is run at the top level, so it is run before the Construct runtime exists, therefore runtime cannot be used. The runtime only exists when the callback to runOnStartup() is executed. So you have to wait until at least that point in time or later before trying to use the runtime.

    A typical way to do that is to declare the global variable as set to null initially, and then assign it later on.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Ashley,

    Declaring such a variable creates scope issues with the others. Moving all the variables also prevents 'export' from working due to scope rules. Unfortunately, JavaScript also doesn't allow type setting. See https://developer.mozilla.org/en/docs/web/javascript/reference/statements/export for the export syntax. It is quite strict on where variables come from. It seems to specifically be for me, because if you do such a thing (without runtime of course) on my website, it will work. However, 'import' will refuse it.

    > function getRuntime(runtime) {
    	return runtime;
    }
    

    This function is pointless - it just returns its parameter.

    This is what you said to try in one of your tutorials, Ashley

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