Home AMX User Forum AMX General Discussion

Initialization of LOCAL_VAR

Hi Experts,

I am new to AMX Programming.

I tried to define a variable as LOCAL_VAR inside the function and try to initialize it.

But NetLinx Studio prompt me an error saying that you can only initialize variable inside DEFINE_VARIABLES and DEFINE_CONSTANTS only ...

But NetLinx Language Reference Guide says that you can initialize the variable ... Check pages 11, 46, 47, 58 of the book and you will find the references for LOCAL_VAR. But when I tried to do that way Netlinx Compiler gave me an error C10213: Illegal Initialization for symbol ....

Can anyone help me with this ?

Regards,

MC78

Comments

  • PhreaKPhreaK Posts: 966
    Outside of the DEFINE_VARIABLES and DEFINE_CONSTANTS sections you have to declare the variable before assign it a value

    For example:
    LOCAL_VAR MY_VAR
    MY_VAR=1
    
  • But there is a problem ...

    Thanks for your response ...

    I understand you point that outside the DEFINE_VARIABLES and DEFINE_CONSTANTS you just put
    LOCAL_VAR MY_VAR
    MY_VAR=1
    

    But then it will not serve the purpose of LOCAL_VAR (Static Variable)

    Reason of LOCAL_VAR is, I want to retain its value till next use ... and as soon as it hit th MY_VAR = 1 it will reload with 1 ... and lost its previous value ...

    So that doesnt serve the purpose of LOCAL_VAR ...

    Please let me know if I am understanding incorrectly ...
    PhreaK wrote: »
    Outside of the DEFINE_VARIABLES and DEFINE_CONSTANTS sections you have to declare the variable before assign it a value

    For example:
    LOCAL_VAR MY_VAR
    MY_VAR=1
    
  • PhreaKPhreaK Posts: 966
    The assignment statement I put in there was just to demonstrate that definition and assignment must be separated. For it to retain the value you still have to define it in within the local scope, but simply do what you wish with it in your code. It will have a null value (0, 0.0, '') etc before you assign anything to it.
  • Thanks for that

    I think I understand what you mean ....

    so if you dont assign it, it will have a value 0 or 0.0. But what if I want to assign local variable with 5.

    Or what if the local variable is string (char array) and I want to assign some string to it ...

    Thanks in advance ...
    PhreaK wrote: »
    The assignment statement I put in there was just to demonstrate that definition and assignment must be separated. For it to retain the value you still have to define it in within the local scope, but simply do what you wish with it in your code. It will have a null value (0, 0.0, '') etc before you assign anything to it.
  • jweatherjweather Posts: 320
    If zero is not a normal value for the variable, just test if (my_var == 0) my_var = 5

    If that won't work, create a separate local_var that indicates whether the variable has been initialized or not. If it's zero, initialize my_var and set the flag to 1.
  • Thanks for that

    Thanks for your reply ...

    Finally I used second solution suggested by you ... use a variable as a flag to indicate whether its been used or not ...

    Thanks anyway for your responses ...
    jweather wrote: »
    If zero is not a normal value for the variable, just test if (my_var == 0) my_var = 5

    If that won't work, create a separate local_var that indicates whether the variable has been initialized or not. If it's zero, initialize my_var and set the flag to 1.
  • a_riot42a_riot42 Posts: 1,624
    I never use local_var except temporarily for debugging, but I thought that they didn't get initialized to 0, but rather will contain whatever that 2 bytes of memory held previously. I am not sure what you are trying to achieve but you may be better off not using local_var at all, and just using stack_var or a global variable.
    Paul
  • viningvining Posts: 4,368
    a_riot42 wrote:
    I never use local_var except temporarily for debugging,
    I'm the complete opposite and will only use a global if I can't use a local or stack. I beleive in keeping the scope of a variable to the section of code that requires it and only when I need the var in multiple locations through out the code will I make it global.

    If possible I'll use a stack but if I need to retain a value I'll use a local and if I need to access the var from various locations I'll go global.
  • jweatherjweather Posts: 320
    From help: "The keyword LOCAL_VAR specifies a static variable. A static variable's value is initialized the first time the statement block in which it is declared is executed and retained after execution of the statement block has finished."

    I agree with vining, and follow the "principle of least scope", so that each variable has the smallest scope possible. Generally my only global variables are for overall system state (projector power, etc.) and polling loops in DEFINE_PROGRAM. That way you don't forget and reuse "TEMPJ" in a function that gets called from another function using "TEMPJ" and wipe it out. Temporary/loop variables are always STACK_VARs.

    Jeremy
    a_riot42 wrote: »
    I never use local_var except temporarily for debugging, but I thought that they didn't get initialized to 0, but rather will contain whatever that 2 bytes of memory held previously. I am not sure what you are trying to achieve but you may be better off not using local_var at all, and just using stack_var or a global variable.
    Paul
  • a_riot42a_riot42 Posts: 1,624
    vining wrote: »
    a_riot42 wrote:

    I'm the complete opposite and will only use a global if I can't use a local or stack. I beleive in keeping the scope of a variable to the section of code that requires it and only when I need the var in multiple locations through out the code will I make it global.

    If possible I'll use a stack but if I need to retain a value I'll use a local and if I need to access the var from various locations I'll go global.

    My point wasn't that you should always use globals, that would be silly. I always prefer to use the stack rather than the heap whenever possible. It was more that if you find yourself using a lot of static variables in functions then it may be wise to look at your design and see if it can be written without static variables at all. They have their usefulness but I have found them to be abused in the netlinx language.
  • viningvining Posts: 4,368
    a_riot42 wrote:
    They have their usefulness but I have found them to be abused in the netlinx language.
    I'm if favor of abuse in any lanuage! :)
Sign In or Register to comment.