How do I move a script variable to an event sheet?

0 favourites
  • 6 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Bear with me, because I am very new to JS.

    Here is my code for getting the mouse button (including forward and backward) in JS:

    // Import any other script files here, e.g.:
    // import * as myModule from "./mymodule.js";
    
    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.
    	document.addEventListener("mousedown", (e) => {e.preventDefault(); mousedown(e.clientX, e.clientY, e.button)});
    	
    	
    	runtime.addEventListener("tick", () => Tick(runtime));
    }
    
    function mousedown(x, y, button){
    console.log(button);
    }
    
    function Tick(runtime)
    {
    	// Code to run every tick
    }
    

    Simply put, I want to use the 'button' number variable in an event sheet, with as little extra code useage as possible. Any ideas?

  • Have you tried this?

    runtime.globalVars.varName = button;

  • Have you tried this?

    runtime.globalVars.varName = button;

    Thanks for the response. I just tried this, but it doesn't seem to be working looking at the devtools. Any idea why?

    // Import any other script files here, e.g.:
    // import * as myModule from "./mymodule.js";
    
    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.
    	document.addEventListener("mousedown", (e) => {e.preventDefault(); mousedown(e.clientX, e.clientY, e.button)});
    	
    	
    	runtime.addEventListener("tick", () => Tick(runtime));
    }
    
    function mousedown(x, y, button){
    	console.log(button);
    	runtime.globalVars.mouseButtonNum = button;
    }
    
    function Tick(runtime)
    {
    	
    }
    
  • Here is the file if its an issue with the events or project structure or smth. dropbox.com/scl/fi/vo2upm80wnszgkgad2iik/mousebuttontest.c3p

  • You can do this:

    But I'm not an expert in JS, so this solution may not be a good one.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You can do this:

    But I'm not an expert in JS, so this solution may not be a good one.

    It works, thanks!

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