Overview
Choosing a good code editor will save you time and heartache. Core actually has a built-in code editor which you can use for this course, but we are recommending you use Visual Studio Code.
Setting up VSCode
To set up VisualStudio to open your Core Lua files:
Install Visual Studio Code
- Download Visual Studio Code from the website
- Install it by double clicking the downloaded file.
Find the File Path
Next, you will need to find out where the Visual Studio Code program is on your computer.
- If you have an icon shortcut on your desktop, you can right-click it and select Properties. The Target value is the path to the executable program.
- If you find it in a search in the Start menu, you can find the path by right-clicking and selecting Open file location…
- Most recent installs will most likely be at:
- C:\Users\YourUserNameHere\AppData\Local\Programs\Microsoft VS Code\Code.exe
- Check out this thread if none of the above suggestions works for you.
Connect Visual Studio Code to Core
- Open Core, and from the Create tab, create a new project or open an old one.
- Click Edit in the top menu bar, and select Settings…
- Paste the file path you found in the previous step into the box by External Script Editor. You can also find it in your File Explorer by clicking the … button.
Code Editor Skills
There are several features of a code editor like Visual Studio Code that make it much better than a normal text editor, and are specifically designed to make your coding life easier.
Tab/Untab
Lua uses indentation to separate out blocks of code. You learn more about this in the Conditionals and Functions lessons, but the Tab key is going to be your best friend to indent code.
To un-indent code, moving it back to the left, you can use Shift + Tab. Trust me, you’re going to need this!
Search
In VSCode, you can use Ctrl + F to search your code. This is very useful if you think you switched names or spellings.
Scripts work by using a specialized vocabulary that the computer understands, and you can’t just write out commands in full sentences and have a program work. However, you can write your own notes in a script using comments, which is a way to use special characters to communicate that the program should not try to understand what you are writing.
In Lua, we use the --
characters to start a comment, and the computer will ignore things you write after them.
Code language: Lua (lua)
You can also use --
to make the computer temporarily ignore your code, to help figure out which parts are not working.
print("This is code I want to run")
Code language: Lua (lua)
Create Your First Script
Create a Script
- In your Core project, click the Script button in the top toolbar.
- Select Create New Script.
- Name your script “HelloLua”.
- Press Create Script.
Open the Event Log
To get output from your scripts and to see any errors that they may have, you will need to use the Event Log
- In the top menu bar, click View.
- Click Event Log to open the Event Log window.
- Drag the Event Log tab next to one of the other tabs open in Core, so that you can always see it. I usually put it with Properties and Performance.
Open Your Script
- Open the Project Content window.
- Find the Scripts section.
- Double-click your “HelloLua” script. It should open in Visual Studio Code.
Write the Code
In the script, add your first line of code:
print("Hello, lua!")
Code language: Lua (lua)
Press Ctrl + S to save your code.
Place Script in Hierarchy
For the script to execute it needs to be placed in the Hierarchy window.
In Project Content -> My Scripts. Find the script called HelloLua
and drag it into the Hierarchy window.
Run the Script
In Core, press the Play button at the top. You should see the text “Hello, lua” appear in the Event Log.
Congratulations! You’re a coder now.
Reading Errors
Coding without bugs would be like a platformer without enemies. We need something to squash!
Errors are unavoidable, and the most important thing for you to know right now is that when your program has an error, the Event Log will tell you the line number of where it occurred.
You will learn about more common errors throughout this course, and have plenty of opportunities to tackle them!
There is a miss match in behaviour in the editor. When using the “Create New Script” button in the editor the script is added to the Project Content but is not added to the current Hierarchy. When using the “Object -> Create Scripts & Gameplay Assets -> Create New Script” the scripts is added to the Project Content AND the Hierarchy (under the currently selected object, or the root if nothing is selected). This lesson doesn’t make a distinction between the two and if just following the instruction to use the button the script will not run and nothing will show in the Event Log (causing alot of confusion).
Added a section on adding it to the Hierarchy. Thanks.
Don’t forget to drag your new script in your project hierarchy for it to be executed.
You will not be able to see event log by default, you will have to add your event log window to Core Creator in order to see your script working. To do this you will need to select the tab at the top called Window then then select Event log. You can then drag it into your Core Creator and into any position you like.
Have fun coding!!
Thanks