-
Notifications
You must be signed in to change notification settings - Fork 2
02. Lua Scripting Tutorial
This page contains a short tutorial for writing your first Lua script for C&C.
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
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
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.
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
notepadand open the app - Paste in the below text:
function handler(scenarioName) local minigunnerSpeed = math.random(5, 30) setInfantryRule("E1", "Speed", minigunnerSpeed) end 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
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.INIfile - Find
LuaScripts=and set it toLuaScripts=my-script.lua
- Launch C&C Tiberian Dawn in the usual way
- Ensure you have
New Construction Optionsmod enabled - Select any mission with Minigunners in it (GDI 1 for example)
- Watch the Minigunner fly
- 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
setInfantryRule("E1", "Speed", minigunnerSpeed)
- Finally, this line tells the mod to run the
handlerfunction when a mission is about to startonScenarioStart(handler)