MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame^] | 1 | CONCEPT |
| 2 | |
| 3 | VARIABLE INITIALIZATION |
| 4 | |
| 5 | DESCRIPTION |
| 6 | Global variables, like their local counterparts, can be defined |
| 7 | with an initial value: |
| 8 | |
| 9 | int * a = ({ 3, 4 }); |
| 10 | |
| 11 | The initialization value can be any legal LPC expression, |
| 12 | including function calls. The code for the initializations is |
| 13 | collected in a compiler-generated function __INIT() which is |
| 14 | called even before the create-hook is applied on the object. |
| 15 | |
| 16 | During initialization, blueprints and clones are treated |
| 17 | slightly differently: |
| 18 | |
| 19 | Blueprint variables are always all initialized using __INIT(). |
| 20 | |
| 21 | For clones the programmer can select whether the clone's |
| 22 | variables should also be initialized with __INIT(), or if they |
| 23 | should be assigned from the current blueprint values |
| 24 | ('shared with the blueprint'). The latter method is useful for |
| 25 | example if blueprints and clones shall share arrays or |
| 26 | mappings. |
| 27 | |
| 28 | The selection is performed with the two pragmas |
| 29 | 'init_variables' and 'share_variables'. The status of this |
| 30 | pragma at the point of the first variable definition counts, |
| 31 | and is applied to all variables in the program. |
| 32 | |
| 33 | The clone initialization method is evaluated per-program, i.e. |
| 34 | if an inherited program defines 'share_variables' and the child |
| 35 | program doesn't, only the inherited variables are initialized |
| 36 | from the blueprint, and all others from __INIT(). |
| 37 | |
| 38 | The default setting for the pragma is configured into the |
| 39 | driver, but can also be chosen when starting the driver. |
| 40 | |
| 41 | EXAMPLE |
| 42 | For the object |
| 43 | |
| 44 | ---------- |
| 45 | inherit "a"; |
| 46 | int a = 4; |
| 47 | int b; |
| 48 | ---------- |
| 49 | |
| 50 | the compiler executes the equivalent of these __INIT() functions: |
| 51 | |
| 52 | #pragma share_variables: |
| 53 | |
| 54 | unknown __INIT() |
| 55 | { |
| 56 | "a"::__INIT(); |
| 57 | if (clonep(this_object())) |
| 58 | { |
| 59 | a = (blueprint of this_object())->a; |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | a = 4; |
| 64 | } |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | #pragma init_variables: |
| 70 | |
| 71 | unknown __INIT() |
| 72 | { |
| 73 | "a"::__INIT(); |
| 74 | a = 4; |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | In either case the variable 'b' (in fact all variables) are |
| 80 | set to '0' as part of the loading/cloning process before the |
| 81 | driver performs the specific initialisation. |
| 82 | |
| 83 | WARNING |
| 84 | Do not call __INIT() yourself, overload, or use it directly in |
| 85 | any other way. The implementation of the variable |
| 86 | initialization may change at any time. |
| 87 | |
| 88 | HISTORY |
| 89 | Before LDMud 3.3.378, the choice between sharing and initializing |
| 90 | variables was a fixed configuration choice of the driver. |
| 91 | |
| 92 | SEE ALSO |
| 93 | pragma(LPC), create(H), invocation(D) |