SimpleSpawnDespawn.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. ----------------------------------------------------------------------------------------------------
  2. --
  3. -- Copyright (c) Contributors to the Open 3D Engine Project.
  4. -- For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. --
  6. -- SPDX-License-Identifier: Apache-2.0 OR MIT
  7. --
  8. --
  9. --
  10. ----------------------------------------------------------------------------------------------------
  11. local SimpleSpawnDespawn =
  12. {
  13. Properties =
  14. {
  15. Prefab = {default=SpawnableScriptAssetRef(), description="Prefab to spawn"},
  16. DespawnTime={default=3, description="When to despawn the entity"},
  17. },
  18. }
  19. function SimpleSpawnDespawn:OnActivate()
  20. self.spawnableMediator = SpawnableScriptMediator()
  21. self.ticket = self.spawnableMediator:CreateSpawnTicket(self.Properties.Prefab)
  22. self.spawnableMediator:SpawnAndParentAndTransform(self.ticket, self.entityId, Vector3(0.0,0.0,5.0), Vector3(0.0,0.0,90.0), 5.0 )
  23. self.spawnableNotificationsBusHandler = SpawnableScriptNotificationsBus.Connect(self, self.ticket:GetId())
  24. end
  25. function SimpleSpawnDespawn:OnTick(deltaTime, timePoint)
  26. self.timeRemaining = self.timeRemaining-deltaTime
  27. if (self.timeRemaining < 0) then
  28. self.tickBusHandler:Disconnect()
  29. self.spawnableMediator:Despawn(self.ticket)
  30. end
  31. end
  32. function SimpleSpawnDespawn:OnSpawn(spawnTicket, entityList)
  33. self.timeRemaining = self.Properties.DespawnTime
  34. self.tickBusHandler = TickBus.Connect(self)
  35. end
  36. function SimpleSpawnDespawn:OnDespawn(spawnTicket)
  37. self.spawnableNotificationsBusHandler:Disconnect()
  38. ConsoleRequestBus.Broadcast.ExecuteConsoleCommand([[quit]])
  39. end
  40. return SimpleSpawnDespawn