The builtin {cargo|rail|road|tram}type functions do not work if the corresponding tables are defined by the user

Open
#299 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
python
Domain
compilers

Research direction

Start by reproducing the failure with the attached test.nml from cargo_type_test.zip using nmlc, then inspect functioncall.py around lines 480-494 and the global constant tables. Done means user-defined cargo and railtype tables are available to cargotype() and railtype() so the example compiles successfully.

Written by the indexing model from the issue text.

Description

There is a topic on the forum that the cargotype function does not work:
https://www.tt-forums.net/viewtopic.php?t=90428

I did some test and looks like that in functioncall.py the global constant tables are empty if the tables defined by the nml file:
https://github.com/OpenTTD/nml/blob/bbe945ed348dfe19d284908983c8bb55e0d840ad/nml/expression/functioncall.py#L480C1-L485

I put the following commands after line 494:

    generic.print_dbg(6, global_constants.cargo_numbers)
    generic.print_dbg(6, global_constants.railtype_table)

If I define the cargotable and the railtypetable, then that is the output:

      {}
      {}

So looks like the tables are empty.
If I do not define the railtypetable, the second table is not empty:

      {}
      {'RAIL': 0, 'MONO': 1, 'MGLV': 2}

Version

$ nmlc --version
0.7.4

Expected result:
The attached nml file can be compiled

Actual result:
nmlc ERROR: "test.nml", line 16: Parameter for railtype() must be a string literal that is also in your railtype table

Steps to reproduce:

  • nmlc test.nml
    Output:
    nmlc ERROR: "test.nml", line 16: Parameter for railtype() must be a string literal that is also in your railtype table

  • Edit the test.nml: comment out the railtypetable:
    // railtypetable { RAIL, ELRL, MONO, MGLV }
    nmlc test.nml
    Output:
    nmlc ERROR: "test.nml", line 24: Parameter for cargotype() must be a string literal that is also in your cargo table

  • Comment out the cargotable and the cargotype_available if statements too:

// cargotable { PASS, MAIL, LVST }
// if (cargotype_available("LVST"))
// {
// 	item(FEAT_CARGOS, cargo_lvst, cargotype("LVST"))
// 	{ 
// 		property { weight: 12.0/16; }
// 	}
// }

nmlc test.nml
Output:
nmlc info: 0 sprites, 0 cached, 0 orphaned, 0 duplicates, 0 newly encoded (native)
nmlc info: Railtype items: 1/64
nmlc info: Concurrent ActionD registers: 1/64 ("test.nml", line 14)

cargo_type_test.zip

Dominant language
Python
Stars
61
Forks
47
Avg merge
8d 14h
Merged PRs (30d)
3

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from OpenTTD/nml

All issues in OpenTTD/nml

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.