Learn JavaScript in Construct, part 1: getting started

50

Index

Features on these Courses

Attached Files

The following files have been attached to this tutorial:

.c3p

learn-js-part-1.c3p

Download now 47.67 KB

Stats

22,515 visits, 55,036 views

Tools

License

This tutorial is licensed under CC BY-NC 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 24 Sep, 2021. Last updated 1 Apr, 2022

Adding JavaScript code in an event sheet

As an easy way to get started, let's write our first line of code in an event sheet. First open the Construct editor and create a new empty project, making sure Start with is set to Event sheet.

Creating a new project

Then click the Event sheet 1 tab to switch to the event sheet view.

Switching to event sheet tab

Now add an On start of layout event:

  1. Click Add event in the event sheet tab
  2. Double-click System
  3. Double-click On start of layout (in the Start & end group)

You should end up with this:

On start of layout

This is an event block that will run its actions on startup.

We can add some JavaScript code in the place of an action in this event block, so our code runs on startup. Click Add... to the right of the block, and select Add script.

Adding a script action

A text field appears as an action. This is a small script editor where you can enter some JavaScript code to run.

Editing a script action

For now, let's just copy-and-paste in the following line of code in to that text field:

console.log("Hello world!");

Note if you switch away to a different window, the script editor may finish and change in to an empty code block. If that happens, double-click the empty code block to edit it again, and then paste in or copy out the code sample above. Then click outside the script editor, or press Ctrl + Enter, to close the script editor.

You should now see your first line of JavaScript code in the event sheet!

Your first line of JavaScript code!

This line of code will log the message Hello world! to the browser console on startup. We'll find how to see that message shortly. To help get you started quickly we're not going to fully explain every part of this code just yet - this guide will go in to detail on it later on. For now though here is a brief summary of what this line of code means:

  • console.log means "access the log function on the console object"
  • The parenthesis ( and ) mean "call the log function"
  • "Hello world!" is some text, provided inside double-quotes, that is passed to the log function.
  • The semicolon ; at the end marks the end of a statement

The end result means "add a message with the text Hello world! to the console". Let's find out how to see our message!

  • 14 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • How do I draw a line with Javascript in Construct3?

  • Wonder why Construct 3 writes certain words like "log" in red. Doesn't red mean an error? Confusing.

    Otherwise, how do I define an error in code?

  • I am using Google Chrome, and after entering the script, pressing f12 will display unnecessary scam errors. How can I solve this problem? "Stop!

    main.js:3009 This is a browser feature intended for developers. If someone told you to copy and paste something here to enable a feature or "hack"..."

      • [-] [+]
      • 1
      • Ashley's avatar
      • Ashley
      • Construct Team Founder
      • 1 points
      • (0 children)

      That means you opened dev tools for the editor, not preview. The message includes the text: "Note: this is the developer tools console for the Construct editor. If you're looking for developer tools for preview, be sure to open developer tools for the preview window instead."

      Make sure the preview window is focused when you press F12.

  • I wrote a javascript file in files folder - how do I import that?

  • I'm using the Vivaldi browser. And if I install Construct as a web app--a stand-alone window. When I hit F12, the whole browser quit, not just the stand-alone window but Vivaldi force quit on me. I have to use editor.construct.net inside the browser to use F12 successfully.

  • Load more comments (6 replies)