Community Forum

Utils.prependedFunction

Forum Overview >> Scripting

CategoryScripting
Created27.03.2021 09:21


Boris Klinko (Boris555) 27.03.2021 09:21
What does Utils.prependedFunction do? There isn't description in documentation.

Bilbo Beutlin (BBeutlin) 27.03.2021 11:02
It doesn't need a documentation.
The 'prepended' does just mean what the name says: a function is linked with another function and executed before the source function.

The counterpart is "Utils.appendedFunction()" where it is executed after the source function.

Boris Klinko (Boris555) 27.03.2021 18:49
Thanks Bilbo

I have used "Utils.appendedFunction()" and my script executes. But now I need to target a file in mods directory from script. Direct path from hard disk root is working and relative path to game directory is working, what I need is relative path to mod folder in mods directory.

Bilbo Beutlin (BBeutlin) 28.03.2021 00:39
Try Utils.getFilename("filename", "$moddir")

Boris Klinko (Boris555) 28.03.2021 11:14
This is working
wrapperBaleFilename="H:/Games/FS19 mods/wrapper/script_Bale_wapper_extension/bales/roundbaleCloverSilage_w112_d130.i3d"

but when I try
wrapperBaleFilename=Utils.getFilename("roundbaleCloverSilage_w112_d130.i3d", g_currentModDirectory)

it doesn't work. Did I use the function properly? Also when I try print( g_currentModDirectory ) I get nothing. My script is in folder script_Bale_wapper_extension.

Bilbo Beutlin (BBeutlin) 28.03.2021 15:34
Oh no. "Utils.getFilename()" works in LUA scripts only.
Sorry, I forgot the closing '$', must be "$moddir$".

In XML you could try the filename prefix "$moddir$", but I'm afraid this works in defaultItems/Vehicles only.
Example: filename="$moddir$path/file"

The global 'g_currentModDirectory' is valid only in the initializing basic code, not in functions.
Also notice this points to the individual mod folder, not to the 'all mods' folder.
That means you must read and store it just at begining like
------
myMod = {}
myMod.homeDir = g_currentModDirectory
------
To go one step above in hierarchy (to parent folder), use the common DOS syntax "..", eg.
myMod.allModsDir = myMod.homeDir .. "/.."

There's also a global variable in the style "g_allModsDir" and IIRC a function getAllModsDir(), but I don't remember the name anymore. *g*


Boris Klinko (Boris555) 03.04.2021 11:01
Thank you Bilbo,

Wrapper.homeDir = g_currentModDirectory did the work for me, but after a while I realized that I should better use files allready included on map and not to use specific files included in my mod folder. The files that I need are i3d's for bales. I have noticed that all of these are listed in maps_baleTypes.xml and are used in Baler specialization as so

function Baler:createBale(baleFillType, fillLevel)
local spec = self.spec_baler
local t = spec.baleTypes[spec.currentBaleTypeId]
local baleType = g_baleTypeManager:getBale(baleFillType, t.isRoundBale, t.width, t.height, t.length, t.diameter)
.....

So i have tried to use g_baleTypeManager:getBale (global function I believe) in my script but I got
this error:

Error: H:/Games/FS19 mods/wrapper/script_BaleWapperExtension/wrapper.lua:13: attempt to index field 'baleType' (a nil value)

code:
.....
Wrapper = {};
Wrapper.baleType = g_baleTypeManager:getBale('CLOVER_FERMENTED', true, 1.12, nil, nil, 1.3)
Wrapper.baleFilename = Wrapper.baleType.filename --line 13

function Wrapper.prerequisitesPresent(specializations)
return true
end;

function Wrapper:onLoad(savegame)
.....


My script is called with
BaleWrapper.onLoad = Utils.appendedFunction(BaleWrapper.onLoad, Wrapper.onLoad);

Have used g_baleTypeManager:getBale properly and can this function run in my script or it runs only in Baler specialization?


Bilbo Beutlin (BBeutlin) 03.04.2021 11:28
To go sure check your 'Wrapper.baleType' for 'nil' before you proceed.

For getBale() you need as first argument a fillTypeIndex (value), but you use a string.
Right is getBale(CLOVER_FERMENTED, ..) without quotes.

Typical rookie mistake! *g* .. a[i] is not the same as a['i']

Case CLOVER_FERMENTED is unknown (not global) you need to use getFillTypeIndexByName() first.

But it may also happen, that at the time of 'onLoad()' the custom fillType table is not setup yet, so you need to execute your function later, then try to append after 'onPostLoad()'.


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