COMMUNITY FORUM

Onload and onUpdate

Forum Overview >> Scripting

CategoryScripting
Created26.02.2025 00:59


Jorn Van Soesbergen (jornvs123) 26.02.2025 00:59
Hello. I am trying to make a mod that chances the behaviour of a pallet discharging with a lua script. I got the script to load in (The first debug message poped up) but i cant get the onLoad and onUpdate functions to work. Does anyone know what i did wrong.

---script----
PlanteraPallet = {}
print("PlanteraPallet script is loaded !") -- Debug message

function PlanteraPallet.prerequisitesPresent(specializations)
return true
end
function PlanteraPallet:registerFunctions(vehicleType)
SpecializationUtil.registerFunction(vehicleType, "updateDischargeState", PlanteraPallet.updateDischargeState)
end

function PlanteraPallet.registerEventListeners(vehicleType)

SpecializationUtil.registerEventListener(vehicleType "onLoad", PlanteraPallet)

SpecializationUtil.registerEventListener(vehicleType, "onUpdate", PlanteraPallet)
end



function PlanteraPallet:onLoad(savegame)
print("PlanteraPallet script is initialized!")
self.canDischarge = false
end

function PlanteraPallet:onUpdate(dt)
print('I am loaded')
self:updateDischargeState(dt)
-- Ensure discharge is controlled by `canDischarge`
if self.canDischarge then
print(" Discharge ENABLED")
self:setDischargeState(Dischargeable.DISCHARGE_STATE_OBJECT) -- Start discharging
self:raiseActive()
else
print("Discharge DISABLED")
self:setDischargeState(Dischargeable.DISCHARGE_STATE_OFF) -- Stop discharging
self:raiseActive()
end
end

function PlanteraPallet:updateDischargeState(dt)
if self.components ~= nil and self.components[1] ~= nil then
-- Get rotation in RADIANS
local x, y, z = getRotation(self.components[1].node)

-- Convert to DEGREES
local xDeg = math.deg(x)
local zDeg = math.deg(z)

-- Debugging: Show actual rotation
print(string.format("Pallet Rotation (Degrees): X=%.2f, Y=%.2f, Z=%.2f", xDeg, math.deg(y), zDeg))

-- Only allow discharge when X or Z is near 180°
if (math.abs(xDeg) > 170 and math.abs(xDeg) < 190) or (math.abs(zDeg) > 170 and math.abs(zDeg) < 190) then
self.canDischarge = true
print("Pallet is upside down. Can discharge.")
else
self.canDischarge = false
print(" Pallet is NOT upside down. No discharge.")
end
else
print(" Error: Could not retrieve pallet rotation.")
end
end

---Moddesc--
<author>Jron</author>
<version>1.0.0.0</version>
<title>
<en>Plantera pallets</en>
</title>

<description>
<en><![CDATA[
Category: Pallets
Price: $150
]]></en>

</description>

<iconFilename>Plantera.dds</iconFilename>

<multiplayer supported="true"/>

<storeItems>
<storeItem xmlFilename="PlanteraPallet.xml"/>

</storeItems>

<brands>
<brand name="PLANTERA" title="Plantera" image="brand/brand.dds"/>
</brands>

<specializations>
<specialization name="PlanteraPallets" className="PlanteraPallet" filename="PlanteraPallet.lua"/>
</specializations>

<vehicleTypes>
<vehicleType name="pallet">
<specialization name="PlanteraPallets"/>
</vehicleType>
</vehicleTypes>


----Part of my pallet XML---
<base>
<typeDesc>$l10n_typeDesc_pallet</typeDesc>
<filename>PlanteraPallet.i3d</filename>
<size width="1.7" length="1.3" height="1.3" />
<supportsRadio>false</supportsRadio>
<canBeReset>true</canBeReset>
<supportsPickup>true</supportsPickup>
<input allowed="true"/>
<components maxMass="400">
<component centerOfMass="0 0.1 0" solverIterationCount="10" mass="150"/>
</components>
<showInVehicleMenu>true</showInVehicleMenu>
<mapHotspot available="false" />
<specialization name="PlanteraPallets"/>
</base>


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