Zoom Scipt Example

Back

The Goal

The goal of this is to make it so when you hold the G key you zoom in, and when you release the G key you zoom out.

This serves as an example of how to make other similar scripts.

Zooming Using the Console

You zoom in and out by changing the environment variable "display.camera_params.zoom".

Because of a quirk of the game, you need to enable cheat codes in order to change environment variables. You enable cheat codes by typing the command "cheat PASSWORD" into the console using the correct password (or putting that line in the "Input/Scripts/program_startup.txt" file).

While in a game, open the console and type the command
 set display.camera_params.zoom 2.0
You are now zoomed in. To return back to normal, open the console and enter the command
 set display.camera_params.zoom 1.0

Binding the G Key to Run the Commands

Open the program and load a game. Then go to

Options -> Controls -> Input Bindings -> Actions -> Custom Actions.

Go to an unused action (it should look blue).

Set the "nickname" of the action to be "zoom" (so you can find it later). Set the primary command to be
 set display.camera_params.zoom 2.0
and set the secondary command to be
 set display.camera_params.zoom 1.0

Now go to
Options -> Controls -> Input Bindings -> Controls

and scroll down until you find the zoom action. Press enter then type the "G" key.

Now when you are in the game, when you press G you zoom in and when you release G you zoom out!

Note the keybindings are package specific. So, if you load a different package, your keybindings will be different.

Binding a Lua Script to the G Key

Here is an alternate way to accomplish the same thing. In the folder Input/Scripts create a file called "script_zoom.lua". Have it read as follows:
function p.start()
    ga_command("set display.camera_params.zoom 2.0")
end

function p.stop()
    ga_command("set display.camera_params.zoom 1.0")
end
The function ga_command causes the system to execute the given console command. Note that you could modify the functions in the script_zoom Lua file to be more complicated. You could even call Lua functions that are defined in whatever package you are using.

Then, go back to the zoom action you made in the previous section and set the primary command to
execl script_zoom.start
and set the secondary command to
execl script_zoom.stop
Now when the G key is pressed, the start function of the script_zoom When the G key is released, the stop function of that script Lua script gets called. gets called.

Note: The command execl stands for "execute Lua". The syntax of the execl command is as follows:

MODULE.FUNCTION STR

The execl function looks for a file called MODULE.lua in either the Input/Scripts folder or the current package folder. It then looks for a function called FUNCTION within that file. It then passes STR as a string argument to the function (and if STR is not given, it passes the empty string).