Learn JavaScript in Construct, part 1: getting started

47

Index

Attached Files

The following files have been attached to this tutorial:

.c3p

learn-js-part-1.c3p

Download now 47.67 KB

Stats

17,856 visits, 45,082 views

Tools

Translations

This tutorial hasn't been translated.

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.

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!

Next Tutorial In Course

Learn JavaScript in Construct, part 2: language basics 15:47

Learn some of the basic features of the JavaScript programming language, such as comments, variables and data types.

  • 12 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • 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?

  • How do I draw a line with Javascript in Construct3?

  • 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.

  • "If you can't open dev tools with F12 in the Edge browser, go to settings and check Use F12 key to open the Developer tools is enabled."

    I found what you stated ... now I hope it works... Thanks

  • Load more comments (5 replies)