LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

Pickup

Description
Specialization for vehicles with independently lowerable PickUps (e.g. forage wagon, baler, ...)
Functions

actionControllerLowerPickupEvent

Description
Definition
actionControllerLowerPickupEvent()
Code
148function Pickup:actionControllerLowerPickupEvent(direction)
149 self:setPickupState(direction > 0)
150end

actionEventTogglePickup

Description
Definition
actionEventTogglePickup()
Code
286function Pickup.actionEventTogglePickup(self, actionName, inputValue, callbackState, isAnalog)
287 local spec = self.spec_pickup
288 if self:getCanChangePickupState(spec, not spec.isLowered) then
289 self:setPickupState(not spec.isLowered)
290 end
291end

allowPickingUp

Description
Definition
allowPickingUp()
Code
180function Pickup:allowPickingUp()
181 local spec = self.spec_pickup
182 return spec.isLowered
183end

getCanChangePickupState

Description
Definition
getCanChangePickupState()
Code
213function Pickup:getCanChangePickupState(spec, newState)
214 return true
215end

getDirtMultiplier

Description
Definition
getDirtMultiplier()
Code
219function Pickup:getDirtMultiplier(superFunc)
220 local spec = self.spec_pickup
221
222 if spec.isLowered and (self.getIsTurnedOn == nil or self:getIsTurnedOn()) then
223 return superFunc(self) + self:getWorkDirtMultiplier() * self:getLastSpeed() / self.speedLimit
224 end
225
226 return superFunc(self)
227end

getIsLowered

Description
Definition
getIsLowered()
Code
187function Pickup:getIsLowered(superFunc, default)
188 local spec = self.spec_pickup
189 return spec.isLowered
190end

getWearMultiplier

Description
Definition
getWearMultiplier()
Code
231function Pickup:getWearMultiplier(superFunc)
232 local spec = self.spec_pickup
233
234 if spec.isLowered and (self.getIsTurnedOn == nil or self:getIsTurnedOn()) then
235 return superFunc(self) + self:getWorkWearMultiplier() * self:getLastSpeed() / self.speedLimit
236 end
237
238 return superFunc(self)
239end

initSpecialization

Description
Called on specialization initializing
Definition
initSpecialization()
Code
25function Pickup.initSpecialization()
26 local schema = Vehicle.xmlSchema
27 schema:setXMLSpecializationType("Pickup")
28
29 schema:register(XMLValueType.STRING, Pickup.PICKUP_XML_KEY .. ".animation#name", "Pickup animation name")
30 schema:register(XMLValueType.FLOAT, Pickup.PICKUP_XML_KEY .. ".animation#lowerSpeed", "Pickup animation lower speed")
31 schema:register(XMLValueType.FLOAT, Pickup.PICKUP_XML_KEY .. ".animation#liftSpeed", "Pickup animation lift speed")
32 schema:register(XMLValueType.BOOL, Pickup.PICKUP_XML_KEY .. ".animation#isDefaultLowered", "Pickup animation is default lowered")
33
34 schema:setXMLSpecializationType()
35end

loadPickupFromXML

Description
Definition
loadPickupFromXML()
Code
194function Pickup:loadPickupFromXML(xmlFile, key, spec)
195 XMLUtil.checkDeprecatedXMLElements(xmlFile, "vehicle.pickupAnimation", key .. ".animation") --FS17 to FS19
196
197 spec.isLowered = false
198 spec.animationName = xmlFile:getValue(key .. ".animation#name", "")
199 if not self:getAnimationExists(spec.animationName) then
200 spec.animationName = ""
201 spec.isLowered = true
202 end
203
204 spec.animationLowerSpeed = xmlFile:getValue(key .. ".animation#lowerSpeed", 1)
205 spec.animationLiftSpeed = xmlFile:getValue(key .. ".animation#liftSpeed", -spec.animationLowerSpeed)
206 spec.animationIsDefaultLowered = xmlFile:getValue(key .. ".animation#isDefaultLowered", false)
207
208 return true
209end

onFoldTimeChanged

Description
Called if fold anim time changes
Definition
onFoldTimeChanged(float foldAnimTime)
Arguments
floatfoldAnimTimefold animation time
Code
142function Pickup:onFoldTimeChanged(foldAnimTime)
143 Pickup.updateActionEvents(self)
144end

onLoad

Description
Definition
onLoad()
Code
70function Pickup:onLoad(savegame)
71 self:loadPickupFromXML(self.xmlFile, "vehicle.pickup", self.spec_pickup)
72end

onPostLoad

Description
Definition
onPostLoad()
Code
76function Pickup:onPostLoad(savegame)
77 local spec = self.spec_pickup
78
79 if spec.animationName ~= "" then
80 local dir = -20
81 if spec.animationIsDefaultLowered then
82 dir = 20
83 spec.isLowered = true
84 end
85 self:playAnimation(spec.animationName, dir, nil, true)
86 AnimatedVehicle.updateAnimations(self, 99999999, true)
87 end
88end

onReadStream

Description
Definition
onReadStream()
Code
92function Pickup:onReadStream(streamId, connection)
93 local isPickupLowered = streamReadBool(streamId)
94 self:setPickupState(isPickupLowered, true)
95end

onRootVehicleChanged

Description
Called if root vehicle changes
Definition
onRootVehicleChanged(table rootVehicle)
Arguments
tablerootVehicleroot vehicle
Code
113function Pickup:onRootVehicleChanged(rootVehicle)
114 local spec = self.spec_pickup
115 if spec.animationName ~= "" then
116 local actionController = rootVehicle.actionController
117 if actionController ~= nil then
118 if spec.controlledAction ~= nil then
119 spec.controlledAction:updateParent(actionController)
120 return
121 end
122
123 spec.controlledAction = actionController:registerAction("lowerPickup", InputAction.LOWER_IMPLEMENT, 2)
124 spec.controlledAction:setCallback(self, Pickup.actionControllerLowerPickupEvent)
125 spec.controlledAction:setFinishedFunctions(self, self.getIsLowered, true, false)
126 spec.controlledAction:setIsSaved(true)
127 if self:getAINeedsLowering() then
128 spec.controlledAction:addAIEventListener(self, "onAIImplementStartLine", 1)
129 spec.controlledAction:addAIEventListener(self, "onAIImplementEndLine", -1)
130 end
131 else
132 if spec.controlledAction ~= nil then
133 spec.controlledAction:remove()
134 end
135 end
136 end
137end

onSetLoweredAll

Description
Definition
onSetLoweredAll()
Code
106function Pickup:onSetLoweredAll(doLowering, jointDescIndex)
107 self:setPickupState(doLowering)
108end

onWriteStream

Description
Definition
onWriteStream()
Code
99function Pickup:onWriteStream(streamId, connection)
100 local spec = self.spec_pickup
101 streamWriteBool(streamId, spec.isLowered)
102end

prerequisitesPresent

Description
Definition
prerequisitesPresent()
Code
19function Pickup.prerequisitesPresent(specializations)
20 return SpecializationUtil.hasSpecialization(AnimatedVehicle, specializations)
21end

registerEventListeners

Description
Definition
registerEventListeners()
Code
58function Pickup.registerEventListeners(vehicleType)
59 SpecializationUtil.registerEventListener(vehicleType, "onLoad", Pickup)
60 SpecializationUtil.registerEventListener(vehicleType, "onPostLoad", Pickup)
61 SpecializationUtil.registerEventListener(vehicleType, "onReadStream", Pickup)
62 SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", Pickup)
63 SpecializationUtil.registerEventListener(vehicleType, "onSetLoweredAll", Pickup)
64 SpecializationUtil.registerEventListener(vehicleType, "onRootVehicleChanged", Pickup)
65 SpecializationUtil.registerEventListener(vehicleType, "onFoldTimeChanged", Pickup)
66end

registerFunctions

Description
Definition
registerFunctions()
Code
39function Pickup.registerFunctions(vehicleType)
40 SpecializationUtil.registerFunction(vehicleType, "allowPickingUp", Pickup.allowPickingUp)
41 SpecializationUtil.registerFunction(vehicleType, "setPickupState", Pickup.setPickupState)
42 SpecializationUtil.registerFunction(vehicleType, "loadPickupFromXML", Pickup.loadPickupFromXML)
43 SpecializationUtil.registerFunction(vehicleType, "getCanChangePickupState", Pickup.getCanChangePickupState)
44end

registerLoweringActionEvent

Description
Definition
registerLoweringActionEvent()
Code
243function Pickup:registerLoweringActionEvent(superFunc, actionEventsTable, inputAction, target, callback, triggerUp, triggerDown, triggerAlways, startActive, callbackState, customIconName)
244 local spec = self.spec_pickup
245 if spec.animationName ~= "" then
246 local _, actionEventId = self:addPoweredActionEvent(spec.actionEvents, InputAction.LOWER_IMPLEMENT, self, Pickup.actionEventTogglePickup, triggerUp, triggerDown, triggerAlways, startActive, callbackState, customIconName)
247 g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_HIGH)
248
249 Pickup.updateActionEvents(self)
250
251 if inputAction == InputAction.LOWER_IMPLEMENT then
252 return
253 end
254 end
255
256 superFunc(self, actionEventsTable, inputAction, target, callback, triggerUp, triggerDown, triggerAlways, startActive, callbackState, customIconName)
257end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
48function Pickup.registerOverwrittenFunctions(vehicleType)
49 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsLowered", Pickup.getIsLowered)
50 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getDirtMultiplier", Pickup.getDirtMultiplier)
51 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getWearMultiplier", Pickup.getWearMultiplier)
52 SpecializationUtil.registerOverwrittenFunction(vehicleType, "registerLoweringActionEvent", Pickup.registerLoweringActionEvent)
53 SpecializationUtil.registerOverwrittenFunction(vehicleType, "registerSelfLoweringActionEvent", Pickup.registerSelfLoweringActionEvent)
54end

registerSelfLoweringActionEvent

Description
Definition
registerSelfLoweringActionEvent()
Code
261function Pickup:registerSelfLoweringActionEvent(superFunc, actionEventsTable, inputAction, target, callback, triggerUp, triggerDown, triggerAlways, startActive, callbackState, customIconName, ignoreCollisions)
262 return Pickup.registerLoweringActionEvent(self, superFunc, actionEventsTable, inputAction, target, callback, triggerUp, triggerDown, triggerAlways, startActive, callbackState, customIconName, ignoreCollisions)
263end

setPickupState

Description
Definition
setPickupState()
Code
154function Pickup:setPickupState(isPickupLowered, noEventSend)
155 local spec = self.spec_pickup
156
157 if isPickupLowered ~= spec.isLowered then
158 PickupSetStateEvent.sendEvent(self, isPickupLowered, noEventSend)
159
160 spec.isLowered = isPickupLowered
161
162 if spec.animationName ~= "" then
163 local animTime = nil
164 if self:getIsAnimationPlaying(spec.animationName) then
165 animTime = self:getAnimationTime(spec.animationName)
166 end
167 if isPickupLowered then
168 self:playAnimation(spec.animationName, spec.animationLowerSpeed, animTime, true)
169 else
170 self:playAnimation(spec.animationName, spec.animationLiftSpeed, animTime, true)
171 end
172 end
173
174 Pickup.updateActionEvents(self)
175 end
176end

updateActionEvents

Description
Definition
updateActionEvents()
Code
267function Pickup.updateActionEvents(self)
268 local spec = self.spec_pickup
269
270 if spec.animationName ~= "" then
271 local actionEvent = spec.actionEvents[InputAction.LOWER_IMPLEMENT]
272 if actionEvent ~= nil then
273 if spec.isLowered then
274 g_inputBinding:setActionEventText(actionEvent.actionEventId, string.format(g_i18n:getText("action_liftOBJECT"), g_i18n:getText("typeDesc_pickup")))
275 else
276 g_inputBinding:setActionEventText(actionEvent.actionEventId, string.format(g_i18n:getText("action_lowerOBJECT"), g_i18n:getText("typeDesc_pickup")))
277 end
278
279 g_inputBinding:setActionEventActive(actionEvent.actionEventId, self:getCanChangePickupState(spec, not spec.isLowered))
280 end
281 end
282end