Lua:Essential Setup
Step one – we’ve gotta grab some tools!
I’ll be covering a handful of setups that I find vital for programming in Lua. First off, I’ll talk about a pretty well-known product – Notepad++. I personally no longer use it, and at this point prefer Visual Studio Code for all my Lua needs, but I’ll go over both for taste. They’ll both be able to do what you need.
This tutorial assumes you already have RPFM downloaded. If you don’t yet, go grab it.
Notepad++ Setup
One of the most well-known text editing products out there is Notepad++. As the name implies, it’s kinda like regular Notepad, but actually good and can do stuff. It has features like global searching, opening a folder and viewing the contents as a tree-view, and is all around a pretty solid product.
I’ve never used Notepad++ for actually writing and editing .lua files – it was for a while primarily what I used to search through and read CA .lua files. I now use VSCode for both of those efforts – editing and searching through files – but many people prefer to use Notepad++ so here I am discussing it.
If interested, grab Notepad++. Download it, install it wherever.
While you wait for Notepad++ to finish installing, we’re gonna make a script dump – a location on your PC where you put all of CA’s scripts, so you can quickly reference them. You should open RPFM, and use the "Open All CA Packs"
command (after making sure you have the proper game selected, under, well, Game Selected).
Now, go find a spot to create a script dump folder. You’ll need to grab all CA’s vanilla scripts and put them somewhere on your PC – I recommend an HDD, if you have two drives! – in order to easily browse through their files. Once you have one created and pinned and you know where it is (mine is S:/Total War Dumps/Script Dumps), go back to RPFM, and go find the main “script” directory. Right-click it and select “Extract”, and target the new folder you just made.
And, boom! You now have a script dump. I personally organize it as follows:
S:/Total War Dumps/Script Dumps/WH2/%today's date or patch name%/script
. I’ll use /3K/ or else for whatever game I’m using. This helps me quickly keep track of older patches, when I need to compare files between versions.
Lastly, open up Notepad++, and use "File -> Open Folder As Workspace..."
. Target your renamed “script” folder within the script dump directory, and you’ll notice that the directory appears on the left-hand side. If you right-click the main folder over there, and press "Find in Files..."
, you’ll be able to global-search all CA scripts. Incredibly handy for looking at their usage of commands, or to see any references to, say, an agent subtype key.
And that’s Notepad++! You can make scripts in there as well, using “File -> New” and naming it “whatever_you_want.lua” when saving. Though, I personally prefer using Visual Studio Code for programming in Lua (and it’s what I’m typing this tutorial in), and that’s where I have my IDE setup. If you’re interested in VSCode, continue reading, if not carry on to the Hello World tutorial!
Visual Studio Code Setup
Clearly, one needs Visual Studio Code. Go get Visual Studio Code.
You got it? No? Why are you reading this?
Okay, now you’re ready? Good.
Visual Studio Code (VSC or VSCode, also) is a super nice program in my opinion, and a nice big brother to Notepad++. It has a lot of extra functionality that NP++ does not, and allows a lot of flexibility in that regard. VSC has greater tab support and multi-window support, it’s easier to use Workspaces in VSC (again in my opinion) than in NP++, and the extensions and further features and settings add a lot that helps me with my programming.
You can save “code workspace” files, which allow you to easily access several folders or files that you have saved for one project, as well as have different settings for different workspaces. I can jump really easily between, say, my Return of the Lichemaster code workspace, my CA Script Dump workspace, and my Other Currently Secret Things code workspace, with only a couple clicks. It has a really pretty dark mode, and the syntax highlighting for Lua is gorgeous, in my opinion.
You can save “code workspace” files, which allow you to easily access several folders or files that you have saved for one project, as well as have different settings for different workspaces. I can jump really easily between, say, my Return of the Lichemaster code workspace, my CA Script Dump workspace, and my Other Currently Secret Things code workspace, with only a couple clicks. It has a really pretty dark mode, and the syntax highlighting for Lua is gorgeous, in my opinion.
To use VSCode to its fullest, I suggest solid integration within your RPFM MyMod folder. In my MyMod folder, I have a folder for each of the mods, and then the .pack that RPFM tracks and saves, and also a .code_workspace for each important workspace. I can use File -> Open Workspace...
for each of those files, or simply use File -> Open Folder...
to directly open the MyMod folder. I use the Workspaces so I can hide files in each of the folders, depending on the mod – I don’t need to see the /db/ folder in VSC, for instance.
Within VSCode now, you can create new folders, open extras, and all, and it will be within the same code workspace!
And that’s it, VSCode is really simple to use (for our application atm). I recommend at this point downloading the vscode-lua
extension, which enables some error logging within VSC (for when you forget some syntax or add an extra comma) and enables coloring and other fun stuff. Link to extension here.