In this tutorial, we will be learning the basics of creating triggers in Lua and refactoring our map so that the units are created by script rather than being directly placed in the Terrain Editor.

This tutorial will cover:

1. Getting Started With Lua


Our game engine uses Lua to build game logic that shape how games are actually played. At some point we will add a trigger GUI for writing game logic without writing script, but for now we use Lua. It may sound like scary coding, but in reality Lua is a widely-used lightweight scripting system focused on speed and ease-of-use. Lua is widely documented and googling “How do I do X in Lua” usually returns useful results.

Follow our Setting up VS Code Environment guide, including installing our recommended extensions and reading our Lua Style Guide

Great! Now we’re ready to get started adding some triggers to our map! You can close each of the open tabs in VS Code by click the “x” in the tab or with the Ctrl+W shortcut.

2. Adding Triggers To Your Map


Triggers live inside the “Trigger.lua.txt” file inside the map folder. You can open this file directly by pressing this “Open Lua Script” Button at the top of the Terrain or Data Window.

Untitled

Alternatively, you can open this file by selecting “File > Open” in VS Code or by dragging the .lua.txt file directly into your open VS Code window. By default, the Trigger.lua.txt file will look like this:

Untitled

For this tutorial, we’ll remove this code (removing it won’t cause any problems).

Write the following (actually write it out, it’ll help you learn better than copy-pasting):

DCEI.LogMessage("Hello Lua!")

Save the trigger file when you finished and press Play in the editor to test your work.

At first it may seem like nothing is different. But if you open “Window > Play Settings Window”, look for the Player Logs on the right side, and scroll to the bottom, you’ll see that “Hello Lua!” has been printed. So what exactly happened there? We used the DCEI.LogMessage function, which takes a string and prints it to the log window.

debug_log.png