Home AMX User Forum AMX Technical Discussion

calling functions from different modules.

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!!

Comments

  • I don't know exactly what you are trying to accomplish but it doesn't get much more immediate than sending and capturing messages to/from a virtual device.
  • MorgoZMorgoZ Posts: 116
    Well, what i mean by inmediate is that if you call a function like

    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!!!
  • ericmedleyericmedley Posts: 4,177
    Well, in the first place, a module is not he best choice to do why your asking. This might be a good case for doing an include instead. While we could have a long discussion about how Netlinx 'should' behave with regards to how modules interact with the main code, we have to deal with how it does.

    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.
  • viningvining Posts: 4,368
    You would need to put a wait in your code to delay the code that requires that value from executing and you would need a global var to hold the value received in the calling module's data_event command handler.

    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.
  • DHawthorneDHawthorne Posts: 4,584
    Netlinx is not really an OOP language. Modules are not much like objects, and calling an internal function, or even making an internal function global, or available outside the scope of the module, is not possible.

    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.
Sign In or Register to comment.