Page MenuHomeFeedback Tracker

Variable assignments fail to evaluate expressions that reference other constants
Closed, ResolvedPublic

Description

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)

Details

Severity
Minor
Resolution
Open
Reproducibility
Always
Operating System
Windows 10 x64
Operating System Version
1903
Category
General
Steps To Reproduce

See description for details, but basically:

  1. Define a constant, e.g. const int FOO = 100;
  2. Define another constant, e.g. const int BAR = 50;
  3. Define a variable whose value is assigned from an expression that includes the above constants, e.g. int baz = FOO - BAR;
  4. The variable will have an unexpected value
Additional Information
Version 1.07.153006

Event Timeline

tjensen created this task.Apr 11 2020, 8:17 PM
komer added a subscriber: komer.Apr 12 2020, 2:38 PM
Geez changed the task status from New to Assigned.Apr 15 2020, 12:14 PM
Geez changed the task status from Assigned to Need More Info.Jan 8 2021, 2:06 PM
Geez added a subscriber: Geez.

Hello tjensen.
Is this still an issue? As there have been some changes that could have fixed this.
Regards,
Geez

tjensen added a comment.EditedJan 10 2021, 2:55 AM

Hi @Geez . It looks like this issue is not fixed in DayZ Server version 1.10.153598.

I am attaching a zip file containing the server config (test.cfg) and mission folder (mission.chernarusplus) I used to reproduce the issue:

This the command line I used to run DayZ server to reproduce the issue:

.\DayZServer_x64.exe "-config=test.cfg" "-profiles=profile" "-mission=P:\testing\mission.chernarusplus"

Here are the logs generated from running the above command:

As you can see from the script log, the printed DIFF value is still 0, which does not equal the expected value, 110000.

Geez closed this task as Resolved.Jun 29 2021, 10:52 AM
Geez claimed this task.

Hello again.
The issue has been fixed internally and the fix will appear in one of the future updates.
Regards,
Geez