Home AMX User Forum NetLinx Studio

How to use a structure as a global variable for intermodule communication?

Hi everybody,
I have a program which consists of a Main file and two modules. In the main file the following structure is declared:

DEFINE_TYPE
STRUCTURE MyStruct
{ LONG Num;
CHAR Name[30];
}

DEFINE_VARIABLE
MyStruct Var_struct;

DEFINE_MODULE 'FirstModule' FirstModule(Var_struct);
DEFINE_MODULE 'SecondModule' SecondModule(Var_struct);

I want it to be visible in both modules. In order to do that I defined the structure in the DEFINE_TYPE section in the modules and declared the MyStruct Var_struct in the formal argument list of both modules. It looks like this:

// Excerpt from the First Module
MODULE_NAME='FirstModule' FirstModule(MyStruct Var_struct);
DEFINE_TYPE
STRUCTURE MyStruct
{ LONG Num;
CHAR Name[30];
}

// Excerpt from the Second Module
MODULE_NAME='SecondModule' SecondModule(MyStruct Var_struct);
DEFINE_TYPE
STRUCTURE MyStruct
{ LONG Num;
CHAR Name[30];
}

But the compiler failed to compile the modules. Apparently it started parsing from the top of each module. By that moment the structure was undefined. So is it possible to use structures as a global variable for intermodule communication?

Your insights are appreciated.
Thanks in advance,

Paul,
Seior programmer,
Disystems,
Yekaterinburg,
Russia

Comments

  • Unfortunately it's not possible to pass structures into and out of a module directly....

    The only way to do something similar is to convert the structure with VARIABLE_TO_XML() or VARIABLE_TO_STRING() into a variable, pass the variable's content out of the module and into the next module, where it can be converted back with XML_TO_VARIABLE() / STRING_TO_VARIABLE().
  • DHawthorneDHawthorne Posts: 4,584
    You could use the marshalling protocols as Marc suggested, or you could just break the elements of your stucture out into its constituent variables, pass those to the module as parameters, and reassign them to an internal structure in the module. Either way, you would have to actively update the contents on both ends; changing one won't automatically update the other.

    I've taken to trying to reduce all my inter-module communications to something that can be encapsulated in a virtual device using levels and channels, then using said virtual for a command interface (send_commands) for everything else. In most cases, passing a structure, or its contents, has not been necessary ...but it sure would be convenient if we could.
Sign In or Register to comment.