text.script 1.2 KB

123456789101112131415161718192021222324
  1. function init(self)
  2. msg.post(".", "acquire_input_focus") -- <1>
  3. self.message = "" -- <2>
  4. end
  5. function on_input(self, action_id, action)
  6. if action_id == hash("type") then
  7. self.message = self.message .. action.text -- <3>
  8. label.set_text("#label", self.message) -- <4>
  9. elseif action_id == hash("backspace") and action.repeated then
  10. local l = string.len(self.message)
  11. self.message = string.sub(self.message, 0, l-1) -- <5>
  12. label.set_text("#label", self.message) -- <6>
  13. end
  14. end
  15. --[[
  16. 1. Tell the engine that this object ("." is shorthand for the current game object) wants to receive input. The function `on_input()` will be called whenever input is received.
  17. 2. Store a variable in the script component with the text that the user types.
  18. 3. If the "type" text trigger action is sent, add the typed text to the variable `message` that stores the text.
  19. 4. Set the label component to the stored text.
  20. 5. If the user presses <kbd>backspace</kbd>, set the stored text to a substring starting at the beginning of the stored text and ending at the length of the stored text minus 1. This erases the last character from the stored text.
  21. 6. Set the label component to the stored text.
  22. --]]