@ScriptModders - Suggested modifications for `register.lua` failures due to game-patch 1.4.0.0

Your forum for all discussions around Modding.
User avatar
Decker_MMIV
Posts: 287
Joined: Wed Aug 22, 2012 1:02 am

@ScriptModders - Suggested modifications for `register.lua` failures due to game-patch 1.4.0.0

Post by Decker_MMIV »

As many of us PC/Mac players who use mods in FS22 experienced, the patch 1.4.0.0 caused quite a change, that broke a random set of script-mods.

I took a look at this, because some script-mods worked fine, where others did not - so I imagined that it could be due to the very different methods, that the script-modding community have created throughout the years, for registering/injecting their custom specializations into existing vehicle-types in Farming Simulator.

What I discovered, was that the script-mods that fails loading in patch 1.4.0.0, mostly uses old register.lua script code, including expecting that the self.spec_... member-variable is automatically created.

After a bit of trial-and-error, I managed to privately modify some of other mod-authors' script-mods that I use in my game-sessions, and got them working again. - Though there may still be other problems I have yet to discover.

DO NOT ATTEMPT THE FOLLOWING, IF YOU HAVE NO EXPERIENCE WITH MODIFYING LUA SCRIPTS!
Instead just be patient and wait for the mod-authors to fix their script-mods.

As there may be other script-modders out there, who are currently struggling with updating their mods to work again for patch 1.4.0.0, I have below explained the steps I did, for what to change and modify, to get script-mods back into a hopefully working state again - unless GIANTS Software quickly provides a hot-fix that restores the old method, of letting script-mods inject their specializations into existing vehicle-types.

Could be that some script-modders may find this information and knowledge useful.

For the below examples, I have used loki_79's Realistic Cab View mod - though it could be any other script-mod that uses an older method of registering/injecting its specializations.

1.
Remove the registration of your specialization from your Lua script, and instead put it into the modDesc.XML file

Code: Select all

// OLD - in Lua script (maybe called `register.lua`)

g_specializationManager:addSpecialization('cabView', 'CabView', Utils.getFilename('CabView.lua', g_currentModDirectory), true);
^^ Comment out or remove such statements from your Lua script, and register your specialization-script(s) within the modDesc.XML file, in the <specializations> block, as individual <specialization ... /> elements.

Code: Select all

// NEW - in `modDesc.xml`

    <specializations>
        <specialization name="cabView" className="CabView" filename="CabView.lua" />
    </specializations>

2.
Change the Lua code that "register"/inject your specialization into existing vehicle-types.
There might be a "better, proper and cleaner method" for doing this (GIANTS developers, please share if possible), but for now this seems to be how it is known and done by the modding community

Code: Select all

// OLD - in Lua script (maybe called `register.lua`)

for vehicleName, vehicleType in pairs(g_vehicleTypeManager.types) do
    if  SpecializationUtil.hasSpecialization(Drivable,  vehicleType.specializations) and
        SpecializationUtil.hasSpecialization(Motorized, vehicleType.specializations) and
        SpecializationUtil.hasSpecialization(Enterable, vehicleType.specializations) and
        SpecializationUtil.hasSpecialization(Dashboard, vehicleType.specializations)
    then
        g_vehicleTypeManager:addSpecialization(vehicleName, "cabView")
        print("INSTALLED: "..vehicleName)
    end
end
^^ Modify the old script code, by:
- Adding a script-scoped variable called modName, that is assigned the value of g_currentModName.
- Moving the for-loop into a function (if not already), which I here have named injectSpecialization.
- Create local variable modNameSpecName and assign it a string-value concatinated to contain "<modName>.<specialization-name>".
- Change the #2 argument in call to g_vehicleTypeManager:addSpecialization to use the modNameSpecName variable.
- Adding new statements for appending an anonymous-function to TypeManager.validateTypes - which will check that it is type-manager for "vehicle" before calling our injectSpecialization function.

Code: Select all

// NEW - in Lua script (maybe called `register.lua`)

local modName = g_currentModName;

function injectSpecialization()
    local modNameSpecName = modName .. "." .. "cabView"

    for vehicleName, vehicleType in pairs(g_vehicleTypeManager.types) do
        if  SpecializationUtil.hasSpecialization(Drivable,  vehicleType.specializations) and
            SpecializationUtil.hasSpecialization(Motorized, vehicleType.specializations) and
            SpecializationUtil.hasSpecialization(Enterable, vehicleType.specializations) and
            SpecializationUtil.hasSpecialization(Dashboard, vehicleType.specializations)
        then
            g_vehicleTypeManager:addSpecialization(vehicleName, modNameSpecName)
            print("INSTALLED: "..vehicleName)
        end
    end
end

TypeManager.validateTypes = Utils.appendedFunction(TypeManager.validateTypes, function(self)
    if self.typeName == "vehicle" then
        injectSpecialization()
    end
end)


3.
Recreate the possibly missing self.spec_... member-variable.
For an example of how GIANTS Software does this, take a look at FS19's Precision Farming DLC mod, the SoilSampler.lua script.

Code: Select all

// OLD - in your Lua specialization script

function CabView:onLoad(savegame)
    spec = self.spec_cabView

    ...

^^ Due to the updated method of registering and injecting the specialization script, now the self.spec_... member-variable cannot be easily accessed, as it have a dot in it.
- First add a script-scoped variable called modName, that is assigned the value of g_currentModName.
- Then in the "first function of this specialization that gets called when a vehicle is spawned", add a new statement, that creates the wanted self.spec_... member-variable and assign it the value of the not-easily-accessible self.spec_<modName>.<specialization-name> member-variable.

Code: Select all

// NEW - in your Lua specialization script

local modName = g_currentModName

function CabView:onLoad(savegame)
    self.spec_cabView = self["spec_" .. modName .. "." .. "cabView"]

    local spec = self.spec_cabView

    ...

ebelbeb
Posts: 5
Joined: Tue Sep 15, 2020 11:11 pm

Re: @ScriptModders - Suggested modifications for `register.lua` failures due to game-patch 1.4.0.0

Post by ebelbeb »

Thank you very much. Thanks to your suggestions, I have fixed about 10 mods :)
Some ModHub mods have been fixed by their authors. I see they are doing this without adding <specializations> to modDesc.
goodN8JohnBoy
Posts: 1454
Joined: Wed Dec 01, 2021 6:51 pm

Re: @ScriptModders - Suggested modifications for `register.lua` failures due to game-patch 1.4.0.0

Post by goodN8JohnBoy »

The defined XML and LUA schemes are now validated very strictly.
The most common problem were wrong parameters in 'g_specializationManager:addSpecialization()'.
Many people used addSpecialization(name, className, filename, 'true|false') for whatever reason.
However right is addSpecialization(name, className, filename, customEnvironment) where the last may be 'nil' or a table.
So many mods worked again merely by changing true|false to nil.
ice_boii_1207
Posts: 346
Joined: Wed Nov 09, 2022 7:28 am
Location: Somewhere on earth

Re: @ScriptModders - Suggested modifications for `register.lua` failures due to game-patch 1.4.0.0

Post by ice_boii_1207 »

Thanks for the information bro. Only if i new how to code 😒
Ice_boii :gamer:
dawauz
Posts: 20
Joined: Wed Feb 03, 2021 9:19 am

Re: @ScriptModders - Suggested modifications for `register.lua` failures due to game-patch 1.4.0.0

Post by dawauz »

I'm also not a scripter, but I like a mod who is broken with this errors.

Maybe can someone fix this?

https://www.kingmods.net/en/fs22/mods/compost-addon
Post Reply