LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

PlaceableColorable

Description
Specialization for placeables
Functions

getAvailableColors

Description
Definition
getAvailableColors()
Code
138function PlaceableColorable:getAvailableColors()
139 return self.spec_colorable.colors
140end

getColor

Description
Definition
getColor()
Code
163function PlaceableColorable:getColor()
164 return self.spec_colorable.currentColorIndex
165end

getHasColors

Description
Definition
getHasColors()
Code
169function PlaceableColorable:getHasColors()
170 return #self.spec_colorable.colors > 0 and #self.spec_colorable.nodes > 0
171end

loadFromXMLFile

Description
Definition
loadFromXMLFile()
Code
119function PlaceableColorable:loadFromXMLFile(xmlFile, key)
120 local spec = self.spec_colorable
121
122 spec.currentColorIndex = xmlFile:getValue(key .. ".color", math.min(#spec.colors, 1))
123 self:setColor(spec.currentColorIndex)
124end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
59function PlaceableColorable:onLoad(savegame)
60 local spec = self.spec_colorable
61 local xmlFile = self.xmlFile
62
63 spec.nodes = {}
64 spec.colors = {}
65 spec.currentColorIndex = 0
66
67 xmlFile:iterate("placeable.colorable.nodes.node", function (_, nodeKey)
68 local node = xmlFile:getValue(nodeKey .. "#node", nil, self.components, self.i3dMappings)
69 if node ~= nil then
70 if not getHasShaderParameter(node, "colorScale0") then
71 Logging.xmlWarning(xmlFile, "Node '%s' has no shader parameter 'colorScale0' for key '%s'!", getName(node), nodeKey)
72 else
73 table.insert(spec.nodes, node)
74 end
75 end
76 end)
77
78 xmlFile:iterate("placeable.colorable.colors.color", function (_, colorKey)
79 local color = xmlFile:getValue(colorKey .. "#value", nil, true)
80
81 if color ~= nil then
82 table.insert(spec.colors, color)
83 end
84
85 if #spec.colors == 255 then
86 return false
87 end
88 end)
89
90 if #spec.colors > 0 then
91 self:setColor(1)
92 end
93
94 if not self:getHasColors() then
95 SpecializationUtil.removeEventListener(self, "onWriteStream", PlaceableColorable)
96 SpecializationUtil.removeEventListener(self, "onReadStream", PlaceableColorable)
97 end
98end

onReadStream

Description
Definition
onReadStream()
Code
102function PlaceableColorable:onReadStream(streamId, connection)
103 local spec = self.spec_colorable
104
105 spec.currentColorIndex = streamReadUInt8(streamId)
106 self:setColor(spec.currentColorIndex)
107end

onWriteStream

Description
Definition
onWriteStream()
Code
111function PlaceableColorable:onWriteStream(streamId, connection)
112 local spec = self.spec_colorable
113
114 streamWriteUInt8(streamId, spec.currentColorIndex)
115end

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 PlaceableColorable.prerequisitesPresent(specializations)
19 return true
20end

registerEventListeners

Description
Definition
registerEventListeners()
Code
33function PlaceableColorable.registerEventListeners(placeableType)
34 SpecializationUtil.registerEventListener(placeableType, "onLoad", PlaceableColorable)
35 SpecializationUtil.registerEventListener(placeableType, "onWriteStream", PlaceableColorable)
36 SpecializationUtil.registerEventListener(placeableType, "onReadStream", PlaceableColorable)
37end

registerFunctions

Description
Definition
registerFunctions()
Code
24function PlaceableColorable.registerFunctions(placeableType)
25 SpecializationUtil.registerFunction(placeableType, "getAvailableColors", PlaceableColorable.getAvailableColors)
26 SpecializationUtil.registerFunction(placeableType, "setColor", PlaceableColorable.setColor)
27 SpecializationUtil.registerFunction(placeableType, "getColor", PlaceableColorable.getColor)
28 SpecializationUtil.registerFunction(placeableType, "getHasColors", PlaceableColorable.getHasColors)
29end

registerSavegameXMLPaths

Description
Definition
registerSavegameXMLPaths()
Code
50function PlaceableColorable.registerSavegameXMLPaths(schema, basePath)
51 schema:setXMLSpecializationType("Colorable")
52 schema:register(XMLValueType.INT, basePath .. ".color", "Active color index")
53 schema:setXMLSpecializationType()
54end

registerXMLPaths

Description
Definition
registerXMLPaths()
Code
41function PlaceableColorable.registerXMLPaths(schema, basePath)
42 schema:setXMLSpecializationType("Colorable")
43 schema:register(XMLValueType.NODE_INDEX, basePath .. ".colorable.nodes.node(?)#node", "Bees action radius")
44 schema:register(XMLValueType.COLOR, basePath .. ".colorable.colors.color(?)#value", "Color")
45 schema:setXMLSpecializationType()
46end

saveToXMLFile

Description
Definition
saveToXMLFile()
Code
128function PlaceableColorable:saveToXMLFile(xmlFile, key, usedModNames)
129 local spec = self.spec_colorable
130
131 if self:getHasColors() then
132 xmlFile:setValue(key .. ".color", spec.currentColorIndex)
133 end
134end

setColor

Description
Definition
setColor()
Code
144function PlaceableColorable:setColor(index)
145 local spec = self.spec_colorable
146
147 index = math.min(index, #spec.colors)
148 spec.currentColorIndex = index
149
150 -- No colors available
151 if index == 0 then
152 return
153 end
154
155 local r, g, b = unpack(spec.colors[index])
156 for _, node in ipairs(spec.nodes) do
157 setShaderParameter(node, "colorScale0", r, g, b, 1, false)
158 end
159end