Skip to content
This repository was archived by the owner on Aug 1, 2025. It is now read-only.

02. Lua Scripting Tutorial

matthew snoddy edited this page Jan 2, 2021 · 3 revisions

This page contains a short tutorial for writing your first Lua script for C&C.

Lua

Lua is a simple scripting language that is very popular for integrating with games. It supports many features, and is easy for even non-programmers to use. You don't need to know Lua to write scripts for C&C, but it will help you create more complex mods.

If you want to learn the basics for yourself, a good guide is maintained here.

Check out all the Lua functions and APIs available with New Construction Options here

In Game Console

If you want to play about with the Lua API or debug a script issue a console is available that can be run in game.

I recommend setting your C&C game to windowed mode so you can easily switch to the console, which appears as a seperate window on your desktop.

See the EnableLuaConsole rule in the RULES.INI Guide

Events

The NCO mod runs scripts when certain events happen in game. Currently the following events are supported:

  • Scenario Start: Called just before the player starts a new level
  • Save Loaded: Called when a player loads a save game
  • On Game Tick: Called as the game iterates over it's main loop (default is every 250 milliseconds) - the tick interval is configurable

See the Roadmap for details of future events.

Create a Simple Script

Script files must be created where the NCO mod is installed. To find this folder use the steps here.

We will create a script that will make the Minigunner speed randomly change in each misson. He will either be a little bit faster or A LOT faster . 😄

  • Click start
  • Type notepad and open the app
  • Paste in the below text:
    local function handler(scenarioName)
      local minigunnerSpeed = math.random(5, 30)
    
      Nco.Infantry.E1.Speed(minigunnerSpeed)
    end
    
    Nco.Events.onScenarioStart(handler)
  • Click File -> Save As...
  • Navigate to the NCO mod folder
  • Click the Save as type: drop down
  • Select All Files (*.*)
  • Set the filename to my-script.lua
  • Click Save

Adding Script to Rules

Now that we have the script file, we need to edit the RULES.INI file to let NCO know that we would like the script to be loaded.

  • Open Explorer
  • Navigate to the NCO mod folder
  • Double click the RULES.INI file
  • Find LuaScripts= and set it to LuaScripts=my-script.lua

Testing the Script

  • Launch C&C Tiberian Dawn in the usual way
  • Ensure you have New Construction Options mod enabled
  • Select any mission with Minigunners in it (GDI 1 for example)
  • Watch the Minigunner fly

Breaking down the Script

  • The below line generates a random number between 5-30 (inclusive)
    local minigunnerSpeed = math.random(5, 30)
  • This line sets the E1 (minigunner) Infantry to the speed we generated above
    Nco.Infantry.E1.Speed(minigunnerSpeed)
  • Finally, this line tells the Nco mod to run the handler function when a mission is about to start
    Nco.Events.onScenarioStart(handler)
    

Clone this wiki locally