Can I pass variables along with Runtime:dispatchEvent?

If I have an external module that I want to send a message back to the module that called it, I know I can use Runtime:dispatchEvent({name = “doSomething”}) 

My question is can I do something like this but also pass parameters along to the function as well?

If so, can someone help me out with what the syntax should look like? 

Hi,

Not sure about passing arguments using the runtime dispatch event, but you can create your own dispatcher which can. It’s already built into the API.

https://docs.coronalabs.com/api/library/system/newEventDispatcher.html

Hope that helps.

-dev

So in the link you posted I see this line:

eventDispatcher:dispatchEvent( { name=“custom” } )

If I was including extra params with it can you give an example of that line of code? Just not sure where exactly I add the params at.

Simply specify whatever params you wish to pass:

[lua]

dispatchEvent( { name=“custom”, params=“Hi” } )

[/lua]

And receive the params in the listener:

[lua]

local function onCustomEvent( event )
    print( “Received custom event!” )
    for k,v in pairs(event) do
        print(k,v)
    end
    – or:
    print(event.params)
end

[/lua]

Dave

Thank you Dave!   Makes sense now with the example.   :slight_smile: