There is some non-intuitive behavior related to variable assignments. Attempting to assign a variable with an expression that references other constants gives unexpected results.
For example, I created a simple mission class that exhibits the unexpected behavior:
class CustomMission extends MissionServer { static private const int FIRST = 2 * 60 * 1000; static private const int SECOND = 10 * 1000; static private const int DIFF = FIRST - SECOND; void CustomMission() { Print("FIRST = " + FIRST); Print("SECOND = " + SECOND); Print("DIFF = " + DIFF); GetGame().RequestExit(0); } }
Running DayZ server with this mission results in the following script logs:
SCRIPT : FIRST = 120000 SCRIPT : SECOND = 10000 SCRIPT : DIFF = 0
The actual logged value of DIFF is 0, but I expected it to be 110000.
I thought maybe this was a scoping problem, so I tried changing the DIFF definition to:
static private const int DIFF = CustomMission.FIRST - CustomMission.SECOND;
However, the results were the same.
I then thought maybe the problem was caused by either the static or private modifier (or both), and tried:
class CustomMission extends MissionServer { void CustomMission() { const int FIRST = 2 * 60 * 1000; const int SECOND = 10 * 1000; const int DIFF = FIRST - SECOND; Print("FIRST = " + FIRST); Print("SECOND = " + SECOND); Print("DIFF = " + DIFF); GetGame().RequestExit(0); } }
This resulted in the compiler error: Can't find variable 'FIRST'
So, then I tried removing the const modifier on the variable whose assignment expression references the other constants:
class CustomMission extends MissionServer { void CustomMission() { const int FIRST = 2 * 60 * 1000; const int SECOND = 10 * 1000; int DIFF = FIRST - SECOND; Print("FIRST = " + FIRST); Print("SECOND = " + SECOND); Print("DIFF = " + DIFF); GetGame().RequestExit(0); } }
This resulted in the same unexpected output as before:
SCRIPT : FIRST = 120000 SCRIPT : SECOND = 10000 SCRIPT : DIFF = 0
The only working solution I have found so far is to remove the const modifiers entirely:
class CustomMission extends MissionServer { void CustomMission() { int FIRST = 2 * 60 * 1000; int SECOND = 10 * 1000; int DIFF = FIRST - SECOND; Print("FIRST = " + FIRST); Print("SECOND = " + SECOND); Print("DIFF = " + DIFF); GetGame().RequestExit(0); } }
...which gives the expected results:
SCRIPT : FIRST = 120000 SCRIPT : SECOND = 10000 SCRIPT : DIFF = 110000
Are const variables expected to behave this way?
I recognize that the DayZ scripting language resembles C/C++/C# but is also different from those languages, but it seems like this issue makes the const modifier less useful. I couldn't find any documentation that explains this behavior on the Enforce Script Syntax wiki page. (Also, signups are disabled so I can't update it to share this knowledge with other DayZ modders)