LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

PlaceableWeighingStation

Description
Specialization for placeables
Functions

onDelete

Description
Definition
onDelete()
Code
112function PlaceableWeighingStation:onDelete()
113 local spec = self.spec_weighingStation
114
115 if spec.trigger ~= nil then
116 removeTrigger(spec.trigger)
117 spec.trigger = nil
118 end
119end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
61function PlaceableWeighingStation:onLoad(savegame)
62 local spec = self.spec_weighingStation
63
64 local key = "placeable.weighingStation"
65
66 spec.trigger = self.xmlFile:getValue(key .. "#triggerNode", nil, self.components, self.i3dMappings)
67 if spec.trigger == nil then
68 Logging.xmlError(self.xmlFile, "Missing vehicle triggerNode for weighing station")
69 return
70 end
71
72 addTrigger(spec.trigger, "onWeighingTriggerCallback", self)
73 spec.triggerVehicleNodes = {}
74 spec.vehicles = {}
75
76 spec.displays = {}
77 self.xmlFile:iterate(key .. ".display", function(_, displayKey)
78 local displayNode = self.xmlFile:getValue(displayKey .. "#node", nil, self.components, self.i3dMappings)
79 if displayNode ~= nil then
80 local fontName = self.xmlFile:getValue(displayKey .. "#font", "DIGIT"):upper()
81 local fontMaterial = g_materialManager:getFontMaterial(fontName, self.customEnvironment)
82
83 if fontMaterial ~= nil then
84 local display = {}
85
86 local alignmentStr = self.xmlFile:getValue(displayKey .. "#alignment", "RIGHT")
87 local alignment = RenderText["ALIGN_" .. alignmentStr:upper()] or RenderText.ALIGN_RIGHT
88
89 local size = self.xmlFile:getValue(displayKey .. "#size", 0.03)
90 local scaleX = self.xmlFile:getValue(displayKey .. "#scaleX", 1)
91 local scaleY = self.xmlFile:getValue(displayKey .. "#scaleY", 1)
92 local mask = self.xmlFile:getValue(displayKey .. "#mask", "00.0")
93 local emissiveScale = self.xmlFile:getValue(displayKey .. "#emissiveScale", 0.2)
94 local color = self.xmlFile:getValue(displayKey .. "#color", {0.9, 0.9, 0.9, 1}, true)
95 local hiddenColor = self.xmlFile:getValue(displayKey .. "#hiddenColor", nil, true)
96
97 display.displayNode = displayNode
98 display.formatStr, display.formatPrecision = string.maskToFormat(mask)
99 display.fontMaterial = fontMaterial
100 display.characterLine = fontMaterial:createCharacterLine(display.displayNode, mask:len(), size, color, hiddenColor, emissiveScale, scaleX, scaleY, alignment)
101
102 table.insert(spec.displays, display)
103 end
104 end
105 end)
106
107 self:setWeightDisplay(0)
108end

onWeighingTriggerCallback

Description
Trigger callback
Definition
onWeighingTriggerCallback(integer triggerId, integer otherId, boolean onEnter, boolean onLeave, boolean onStay)
Arguments
integertriggerIdid of trigger
integerotherIdid of actor
booleanonEnteron enter
booleanonLeaveon leave
booleanonStayon stay
Code
167function PlaceableWeighingStation:onWeighingTriggerCallback(triggerId, otherId, onEnter, onLeave, onStay)
168 if onEnter or onLeave then
169 local spec = self.spec_weighingStation
170 if onEnter then
171 spec.triggerVehicleNodes[otherId] = true
172 else
173 -- on leave
174 spec.triggerVehicleNodes[otherId] = nil
175 end
176
177 self:updateWeightDisplay()
178 end
179end

prerequisitesPresent

Description
Checks if all prerequisite specializations are loaded
Definition
prerequisitesPresent(table specializations)
Arguments
tablespecializationsspecializations
Return Values
booleanhasPrerequisitetrue if all prerequisite specializations are loaded
Code
18function PlaceableWeighingStation.prerequisitesPresent(specializations)
19 return true
20end

registerEventListeners

Description
Definition
registerEventListeners()
Code
32function PlaceableWeighingStation.registerEventListeners(placeableType)
33 SpecializationUtil.registerEventListener(placeableType, "onLoad", PlaceableWeighingStation)
34 SpecializationUtil.registerEventListener(placeableType, "onDelete", PlaceableWeighingStation)
35end

registerFunctions

Description
Definition
registerFunctions()
Code
24function PlaceableWeighingStation.registerFunctions(placeableType)
25 SpecializationUtil.registerFunction(placeableType, "onWeighingTriggerCallback", PlaceableWeighingStation.onWeighingTriggerCallback)
26 SpecializationUtil.registerFunction(placeableType, "updateWeightDisplay", PlaceableWeighingStation.updateWeightDisplay)
27 SpecializationUtil.registerFunction(placeableType, "setWeightDisplay", PlaceableWeighingStation.setWeightDisplay)
28end

registerXMLPaths

Description
Definition
registerXMLPaths()
Code
39function PlaceableWeighingStation.registerXMLPaths(schema, basePath)
40 schema:setXMLSpecializationType("WeighingStation")
41
42 schema:register(XMLValueType.NODE_INDEX, basePath .. ".weighingStation#triggerNode", "Vehicle trigger")
43
44 schema:register(XMLValueType.NODE_INDEX, basePath .. ".weighingStation.display(?)#node", "Display start node")
45 schema:register(XMLValueType.STRING, basePath .. ".weighingStation.display(?)#font", "Display font name")
46 schema:register(XMLValueType.STRING, basePath .. ".weighingStation.display(?)#alignment", "Display text alignment")
47 schema:register(XMLValueType.FLOAT, basePath .. ".weighingStation.display(?)#size", "Display text size")
48 schema:register(XMLValueType.FLOAT, basePath .. ".weighingStation.display(?)#scaleX", "Display text x scale")
49 schema:register(XMLValueType.FLOAT, basePath .. ".weighingStation.display(?)#scaleY", "Display text y scale")
50 schema:register(XMLValueType.STRING, basePath .. ".weighingStation.display(?)#mask", "Display text mask")
51 schema:register(XMLValueType.FLOAT, basePath .. ".weighingStation.display(?)#emissiveScale", "Display emissive scale")
52 schema:register(XMLValueType.COLOR, basePath .. ".weighingStation.display(?)#color", "Display text color")
53 schema:register(XMLValueType.COLOR, basePath .. ".weighingStation.display(?)#hiddenColor", "Display text hidden color")
54
55 schema:setXMLSpecializationType()
56end

setWeightDisplay

Description
Definition
setWeightDisplay()
Code
150function PlaceableWeighingStation:setWeightDisplay(mass)
151 local spec = self.spec_weighingStation
152 for _, display in ipairs(spec.displays) do
153 local int, floatPart = math.modf(mass)
154
155 local value = string.format(display.formatStr, int, math.abs(math.floor(floatPart * (10 ^ display.formatPrecision))))
156 display.fontMaterial:updateCharacterLine(display.characterLine, value)
157 end
158end

updateWeightDisplay

Description
Definition
updateWeightDisplay()
Code
123function PlaceableWeighingStation:updateWeightDisplay()
124 local spec = self.spec_weighingStation
125
126 -- resolve nodes to vehicles, add to table to eliminate duplicates
127 for node, _ in pairs(spec.triggerVehicleNodes) do
128 if entityExists(node) then
129 local vehicle = g_currentMission:getNodeObject(node)
130 if vehicle ~= nil and vehicle.getTotalMass ~= nil then
131 spec.vehicles[vehicle] = true
132 end
133 else
134 -- vehicle does not exist anymore
135 spec.triggerVehicleNodes[node] = nil
136 end
137 end
138
139 local mass = 0
140 for vehicle in pairs(spec.vehicles) do
141 mass = mass + vehicle:getTotalMass(true)
142 end
143 table.clear(spec.vehicles)
144
145 self:setWeightDisplay(mass*1000)
146end