Attribute 'global_init_slot'

The pragma defines the order of the initialization of programming blocks and global variable lists. Variables within a GVL or POU are initialized from top to bottom. In the case of several global variable lists, the initialization sequence is undefined.

For initializations with literal values, for example 1, 'hello', 3.6, or constants of basic data types, the order of the initializations is irrelevant. However, if there are interdependencies in the initializations, you must set the initialization order. To do this, you can assign a defined initialization slot to a GVL or POU with the attribute 'global_init_slot.

Constants are initialized before the variables, in the same order as the variables. During initialization, the signatures (function blocks, GVLs) are first sorted according to the value for <slot>. Then the code for initializing the constants is generated, followed by the code for initializing the variables.

The initialization is thus divided into the following phases:

  1. The signatures are sorted according to the initialization slots. The slots are either defined implicitly or explicitly via the attribute 'global_init_slot'.
  2. All constants are then initialized. This is done in the order of the slots. Signatures without constants are skipped.
  3. Then the variables are initialized, again in the order of the slots.

Syntax: {attribute 'global_init_slot' := '<slot>'}

<slot>: Integer value that defines the position in the order of the calls. The default value for a POU (program, function block) is 50000. The default value for a GVL is 49990. The default value for static variables is 49980. A lower value results in earlier initialization.

Insertion location: The pragma always affects the entire GVL or POU and must therefore be above the VAR_GLOBAL declaration or the POU declaration.

Attribute 'global_init_slot' 1:

If several programming blocks have been assigned the same value for the 'global_init_slot' attribute, the order of their initialization remains undefined. To avoid influencing the system behavior of TwinCAT 3, use values above 40000.

Sample:

The project contains two global variable lists GVL1 and GVL2. The MAIN program uses variables from both lists. For the initialization of a variable nA, GVL1 uses the variable nB, which is initialized in GVL2 with a value of 1000:

GVL1:

VAR_GLOBAL   //49990
    nA : INT := GVL2.nB*3;
END_VAR

GVL2:

VAR_GLOBAL   //49990
    nB : INT := 1000;
    nC : INT := 10;
END_VAR

MAIN program:

PROGRAM MAIN  //50000
VAR
    nVar1 : INT := GVL1.nA;
    nVar2 : INT;
END_VAR
nVar1 := nVar + 1;
nVar2 := GVL2.nC;

In this case, the compiler issues an error because GVL2.nB is used to initialize GVL1.nA before GVL2 has been initialized. You can prevent this by setting the global_init_slot attribute to the position of GVL2 in the initialization sequence before GVL1.

To do this, the global_init_slot value of GVL2 must be smaller than the value of GVL1 (with the default value 49990) and greater than 40000 (reserved for system functions).
I.e.: 40001 <= global_init_slot value of GVL2 <= 49989.

GVL2:

{attribute 'global_init_slot' := '40500'}
VAR_GLOBAL
    nB : INT := 1000;
    nC : INT := 10;
END_VAR

The use of GVL2.nC in the implementation part of MAIN is uncritical even without using a pragma, since both GVLs are always initialized before the program.