NewGRF with NML and Python

Discuss, get help with, or post new graphics for TTDPatch and OpenTTD, using the NewGRF system, here. Graphics for plain TTD also acceptable here.

Moderator: Graphics Moderators

Post Reply
User avatar
GuilhermeJK
Engineer
Engineer
Posts: 55
Joined: 01 Nov 2014 05:24
Location: São José dos Campos, Brazil

NewGRF with NML and Python

Post by GuilhermeJK »

Hello.

So, I'm considering experimenting again with NewGRFs (I dropped the idea years ago due a combination of depression + masters degree + taxing corporate job, now I only have a less worse depression and my final masters thesis).

I have a mix bag of ideas and while taking a look at various sources, I noticed that some NewGRFs are coded in Python instead of NML o.O. An example is the awesome FIRS project.

How does coding a NewGRF in Python work? From a development perspective, what advantages does it have over pure NML? Is there a guide or something on coding NewGRF using Python?

P.S. NewGRF-wise, I have done some stuff in NML and those modifications of the HEX code for Early TTRS, Swedish Houses and TaI. I also am familiar with Python already :)
User avatar
andythenorth
Tycoon
Tycoon
Posts: 5658
Joined: 31 Mar 2007 14:23
Location: Lost in Music

Re: NewGRF with NML and Python

Post by andythenorth »

(Incomplete) guide to Python + NML here viewtopic.php?f=68&t=58390

The principal benefit of using python is separation of content (data) from presentation (nml code). Any other similar language would work just as well.

For me this provides three useful benefits:

1. Removes repetition

Instead of writing out all the nml for every vehicle (or industry), including all the switch chains, the nml is written once in a template.

This means that if nml needs maintained / changed, it's changed once in a template and applied everywhere.

The downside is that it's possible to construct very complex conditional templates (I do), and these can require a lot of design and debugging themselves (I make this mistake).

Similarly, global values can be used for things like vehicle speeds, refittable cargos etc. Changing these in one place changes them everywhere they are used. Pure nml would require careful find and replace, or manual changes.

2. Offload calculation and value checking

Defining properties (for e.g. vehicles) in python means they can be checked for valid values trivially, before they reach nml (this means errors are found faster).

Properties in python can also be used in calculations, which are then passed to the nml template, for example I calculate capacity for vehicles based on length and intro date.

NML can do really quite complex calculations, but it's not as flexible as python and it makes for hard-to-read nml code.

3. Re-use the values elsewhere.p

Because the vehicle / industry / cargo properties aren't locked up in nml files, they can be re-used by other programs. So for example
* I auto-generate html docs + images for all my newgrfs
* I auto-generate cargo sprites for vehicles
* I use a shared library of cargo labels, refits and other constants between all my newgrfs

None of this is needed for a simple grf, but for making multiple large sets, it would be awful to not have the data separate from the nml files.

Doesn't have to be python. Other approaches are available.
User avatar
GuilhermeJK
Engineer
Engineer
Posts: 55
Joined: 01 Nov 2014 05:24
Location: São José dos Campos, Brazil

Re: NewGRF with NML and Python

Post by GuilhermeJK »

Interesting, I feel then that some of my more ambitious ideas would be suited for Python then.

Thanks Andy :D
Maginred
Engineer
Engineer
Posts: 2
Joined: 16 Apr 2020 08:10

Re: NewGRF with NML and Python

Post by Maginred »

I was happier and more knowledgeable when I read it.
Post Reply

Return to “Graphics Development”

Who is online

Users browsing this forum: Ahrefs [Bot] and 35 guests