DespawnOnEntityDeactivate.lua 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 DespawnOnEntityDeactivate =
  12. {
  13. Properties =
  14. {
  15. Prefab = { default=SpawnableScriptAssetRef(), description="Prefab to spawn" }
  16. },
  17. }
  18. function DespawnOnEntityDeactivate:OnActivate()
  19. self.spawnableMediator = SpawnableScriptMediator()
  20. self.ticket = self.spawnableMediator:CreateSpawnTicket(self.Properties.Prefab)
  21. self.spawnableNotificationsBusHandler = SpawnableScriptNotificationsBus.Connect(self, self.ticket:GetId())
  22. self.spawnableMediator:Spawn(self.ticket)
  23. end
  24. function DespawnOnEntityDeactivate:OnSpawn(spawnTicket, entityList)
  25. self.spawnableNotificationsBusHandler:Disconnect()
  26. self.entityBusNotificationsHandler = EntityBus.Connect(self, entityList[1])
  27. GameEntityContextRequestBus.Broadcast.DeactivateGameEntity(entityList[1])
  28. end
  29. function DespawnOnEntityDeactivate:OnEntityDeactivated(entityId)
  30. self.spawnableMediator:Despawn(self.ticket)
  31. end
  32. return DespawnOnEntityDeactivate