Home AMX User Forum AMX General Discussion

Global Variables

Hi all,
I'm writing a netlink program which has the following components:
- main.axs
- data.axi
- ipmodule.axs

both axs files include `data.axi`, without any problem.
I've declared the following variable into the data.axi file :

define_variable
slong globalCounter = -1;

So I can refer to `globalCounter` into each part of my program.
But, If I write :
BUTTON_EVENT[dvTP,19]
{
PUSH:
{
SEND_COMMAND dvTP, "'@TXT',2,FORMAT('"Global counter is %d"',globlaCounter)";
}
}
into the main file, the value shown on my TP is the value assigned to globalCounter into `define start` section,
if I put the same code into ipmodule.axs file, which is modified at runtime, the right value is shown.
Can someone tell me why I cannot write event handler into main.axi file?
Can someone tell me how works the global variables?
Tahnks in advance
Best regards ND

Comments

  • ericmedleyericmedley Posts: 4,177
    There are a variety of things that could be going on.

    Can you post the part of your code where you declare the includes?

    That might give us a clue as to how to help.
    e
  • viningvining Posts: 4,368
    If you want to share/change values between your module and your main code your have several choices but the simplest is to pass the variable/s that you want to share/change in the header of the module declaration.

    By including the data.axi in the 2 .axs file you merely included the contents of the .axi and thus created 2 instances of the contents that are completely independant of each other. They're actually different values since the constants, variable, functions or what have you are flagged with an id that makes them unique to the file they are included into. So if your data.axi has a variable named nVar for instance when it's included in the main compiled code it may be flagged by the compiler as main.nVar or something while in the ipmodule.axs it may be ipmodule.nVar so they are referencesd as seperate entities by the processor not the same so they don't share values.

    In your main code:
    DEFINE_MODULE 'IP_Module' IP_Mod_1 (vdvIP,dvIP,nVarToShare)
    
    In your module header:
    MODULE_NAME='IP_Module'  (DEV vdvIP,DEV dvIP,INTEGER nVarToShare)
    
    Of course you don't have to leave the names the same since values are passed by reference to the location in the module header not by name:
    MODULE_NAME='IP_Module'  (DEV vdvMod_IP,DEV dvMod_IP,INTEGER nMyModVarToShare)
    
  • Hi all,
    thanks for you answers.

    If I understand I cannot declare a global variable shared from more module (axs files) into an include file, because the compiler cannot discriminate which of them has been involved.
    A way to share some variables between modules is pass them as module parameters.
    But if I declare the variable just into an include file, why I can see all variables already delcared in all program parts?
    Any way, I attach my code so if you have time, can see it, and give me some help to better use netlink environment.
    Best Regards
    ND

    DATA.AXI

    PROGRAM_NAME='data'

    #if_not_defined __DATA__
    #define __DATA__

    define_variable
    slong globalCounter = -1;

    #end_if
  • viningvining Posts: 4,368
    Just like when you look at the vars in debug they have different file paths when they get compiled they get different pointers (file paths) to their storage compartment. (Pardon my programming illiteracy of terms but I'm only a mild manner construction worker).

    So you can see them all but they are all really different.
  • truetrue Posts: 307
    vining wrote: »
    By including the data.axi in the 2 .axs file you merely included the contents of the .axi and thus created 2 instances of the contents that are completely independant of each other. They're actually different values since the constants, variable, functions or what have you are flagged with an id that makes them unique to the file they are included into. So if your data.axi has a variable named nVar for instance when it's included in the main compiled code it may be flagged by the compiler as main.nVar or something while in the ipmodule.axs it may be ipmodule.nVar so they are referencesd as seperate entities by the processor not the same so they don't share values.

    Not really, meaning the names are not used (except for debugging). It works similar to how you described it but the technical details are quite a bit different.
  • viningvining Posts: 4,368
    true wrote:
    Not really, meaning the names are not used (except for debugging). It works similar to how you described it but the technical details are quite a bit different.
    A name, a pointer, human readable, machine readable, potatoe', pototoe'. Although I really don't know the technical specifics of how all this stuff works my analogy or explanation is really intended so simple folks like myself can better understand. After all for what we're attempting to do the specific technical understanding isn't important as long as we have a general understanding of how Netlinx works. I leave the technical stuff to the smart folks while I concentrate on making buttons turn on and off. :)
  • So the global variables don't have a global scope but just a limited module scope?
  • viningvining Posts: 4,368
    Depends where the global variable is declared. If declared in the main code they are global to the main code. If they are declared in an include file pulled into the main code they are global to the main code from the point of inclusion on so that any earlier includes can't use them which IMHO should be changed. If they are declared in a module they are global to that module and that's it. Of the many ways to share values between the main code and modules, passing the value in the module header is the easiest and doesn't require any further action or coding.
  • DHawthorneDHawthorne Posts: 4,584
    If you put your variables in an include file, each code segment, main and modules, winds up with their own copy. Includes are nothing more than a shortcut to typing the code in them out in each segment. Since variables declared in a module have a scope limited to that module, declaring them in an include is declaring a new variable with module scope, just the same as if you simply declared them in the module.

    The term "global variable" is not particularly meaningful with modules. It may be global in terms of the main code, or global in terms of the module, but unless it's declared in the main code and passed as a parameter, it's never global to both. Don't be confused by the fact that the debugger sees them with the same name. They are different variables. The debugger doesn't inherit the unique token generated when the module is initialized (which I understand is the variable name plus the instance label of the module). You might think you are looking at one variable, but are really looking at another.
  • Thanks for your answers
    ND
Sign In or Register to comment.