Learn JavaScript in Construct, part 1: getting started

52

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

23,468 visits, 57,134 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

Using developer tools

The browser console is part of the browser's developer tools, or "dev tools" for short. Getting to developer tools might seem like a bit of a detour when you're just getting started, but it's well worth it, for two reasons:

  1. Developer tools, and things like logging messages to the console, are an essential part of the day-to-day work of programming. It's definitely worth learning about them and getting familiar with how they work.
  2. The browser console is also a really useful tool for helping you learn JavaScript. You can type in snippets of JavaScript, run them and see their result immediately, which is a great way to experiment and learn more. We'll try that out later on.

Opening dev tools

You can open dev tools for any browser window or tab. However we only want dev tools for the preview window where your project runs, as that's where your JavaScript code runs too. While you can open dev tools for the Construct editor, it's not useful here.

So first of all preview your project by clicking the "Play" icon in the toolbar near the "Menu" button. (You can also preview by pressing F5.)

The Preview button in Construct 3

Construct will open a popup window with a blank white screen, as there's nothing in the layout. Make sure the preview window is the active window - you can click inside the preview window to make sure it's the active window - then:

  • In Safari, press Command + Option + I.
  • In any other browser, press F12.

A new window should open for the browser developer tools. Click the Console tab near the top to make sure you're viewing the list of console messages. You should see Hello world! in the list of messages! There may be some other messages that Construct logged itself, but you can ignore those.

The browser developer tools console

Congratulations! You have run your first line of JavaScript code and seen the message it logged.

Changing the message

In Construct you can try changing the message that is logged. (Remember to double-click the script block to edit it, and click outside to finish.) Any text between the double quotes will be shown in the console. For example you could try:

console.log("My super cool message!");

Preview the project, open dev tools, and you can see your updated message.

Testing code in the console

As mentioned previously, a great feature of the console is that you can also type in code and see it run immediately. This is a very handy way to try things out. At the bottom of the list of messages in the console should be an input field where you can enter some code. Try typing 1 + 1 and press Enter. It will instantly run this as JavaScript code and log the result.

Writing code in the console

You can also try entering the console log code inside the console, and see it print the message there.

Logging to the console from the console

You can enter code in to the console this way at any time and immediately see the result - or an error if you entered something incorrectly. This makes it a great way to quickly experiment with lots of different variations of code, and learn more about how JavaScript works and what results you get from different code snippets. Do feel free to try it out at any point during this guide and play with different code samples - it's a great way to learn more.

Conclusion

In this part we've covered:

  • About this guide, and what you'll learn in this series
  • How to add some JavaScript code in an event sheet in Construct
  • Using console.log() to add a message to the browser console
  • How to open the browser developer tools and view the console
  • How to enter code in the console to quickly try it out

This guide will often use the browser console for logging the results of code, and it provides an excellent place to experiment with code, so it's worth the time to get familiar with it.

You can download a Construct project file for this part of the guide here:

.C3P

learn-js-part-1.c3p

Download now 47.67 KB

Part 2

When you're ready to continue, head to the next part at Learning JavaScript in Construct, part 2: language basics!

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