Choose script file to run from Event sheet

1 favourites
  • 4 posts
From the Asset Store
Full game Construct 2 and Construct 3 to post on Google Play
  • Hey, like the topic says. I have two script files

    "Gun.js" with

    function show()
    {
    	alert("Gun");
    }
    

    inside and "Laser.js" containing

    function show()
    {
    	alert("Laser");
    }
    

    and in Event sheet there's just a

    Button: On Click -> show()

    Pressing a button at runtime will always fire "Gun.js" probably because of the alphabetic list order.

    How can I make it to run specific script file

    So when I press Button1 - run show() from "Gun.js"

    and when I press Button2 - run show() from "Laser.js"

    I'm aware that my functions naming is bad and making it for example showGun() and showLaser() will solve all of my problems but I think that's an interesting question to ask. Imagine having a lot of long scripts and having bugs just because you forgot and make couple of functions with same name.

    Tagged:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • JavaScript doesn't work like that. The second function overwrites the first one so it is no longer available. You need to give the functions different names.

  • you can also wrap each of your functions in a class

    class gun
    {
    	constructor() {}
    	show() {
    		alert("gun");
    	}	
    }
    
    class laser
    {
    	constructor() {}
    	show() {
    		alert("laser");
    	}	
    }
    

    then you would need to create instances of each of those classes

    const gunInst = new gun();
    const laserInst = new laser();
    

    then on your button click you can do

    gunInst.show();
    
  • Another approach is to use objects like namespaces, e.g. in Gun.js:

    const Gun = {
    	show()
    	{
    		alert("Gun");
    	}
    };
    

    and in Laser.js:

    const Laser = {
    	show()
    	{
    		alert("Laser");
    	}
    };
    

    Now you can call Gun.show() or Laser.show(). If each file puts everything in to the same "namespace" (really just a named object), then you have a way to distinguish functions, variables etc. with the same name.

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