Running a function in a js file.

0 favourites
  • 11 posts
From the Asset Store
Full game Construct 2 and Construct 3 to post on Google Play
  • Ok, so I have this function:

    function UUID() {
    	runtime.globalVars.UUID = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    		const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
    		return v.toString(16);
    	});
    }
    

    If I click add script to an event (and remove the function UUID() bits) and then assign the global var UUID to say a text object, it works exactly as intended.

    However, I plan on calling this a bunch of times. Seems a tad wasteful to keep copy/pasting this script all over the place. I'd like to put it in a js file in either the files or scripts folder (dunno which) and then call the function with an event. I tried the whole browser.execjs thing and it can't find it.

    So, the question is (after spending several hours digging through the JS tutorial to no avail), how can I call this as a function from an event????

    Oh and I tried this: runtime.callFunction("FunctionName")

    Strangely enough, the code completion for callfunction finds the function name.

    But apparently I'm not holding my mouth right. I put the function in files/stuff.js and I tried putting it in scripts/main.js. Doesn't find it.

    Thanks

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • and I tried this:

    globalThis.UUID = function UUID() {
    	runtime.globalVars.UUID = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    		const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
    		return v.toString(16);
    	});
    }

    And then

    globalThis.UUID;

    Doesn't produce an error but it doesn't appear to set the UUID global var to anything

  • Put the file into Scripts folder and change its purpose to "Import for events".

    However, if you need to access runtime in the function, you might have to pass it as a parameter.

  • Put the file into Scripts folder and change its purpose to "Import for events".

    However, if you need to access runtime in the function, you might have to pass it as a parameter.

    And tried it by just running UUID();

    Didn't work.

  • No, runtime.callFunction is for calling C3 functions defined in events. So to call just use UUID();

    But you need to pass runtime to it. So UUID(runtime);

    And in your script change the function to function UUID(runtime) {....}

    Look at my second screenshot.

  • I'm just trying to call it when I need it. It's purpose is to create a random nonce to pass to the server.

  • IT WORKED! THANK YOU... hours and hours spent on that.

    Now, one more question. I have a sha256 function I need to pass a string to.

    function sha256(ascii)

    How would I call it?

    function sha256(ascii, runtime)?????

  • Figured it out. passed a string and runtime, I got a hash back.

    Thanks again!!!!

  • You only need to pass runtime if it's used inside the function. (for example if the function needs to access C3 variables or C3 objects)

  • Right, both the uuid and sha are setting a c3 variable which will be ajax posted to the server. The general idea is a basic verification system that can't be copy/pasted.

    Saved me much gnashing of teeth. Appreciate it!

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