LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

MotorGearShiftEvent

Description
Event for sending shifting inputs from client to server
Parent
Event
Functions

emptyNew

Description
Create instance of Event class
Definition
emptyNew()
Return Values
tableselfinstance of class event
Code
23function MotorGearShiftEvent.emptyNew()
24 local self = Event.new(MotorGearShiftEvent_mt)
25 return self
26end

new

Description
Create new instance of event
Definition
new(table vehicle, boolean turnedOn)
Arguments
tablevehiclevehicle
booleanturnedOnis turned on
Code
32function MotorGearShiftEvent.new(vehicle, shiftType, shiftValue)
33 local self = MotorGearShiftEvent.emptyNew()
34 self.vehicle = vehicle
35 self.shiftType = shiftType
36 self.shiftValue = shiftValue
37 return self
38end

readStream

Description
Called on client side on join
Definition
readStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
44function MotorGearShiftEvent:readStream(streamId, connection)
45 self.vehicle = NetworkUtil.readNodeObject(streamId)
46 self.shiftType = streamReadUIntN(streamId, 4)
47
48 if self.shiftType == MotorGearShiftEvent.TYPE_SELECT_GEAR or self.shiftType == MotorGearShiftEvent.TYPE_SELECT_GROUP then
49 self.shiftValue = streamReadUIntN(streamId, 3)
50 end
51
52 self:run(connection)
53end

run

Description
Run action on receiving side
Definition
run(integer connection)
Arguments
integerconnectionconnection
Code
71function MotorGearShiftEvent:run(connection)
72 if self.vehicle ~= nil and self.vehicle:getIsSynchronized() then
73 local spec = self.vehicle.spec_motorized
74 if spec ~= nil and spec.isMotorStarted then
75 if self.shiftType == MotorGearShiftEvent.TYPE_SHIFT_UP then
76 spec.motor:shiftGear(true)
77 elseif self.shiftType == MotorGearShiftEvent.TYPE_SHIFT_DOWN then
78 spec.motor:shiftGear(false)
79 elseif self.shiftType == MotorGearShiftEvent.TYPE_SELECT_GEAR then
80 spec.motor:selectGear(self.shiftValue, self.shiftValue ~= 0)
81 elseif self.shiftType == MotorGearShiftEvent.TYPE_SHIFT_GROUP_UP then
82 spec.motor:shiftGroup(true)
83 elseif self.shiftType == MotorGearShiftEvent.TYPE_SHIFT_GROUP_DOWN then
84 spec.motor:shiftGroup(false)
85 elseif self.shiftType == MotorGearShiftEvent.TYPE_SELECT_GROUP then
86 spec.motor:selectGroup(self.shiftValue, self.shiftValue ~= 0)
87 elseif self.shiftType == MotorGearShiftEvent.TYPE_DIRECTION_CHANGE then
88 spec.motor:changeDirection()
89 elseif self.shiftType == MotorGearShiftEvent.TYPE_DIRECTION_CHANGE_POS then
90 spec.motor:changeDirection(1)
91 elseif self.shiftType == MotorGearShiftEvent.TYPE_DIRECTION_CHANGE_NEG then
92 spec.motor:changeDirection(-1)
93 end
94 end
95 end
96end

sendEvent

Description
Broadcast event from server to all clients, if called on client call function on server and broadcast it to all clients
Definition
sendEvent(table vehicle, integer shiftType, integer shiftValue)
Arguments
tablevehiclevehicle
integershiftTypetype of shifting event
integershiftValueadditional value for shifting event
Code
103function MotorGearShiftEvent.sendEvent(vehicle, shiftType, shiftValue)
104 if g_client ~= nil then
105 g_client:getServerConnection():sendEvent(MotorGearShiftEvent.new(vehicle, shiftType, shiftValue))
106 end
107end

writeStream

Description
Called on server side on join
Definition
writeStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
59function MotorGearShiftEvent:writeStream(streamId, connection)
60 NetworkUtil.writeNodeObject(streamId, self.vehicle)
61 streamWriteUIntN(streamId, self.shiftType, 4)
62
63 if self.shiftType == MotorGearShiftEvent.TYPE_SELECT_GEAR or self.shiftType == MotorGearShiftEvent.TYPE_SELECT_GROUP then
64 streamWriteUIntN(streamId, self.shiftValue, 3)
65 end
66end