ScriptEvents_Addressable.lua 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 that requires an address
  18. -- in order for a handler to be invoked
  19. luaScriptEventWithId = {
  20. -- This method will be broadcast, but only handlers connected to the matching address
  21. -- as the one specified in the event will invoke it
  22. MethodWithId0 = 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 == 1, "The first parameter must be 1")
  27. ScriptExpectTrue(param2 == EntityId(12345), "The received entity Id must match the one sent")
  28. ScriptTrace("MethodWithId0 handled")
  29. return true
  30. end,
  31. MethodWithId1 = function(self)
  32. ScriptTrace("MethodWithId1 handled")
  33. end
  34. }
  35. -- "Script_Event" will be the name of the callable Script Event, it will require the address type to be a string.
  36. local scriptEventDefinition = ScriptEvent("Script_Event", typeid("")) -- Event address is of string type
  37. -- Will define some methods that handlers may implement
  38. local method0 = scriptEventDefinition:AddMethod("MethodWithId0", typeid(false)) -- Return value is Boolean
  39. method0:AddParameter("Param0", typeid(0))
  40. method0:AddParameter("Param1", typeid(EntityId()))
  41. -- NOTE: Type's are specified using the typeid keyword with a VALUE of the type you wish (for example, typeid("EntityId")
  42. -- will produce the type id for a string, and not the type of EntityId)
  43. scriptEventDefinition:AddMethod("MethodWithId1") -- No return, no parameters
  44. -- Once the Script Event is defined, call Register to enable it, typically this should be done within OnActivate
  45. scriptEventDefinition:Register()
  46. -- At this point, the Script Event is usable, so we will connect a handler to it, this will install luaScriptEventWithId as the Handler
  47. -- which will provide implementations to the methods we defined. Notice that we are connecting with the string "ScriptEventAddress"
  48. -- as the address for this event. Any methods sent to a different address would not be handled by this handler we are connecting.
  49. scriptEventHandler = Script_Event.Connect(luaScriptEventWithId, "ScriptEventAddress")
  50. -- Now we will invoke the event and we will specify "ScriptEventAddress" as the address, this means the handler we previously
  51. -- connected will be able to handle this event.
  52. local returnValue = Script_Event.Event.MethodWithId0("ScriptEventAddress", 1, EntityId(12345))
  53. -- We know that "Method0" should return true, we verify that it is.
  54. ScriptExpectTrue(returnValue, "Method0's return value must be true")
  55. -- Finally we send "MethodWithdId1" which does not require any parameters, but still needs the address to be provided.
  56. Script_Event.Event.MethodWithId1("ScriptEventAddress")