runtime not defined

  • hello everyone, sorry for my silly question, I've just started working with construct and scripting. I prefer to use scripting than the visual editor but I'm running into an issue in calling objects from script.

    runOnStartup(async runtime =>
    {
    	// Code to run on the loading screen.
    	// Note layouts, objects etc. are not yet available.
    	
    	runtime.addEventListener("beforeprojectstart", () => OnBeforeProjectStart(runtime));
    });
    
    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)
    {
    	// Code to run every tick
    	jumpPowerBarRun();
    }
    
    var state = 0;
    var size = 0;
    
    function jumpPowerBarRun (){
    	if (state == 0){
    		//alert(" this is running");
    		console.log("jumpPowerBarRun ");
    		console.log(runtime.objects.JumpPowerBar.width);
    		//runtime.objects.JumpPowerBar.setSize(runtime.objects.JumpPowerBar.width - 5);
    		//size = size + 5;
    		//console.log(size);
    		
    	}
    }

    right now I'm just trying to print some info from the object to the console but I keep getting uncaught exceptions.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You have two issues.

    1) You need to pass the runtime into your function jumpPowerBarRun. It should be jumpPowerBarRun(runtime)

    2) using runtime.objects.JumpPowerBar. That doesn't return an object for you to work with. It gives you access to functions to get instances that exist in your layout

    You would have to do something like runtime.objects.JumpPowerBar.getAllInstances() or runtime.objects.JumpPowerBar.getFirstInstance(). When you get all you will have to loop through them, but get first returns the object so you could then do runtime.objects.JumpPowerBar.getFirstInstance().width.

    There are other methods that you can use

    https://www.construct.net/en/make-games/manuals/construct-3/scripting/scripting-reference/object-interfaces/iobjectclass

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