Files
secretsplash/Sample/Lua/example.lua
Jan Racek 4aa4e49424 psst
2026-03-24 13:00:54 +01:00

95 lines
3.2 KiB
Lua

-- ============================================================================
-- PSXSplash Example Script
-- Demonstrates all working Lua events and API calls.
-- Attach this to a PSXObjectExporter via the "Lua File" field.
-- ============================================================================
-- Per-object state (each object gets its own copy of these locals)
local bobAmplitude = 50 -- vertical bob range
local bobSpeed = 2 -- bob increment per frame
local bobPhase = 0
-- ============================================================================
-- LIFECYCLE EVENTS
-- ============================================================================
--- Called once when the object is first loaded into the scene.
function onCreate(self)
Debug.Log("Object created!")
local pos = Entity.GetPosition(self)
Debug.Log(" start pos: " .. pos.x .. ", " .. pos.y .. ", " .. pos.z)
end
--- Called every frame while the object is active.
--- WARNING: This runs on a 33 MHz MIPS R3000 — keep it light!
function onUpdate(self)
-- Simple vertical bob
bobPhase = bobPhase + bobSpeed
if bobPhase > 360 then bobPhase = bobPhase - 360 end
local pos = Entity.GetPosition(self)
pos.y = pos.y + PSXMath.Sign(180 - bobPhase) * bobAmplitude / 60
Entity.SetPosition(self, pos)
end
--- Called when the object is destroyed or the scene unloads.
function onDestroy(self)
Debug.Log("Object destroyed!")
end
-- onEnable / onDisable are available but omitted here to reduce log noise.
-- function onEnable(self) Debug.Log("Object enabled") end
-- function onDisable(self) Debug.Log("Object disabled") end
-- ============================================================================
-- INTERACTION EVENTS (requires PSXInteractable component)
-- ============================================================================
--- Called when the player interacts with this object.
function onInteract(self)
Debug.Log("Player interacted!")
local active = Entity.IsActive(self)
Entity.SetActive(self, not active)
end
-- ============================================================================
-- COLLISION / TRIGGER EVENTS
-- ============================================================================
--- Called when this object's collider overlaps another.
function onCollision(self, other)
Debug.Log("Collision with another object")
end
--- Called on the first frame two triggers overlap.
function onTriggerEnter(self, other)
Debug.Log("Trigger enter")
end
--- Called every frame while two triggers continue to overlap.
function onTriggerStay(self, other)
-- Expensive! Avoid heavy work here.
end
--- Called when two triggers stop overlapping.
function onTriggerExit(self, other)
Debug.Log("Trigger exit")
end
-- ============================================================================
-- INPUT EVENTS
-- ============================================================================
--- Called when any button is pressed this frame.
function onButtonPress(self, button)
if button == Input.CROSS then
Debug.Log("Cross pressed!")
end
end
--- Called when any button is released this frame.
function onButtonRelease(self, button)
if button == Input.CROSS then
Debug.Log("Cross released!")
end
end