Community Forum

Start Scripting - How to create a script guide for newbies

Forum Overview >> Scripting

Created11.10.2021 09:58

@ Squity (Squity) 11.10.2021 09:58
This guide refers to FS19 and things may change.
I'm also new to FS scripting so please be patient and correct me if i'm doing something wrong.

#1 - Prepare the game for development

## Enable development mode that allow you to see the game logs/console in game, the console can be opened by pressing the keyboard key positioned above Tab key, pressing the key twice will allow you to input commands. Go in <My games/Farming Simulartor19>/game.xml and set controls to true:

#2 - Create your mod folder myModName in mods folder and the modDesc.xml file, for this file you can copy most of the stuff from other mods.

#3 - Create a myScript.lua file and put it in myModName folder, below are some examples on how to include it in modDesc.xml file in order to be loaded by the game.

## Basic way to include the script in modDesc.xml
<sourceFile filename="myScript.lua" />
-- You can include multiple scripts files and then use the myScript Class created later in this guide in your file to reach for the main script.

## Another way is by using the AddSpecialization.lua script (you need to get the script file from other mods, search for it online).
<sourceFile filename="AddSpecialization.lua" />
<specialization name="myScript" className="myScript" filename="myScript.lua" debug="false">
<restrictions state="0" name="drivable" />

-- The AddSpecialization.lua script will allow you to register for events fired when things happens on vehicles, for instance you can do this in your script:
myScript.specialization = {};
function myScript.prerequisitesPresent(specializations)
return SpecializationUtil.hasSpecialization(Drivable, specializations);
-- Register specialization events
function myScript.registerEventListeners(vehicleType)
SpecializationUtil.registerEventListener(vehicleType, "onEnterVehicle", myScript.specialization);
-- onLeaveVehicle is also available.
function myScript.specialization:onEnterVehicle()

#4 - Capture game events in your mod myScript.lua, the myScript Class has to be created and attached to the mod event listener game function:

myScript = {};
local myScript_mt = Class(myScript);

function myScript:draw()
-- Render your text or images on screen (called at every frame draw?)

function myScript:update(dt)
-- The update function is a function that is called after every frame. The parameter dt is the time that has passed since the last update call in milliseconds.

function myScript:loadMap()
-- Savegame map is loaded

function myScript:mouseEvent(posX, posY, isDown, isUp, button)
-- When the mouse is moved or mouse buttons are pressed.

-- For keyboard inputs see how registerActionEvents() is used on other mods, you need to define the input key and action in modDesc.xml and then attach the key listener in myScript.lua

-- etc...

#5 - Important game global variables
g_inputBinding -- Contains all input related stuff, properties, events, attach listeners.
g_currentMission -- Contains player related stuff, properties, events, attach listeners, enter/exit vehicles, farm, current vehicle (g_currentMission.controlledVehicle)
g_i18n -- Localization stuff and strings
-- Save g_currentModName and g_currentModDirectory to your local scope or to myScript Class since they may be available only in the global scope when the mod is loaded.
myScript.modName = g_currentModName;
myScript.modDir = g_currentModDirectory;

#6 - Useful stuff

## Fast way to edit your myScript.lua and reload the game, create a console command that restarts your savegame.
function myScript:restartMySavegame()
if g_server then
restartApplication(true, " -autoStartSavegameId 6"); -- FS22 restart application
-- OLD FS19 command: restartApplication(" -autoStartSavegameId 6"); -- 6 is the savegame number used by me to test my mods, you can change the number to your savegame nr.
function myScript:loadMap(name) -- Note: You don't need to duplicate the loadMap listener.
addConsoleCommand('restartMySavegame', 'Restart my savegame', 'restartMySavegame', self); -- Add a console command when the mod is loaded, you can call this command from in game console and quickly restart the savegame.

## LUA Print stuff in console/log.txt file
print("This is my text")
print("This is my variable: " ..tostring(yourVariable)) -- with the two dots ".." you can concatenate stuff
print_r(myScript) -- Print all table contents.

-- the self is defined only if you call the function like this: myScript:myFn() calling the function with the dot works but will not define the self variable myScript.myFn()
function myScript:myFn()
-- self is myScript Class.

Hungrycowdesign (HungryCow212) 10.11.2021 14:31
Thanks for this! I have a lot of experience with low-level languages such as C, but I'm just getting started with object oriented languages and scripts. This is extremely helpful. Thanks again!

Kal Sarwar (KhalidIbn) 13.11.2021 14:23
Thanks for this, I don't have FS19 but was looking at what the SDK would let you do. Hopefully most of this will be applicable to FS22.

Marciel Grutzmann (Marciel032) 19.11.2021 19:49
Thank you so much, I realy need this.

Olivier Beauchemin (obeauchemin) 23.11.2021 19:14
Very nice. It could be helpful too to show how debugging works with Giant Remote Debugger or how to set up a minimal development environment.

Holger Tangemann (Holger1607) 22.12.2021 14:38
Your code for automatically reload the savegame is not working with FS22 - the reason is a missing parameter.

Don't write: restartApplication(" -autoStartSavegameId 6") --6 is only the Savgame placeholder

better write: restartApplication( true, " -autoStartSavegameId 6" )

Josh Benoit (CaffeinatedCM) 10.01.2022 00:31
The restartApplication isn't auto loading my savegame, I'm using the " restartApplication( true, " -autoStartSavegameId 6" )" and my save id is 6, any ideas? (I'm in FS22)

Antonis L (antonis78) 11.01.2022 00:13
@ Squity Hello. Thank you very much for this guide. Is it possible to send you PM for a little help if you can?
Best regards

@ Squity (Squity) 23.01.2022 07:55
Hi, happy to help you all.

Thank you @Holger, i've edited the code in my example.
I well keep an eye on this thread and try to update the script to FS22.

@Antonis, you can get better help by posting in this forum.

Gordo Ismyname (gordoismyname) 06.02.2022 05:42
i wish there were more posts like this one on the forum. it is really helpful!

Gordo Ismyname (gordoismyname) 06.02.2022 05:43
i wish there were more posts like this one on the forum. it is really helpful!

Tobias Schumacher (NeoPrince) 22.02.2022 22:36
@ Squity you sir are awesome.
You saved me a ton of work with this
restartApplication( true, " -autoStartSavegameId 6" )

@ Squity (Squity) 26.02.2022 09:06
Happy to help @Tobias

You can work even faster if you create a shortcut on windows and append the restart string with your savegame nr.
After creating a duplicate of fs22 launching shortcut you can edit the shortcut properties (right click on shortcut -> Properties) and modify the Target:
"C:\Program Files (x86)\Farming Simulator 22\x64\FarmingSimulator2022Game.exe" -autoStartSavegameId 3

Then you can launch the game directly to your savegame, and in window mode you can close the game and start again faster with the shortcut.

Note: Log in to post. Create a new account here.