calling functions from different modules.
MorgoZ
Posts: 116
Hi all,
i need to call a function in a module from another module (something like getting an instance of one module within the other and then executing its function to get the returned value)
Is it possible ???
I think that the only way to communicate with a module is through a virtual device (isn´t it?), sending messages to it. But it doesn´t help in my case since i need an "inmediate" answer with the returned value once the function is called.
Thanks in advance!!
i need to call a function in a module from another module (something like getting an instance of one module within the other and then executing its function to get the returned value)
Is it possible ???
I think that the only way to communicate with a module is through a virtual device (isn´t it?), sending messages to it. But it doesn´t help in my case since i need an "inmediate" answer with the returned value once the function is called.
Thanks in advance!!
0
Comments
value = function1()
-- Use value ---
you get the "value" inmediatly, but if i send a message through a virtual device to the module to indicate it that i need the "value", i wont have it inmediatly after the message.
For example, "function1()" is inside the module "Module1", which receives messages through the virtual device "vdvModule1", and the command "GET_VALUE" is used to execute "function1" and then send the "value" to the caller.
send_command vdvModule1, "'GET_VALUE'"
-- Can´t use value jet, got to wait for a response message from vdvModule1 --
Thanks!!!
There are ways to do what your asking but it does require staying in the sandbox of moving info in and out via commands/strings.
You send_command requesting the value and pass the module the DPS of the calling module and then that module will execute the request and in return send_command the value back to the caller's DPS. The caller's data_event command handler receives the string and stores the value in a global var. Shortly thereafter the wait expires and the code needing this value executes using the value stored in that global var.
Maybe this is just a bad example of what your trying to do but if you know what vDev to send_command to then you already should know the instance of that module since most module instances use a unique virtual DPS's in the first place. Maybe your better off passing arrays into the modules to store the values needed in both.
My solution is to give every module a virtual device as an interface. Then I create a handler so I can SEND_COMMAND to the virtual device, and the module will do its thing, and, if necessary, send data back via SEND_STRING. IMO, it's the best way to keep module functions as encapsulated as possible.