stom66

TTS Toggle Attachments

May 30th, 2025 (edited)
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.27 KB | None | 0 0
  1. function onLoad()
  2.     CreateButton()
  3.     enabled = false
  4. end
  5.  
  6. function CreateButton()
  7.     local btn = self.createButton({
  8.         click_function = "ToggleLock",
  9.         function_owner = self,
  10.         position       = {-1.75, 0.05, -1.7},
  11.         width          = 160,
  12.         height         = 160,
  13.         font_size      = 64,
  14.         label          = "x"
  15.     })
  16. end
  17.  
  18. function ToggleLock()
  19.     enabled = not enabled
  20.     print(enabled and "enabled" or "disabled")
  21.    
  22.     if not enabled then
  23.         self.removeAttachments()
  24.     end
  25.    
  26.     if enabled then
  27.         local objects = FindObjects()
  28.         AttachObjects(objects)
  29.     end
  30. end
  31.  
  32. function FindObjects()
  33.     local size = self.getBounds().size
  34.     local pos = self.getPosition()
  35.     local hitList = Physics.cast({
  36.         origin       = {pos.x, pos.y + size.y, pos.z},
  37.         direction    = {0,1,0},
  38.         type         = 3,
  39.         size         = {size.x,1,size.z},
  40.         max_distance = 0,
  41.         debug        = true,
  42.     })
  43.    
  44.     local result = {}
  45.     for _, obj in ipairs(hitList) do
  46.         if obj.hit_object and obj.hit_object.guid and obj.hit_object ~= self then
  47.             if obj.hit_object.GetPosition().y > pos.y then
  48.                 table.insert(result, obj.hit_object)
  49.             end
  50.         end
  51.     end
  52.     return result
  53. end
  54.  
  55. function AttachObjects(objects)
  56.     for _, obj in ipairs(objects) do
  57.         self.addAttachment(obj)
  58.     end
  59. end
Advertisement
Add Comment
Please, Sign In to add comment