Community Forum

Tree planter

Forum Overview >> Scripting

Created01.10.2019 23:16

Marctheboy Geekabits (Marctheboy) 01.10.2019 23:16
Im currently trying to code a tree planter to plant trees at 3 different location. im starting with the coding since it my weakness.

i have a fresh Damcon PL75 mod folder with all the correct mod desc and .XML purchasable and working in fs19.

now i have added a Lua to a Script folder.
i have 2 Lua script

in the modDesc.xml i have added

<sourceFile filename="Script/register.lua"/>

<type name="treePlanter" parent="baseGroundTool" className="Vehicle" filename="$dataS/scripts/vehicles/Vehicle.lua">
<specialization name="turnOnVehicle" />
<specialization name="fillUnit" />
<specialization name="treePlanter2" />
<specialization name="GroundReference" />


in the Register script i have

TreePlanter2_register = {}

if g_specializationManager:getSpecializationByName("treePlanter2") == nil then
g_specializationManager.addSpecialization('treePlanter2', 'treePlanter2', 'TreePlanter2', Utils.getFilename("Script/treePlanter2.lua", g_currentModDirectory))
print("----TreePlanter2 registered.")
print("----TreePlanter2 already exists.")



now in the treePlanter2.Lua i have all the function described at this link

now the main question....
when i boot my game in the log file after i have those errors that i just dont understands?

2019-10-01 15:04 ----TreePlanter2 registered.
2019-10-01 15:04 Error: Vehicle type 'DAMCON.treePlanter' has unknown specialization 'DAMCON.GroundReference'!
2019-10-01 15:04 Error: Running LUA method 'update'.
2019-10-01 15:04 dataS/scripts/vehicles/SpecializationUtil.lua(67) : Error: Given reference for OverwrittenFunction 'addFillUnitFillLevel' is 'nil'!

and that

2019-10-01 15:04 Warning (C:/User/Documents/My Games/FarmingSimulator2019/mods/DAMCON/damconPL75.xml): TreePlanter requires a palletTrigger!

this one i dont understands because it the TreePlanter script and not my TreePlanter2 script that is requiered a palletTriggered.

how can i disactivate the treePlanter to leave my TreePlanter2 active or how can i do this, what im missing?


Bilbo Beutlin (BBeutlin) 02.10.2019 08:55
1. The register code doesn't need 'addModEventListener()' - it is executed only once at startup

2. The documented spec code is incomplete, only meant for reference.
Sometimes are missing certain functions, always missing the initializing code part.

3. I'd guess, you have adopted the documented code 1:1 - that's totally wrong. All references to original 'TreePlanter' must be replaced by 'myMod', be it as function (class) name or within the function code itself.

Gtx | Andy (GtX_Andy) 02.10.2019 11:51
Just to add to what BB has said.

There is no need to have a register script for a vehicle specialization unless it is being injected into other vehicles in a global way.
When you write a specialization script for a mod vehicle it should be registered correctly using the modDesc.

<specialization name="myScript" className="MyScript" filename="scripts/MyModScript.lua"/>

You can then use this spec in you new 'vehicleType'

Marctheboy Geekabits (Marctheboy) 02.10.2019 22:24
so my new mod desc will look like so.

<sourceFile filename="Script/treePlanter2.lua"/>

<specialization name="treePlanter2" className="Treeplanter2" filename="scripts/treePlanter2.lua"/>

<type name="treePlanter" parent="baseGroundTool" className="Vehicle" filename="$dataS/scripts/vehicles/Vehicle.lua">
<specialization name="treePlanter2" />

now how does the engine compile the mods?

like how do i override the Create tree function, like so in my Lua?

function TreePlanter2.registerOverwrittenFunctions(vehicleType)
SpecializationUtil.registerOverwrittenFunction(vehicleType, "createTree", TreePlanter2.createTree)

now with this new function, will it take over the base game script createTree to my new define Function TreePlanter2.createTree?

function TreePlanter2:createTree()
local spec = self.spec_treePlanter2
if not g_treePlantManager:canPlantTree() then
spec.showTooManyTreesWarning = true
if self.isServer and spec.mountedSaplingPallet ~= nil then
local pallet = spec.mountedSaplingPallet
local x,y,z = getWorldTranslation(spec.node)
local yRot = math.random() * 2*math.pi
local fillType = pallet:getFillUnitFillType(1)
local treeTypeIndex = 1
if fillType == FillType.TREESAPLINGS then
local treeTypeName = getUserAttribute(pallet.rootNode, "treeType")
if treeTypeName ~= nil then
local desc = g_treePlantManager:getTreeTypeDescFromName(treeTypeName)
if desc ~= nil then
treeTypeIndex = desc.index
g_treePlantManager:plantTree(treeTypeIndex, x, y, z, 0, yRot, 0, 0)
spec.lastTreePos = {x,y,z}
local stats = g_farmManager:getFarmById(self:getActiveFarm()).stats
if g_currentMission.missionInfo.helperBuySeeds and self:getIsAIActive() then
local storeItem = g_storeManager:getItemByXMLFilename(pallet.configFileName)
local pricePerSapling = 1.5 * (storeItem.price / pallet:getFillUnitCapacity(1))
stats:updateStats("expenses", pricePerSapling)
g_currentMission:addMoney(-pricePerSapling, self:getActiveFarm(), MoneyType.PURCHASE_SEEDS)
-- use 0.9999 instead of 1 to compansate float precision on mp sync
local fillLevelChange = -0.9999
if self:getFillUnitFillLevel(spec.fillUnitIndex) < 1.5 then
fillLevelChange = -math.huge
self:addFillUnitFillLevel(self:getOwnerFarmId(), spec.fillUnitIndex, fillLevelChange, self:getFillUnitFillType(spec.fillUnitIndex), ToolType.UNDEFINED)
-- increase tree plant counter for achievements
stats:updateStats("plantedTreeCount", 1)

am i in a good way or should i know something more?
just need to know how to visualize the compiling


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