ScriptEvents_Broadcast.lua 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. function ScriptTrace(txt)
  10. Debug.Log(txt)
  11. end
  12. function ScriptExpectTrue(condition, msg)
  13. if (not condition) then
  14. ScriptTrace(msg)
  15. end
  16. end
  17. -- This example shows how to implement a handler for a Script Event Broadcast
  18. -- Broadcast Script Events do not specify an address type and so may be handled
  19. -- by connecting to the Script Event.
  20. luaScriptEventBroadcast = {
  21. -- This method will be called as a result of a Broadcast call on the Script Event.
  22. BroadcastMethod0 = function(self, param1, param2)
  23. ScriptTrace("Handler: " .. tostring(param1) .. " " .. tostring(param2))
  24. ScriptExpectTrue(typeid(param1) == typeid(0), "Type of param1 must be "..tostring(typeid(0)))
  25. ScriptExpectTrue(typeid(param2) == typeid(EntityId()), "Type of param2 must be "..tostring(typeid(EntityId())))
  26. ScriptExpectTrue(param1 == 2, "The first parameter must be 2")
  27. ScriptExpectTrue(param2 == EntityId(23456), "The received entity Id must match the one sent")
  28. ScriptTrace("BroadcastMethod0 Called")
  29. return true
  30. end,
  31. BroadcastMethod1 = function(self)
  32. ScriptTrace("BroadcastMethod1 Called")
  33. end
  34. }
  35. local scriptEventDefinition = ScriptEvent("Script_Broadcast") -- Script_Broadcast will be the name of the callable Script Event
  36. -- Define methods for Script_Broadcast
  37. local method0 = scriptEventDefinition:AddMethod("BroadcastMethod0", typeid(false)) -- Adding a method expects a method name and an optional return type.
  38. method0:AddParameter("Param0", typeid(0))
  39. method0:AddParameter("Param1", typeid(EntityId()))
  40. -- NOTE: Type's are specified using the typeid keyword with a VALUE of the type you wish (for example, typeid("EntityId")
  41. -- will produce the type id for a string, and not the type of EntityId)
  42. scriptEventDefinition:AddMethod("BroadcastMethod1")
  43. -- Once the Script Event is defined, call Register to enable it, typically this should be done within OnActivate
  44. scriptEventDefinition:Register()
  45. -- At this point, the Script Event is usable, so we will connect a handler to it, this will install luaScriptEventBroadcast as the Handler
  46. -- which will provide implementations to the methods we defined.
  47. scriptEventHandler = Script_Broadcast.Connect(luaScriptEventBroadcast)
  48. -- In order to test the event, we will Broadcast "BroadcastMethod0" which as defined will return a Boolean value and expects two parameters
  49. local returnValue = Script_Broadcast.Broadcast.BroadcastMethod0(2, EntityId(23456))
  50. -- We know that BroadcastMethod0 should return true, so we will verify the result.
  51. ScriptExpectTrue(returnValue, "BroadcastMethod0's return value must be true")
  52. -- Broadcast an event without a return or parameters, the BroadcastMethod1 will be invoked
  53. Script_Broadcast.Broadcast.BroadcastMethod1()