Skip to content

Instantly share code, notes, and snippets.

@d-lua-stuff
Last active September 4, 2020 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d-lua-stuff/771505a417353f08c4341af58740d3c7 to your computer and use it in GitHub Desktop.
Save d-lua-stuff/771505a417353f08c4341af58740d3c7 to your computer and use it in GitHub Desktop.
-- Define a global script object with event handlers
script = {}
function script.onStart ()
-- Display some text
screen.setCenteredText("script started")
-- Create some timers to show that the script is working
unit.setTimer("a", 2) -- timer id "a", ticks every 2 seconds
unit.setTimer("b", 3) -- timer id "b", ticks every 3 seconds
end
function script.onStop ()
screen.setCenteredText("script stopped")
end
function script.onActionStart (actionName)
screen.setCenteredText(actionName .. " key pressed")
end
function script.onActionStop (actionName)
screen.setCenteredText(actionName .. " key released")
end
function script.onTick (timerId)
screen.setCenteredText("timer " .. timerId .. " ticked")
end
-- Other events that are available by default:
-- * onActionLoop(actionName): action key is held
-- * onUpdate(): executed once per frame
-- * onFlush(): executed 60 times per second, for physics calculations only; setEngineCommand must be called from here
-- Slot events are available if slot type is set with the --slot command line option.
function script.onMouseDown (x, y)
screen.setCenteredText("mouse down: x=" .. x .. " , y=" .. y)
end
-- Call the start event handler
-- Alternatively, initialization code can be placed anywhere in this file.
-- The only requirement is that there is a global "script" object with event handlers
script.onStart()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment