Call a function within another Component

You may need to call a function from a different component for various reasons, such as reducing code duplication and promoting reusability.

You can use component.call to pass data to and call a function from a different component.

Here’s an example which uses component.call to call the main function in the target “ComponentB”:

function callComponentB() local Data = "some string data" -- Call the main function of Other and wait to hear back local Result = component.call{name="ComponentB", data=Data, func="main", async=false} return Result end
  • name of the target component. This component must be running.

  • data you need to send to the target component function. This is sent directly to the called function in the target component. If you target component is busy, this can take additional time to execute. To pass multiple data arguments, serialize a lua table to JSON.

  • func name of the target component function.

  • async boolean used to determine whether the current component Lua script should wait until it receives a response from the function that was called or continue immediately. Default is set to false.

Related pages