How to create a lua script (for SaintExec)

Hello everyone,

If you want to create a script for Saint Row IV you've come to the right place!​

Let's begin.

Prerequisite:
- Knowledge about how to program in lua or another interpreted language like python (I will try my best to explain technicals parts).
- Download the latest release of SaintExec : https://www.saintsrowmods.com/forum/threads/saint-exec-lua-executor-for-saint-row-iv-re-elected.20348/

Part 1: Understand How It work


First, Saint Row is programmed in C/C++, so is compiled (code to 0 and 1 that you computer understand), and it's theses 0 and 1 are the game itself. That means we can't retrieve the original code and if yes it's pretty difficult to mod it. However, Lua come to place in fact Voltion embedded lua in C++ (1). That can allow them to maintain the code and add new functionalities easily. Lua is an interpreted language so code is convert in 0 and 1 and executed in runtime while you play. This means two things : we can retrieve this code, and we can write and execute lua for modding Saint row ! With the help of ElCapor and the release pdb (2) I was able to do exactly that.

Saint row has two instances of lua one for the interface who is created is the main menu and one game play who is created each time you go in-game, there are called Lua States. There is registered C functions which are expose to lua (the 2 files in the attachment repertory them). The main problem is that all these functions are targeted to do specifics tasks (but we can still do cool stuff with them). All the documentation you will find come from Saint row the Third (3) so it can have outdated informations and there is also a lot of functions undocumented. To develop, I have implemented 2 more functions printc and sleepc (their documentation is also in the files).

To conclude, in our program, we can use all functions registered and lua scripts which are already running in the specific Lua State.

Part 2: Dump the files


Launch SaintExec check "Dump Lua Scripts" and open the game. If you want a safe dump go in-game and do not open any menu and close it, with this method you know that every script will be load at any time in the game. The Dump is at %SaintRowDirectory%/luadump/ There is a folder for each lua state.
You can also make another dump and enter in all menus and stores... So you can have an overview of all functions and is very useful if you want to use undocumented functions.

Part 3: Setup the IDE

In the folder of the dump, put game_functions.lua in the folder gameplay and interface_functions.lua in interface.
For this tutorial i will use Visual Studio code, but you can use any code editor you want.
First install the lua extension by sumneko,
Next open the folder of your choice gameplay or interface. You can now code with autocompletion. Create a script in this folder :
Capture2.PNG

A useful thing is, when you don't know a function, right click on it and "Find all references".
When you code don't forget printc (which prints the text to the console) and sleepc (which wait in ms).
When you finish your script, copy paste it into SaintExec select the correct Lua State and execute it !

There are some examples in sample_scripts.lua with additional explanations.


Happy coding !

References:
1
. https://www.lua.org/source/5.1/
2. https://minimaul.saintsrowmods.com/files/temp/inauguration-station-debug-exes.zip
3. https://www.saintsrowmods.com/Kinzies-Toy-Box/script_actions/_script_actions_index.html
 

Attachments

  • gameplay_functions.lua
    312 KB · Views: 99
  • interface_functions.lua
    29.1 KB · Views: 92
  • sample_scripts.lua
    1.9 KB · Views: 106
Last edited:
Back
Top