How to check if previous or next vehicle in train is a certain one in NML

Discussions about the technical aspects of graphics development, including NewGRF tools and utilities.

Moderator: Graphics Moderators

Post Reply
User avatar
LaRoso
Traffic Manager
Traffic Manager
Posts: 144
Joined: 15 Sep 2017 22:29
Location: Freiburg, BW, Germany

How to check if previous or next vehicle in train is a certain one in NML

Post by LaRoso »

Hi,

I've benn searching the NML Docs for quite some time but haven't found a solution.

What I try to achieve: When selecting the spriteset for a train vehicle, I want to check if the previous and/or next vehicle in the train consist is a specific one.

Let's say I have a wagon with the standard spriteset "set_wagon_a", but in case there is a certain other wagon or loco in front of it, it should be drawn from another spriteset "set_wagon_b". Is there a way to achieve that?

Greetings, LaRoso
Making NewGRFs from time to time.
Brickblock1
Traffic Manager
Traffic Manager
Posts: 132
Joined: 04 Apr 2022 12:44
Location: The openttd discord server
Contact:

Re: How to check if previous or next vehicle in train is a certain one in NML

Post by Brickblock1 »

This is possible in nml with using the old var format and var 0x61. NML docs, Query variable of n-th vehicle in chain (61)

Depending on what exactly you're trying to do it might be easier to use Livery override instead or maybe position_in_vehid_chain.
User avatar
LaRoso
Traffic Manager
Traffic Manager
Posts: 144
Joined: 15 Sep 2017 22:29
Location: Freiburg, BW, Germany

Re: How to check if previous or next vehicle in train is a certain one in NML

Post by LaRoso »

Thank you.

I guess, I'll stick to available solutions instead of using some deprecated stuff I don't even understand. :)
Making NewGRFs from time to time.
audigex
Tycoon
Tycoon
Posts: 2074
Joined: 09 Dec 2007 21:28
Contact:

Re: How to check if previous or next vehicle in train is a certain one in NML

Post by audigex »

LaRoso wrote: 15 Oct 2026 19:22 Thank you.

I guess, I'll stick to available solutions instead of using some deprecated stuff I don't even understand. :)
It’s quite easy if you use badges, and var61 looks complicated but is fairly simple at heart
Jon
User avatar
LaRoso
Traffic Manager
Traffic Manager
Posts: 144
Joined: 15 Sep 2017 22:29
Location: Freiburg, BW, Germany

Re: How to check if previous or next vehicle in train is a certain one in NML

Post by LaRoso »

audigex wrote: 19 Oct 2026 17:50 It’s quite easy if you use badges, and var61 looks complicated but is fairly simple at heart
Maybe, but I haven't found it being explained in a way I'd be able to understand anywhere yet.
Making NewGRFs from time to time.
audigex
Tycoon
Tycoon
Posts: 2074
Joined: 09 Dec 2007 21:28
Contact:

Re: How to check if previous or next vehicle in train is a certain one in NML

Post by audigex »

Actually thinking about it, you don't really need badges for this - it's similar to what we did for "push pull" before the recent feature was added for trains to reverse

Code: Select all

// Returns a special spriteset when the next vehicle is one of the target vehicles.
switch (FEAT_TRAINS, SELF, sw_next_vehicle_is_target, [
    STORE_TEMP(1, 0x10F),
    var[0x61, 0, 0xFFFF, 0xC6]
]) {
    item_some_vehicle_name:    spriteset_1;
    item_another_vehicle_name: spriteset_2;

    spriteset_default;
}


// Returns a special spriteset when the previous vehicle is one of the target vehicles.
sw_previous_vehicle_is_target, [
    STORE_TEMP(-1, 0x10F),
    var[0x61, 0, 0xFFFF, 0xC6]
]) {
    item_some_vehicle_name:    spriteset_1;
    item_another_vehicle_name: spriteset_2;

    spriteset_default;
}
If you want to check more generically if it's "either side" then you just chain the switches (eg the previous_vehicle_is_target switch returns the next_vehicle_is_target, instead of returning the default spriteset straight away)

These examples both show checking for two different units, obviously you can include just 1, or expand the list

This part looks quite complicated because it's a bit low level but it's actually fairly simple in terms of what it's doing, there's just no clean NML keyword for it

Code: Select all

[
    STORE_TEMP(1, 0x10F),
    var[0x61, 0, 0xFFFF, 0xC6]
]
Essentially STORE_TEMP puts an offset of either 1 (next vehicle) or -1 (previous vehicle) into a temporary variable 0x10F

Then var 0x61 means "read from another vehicle in the chain, using the offset at 0x10F"

0xC6 is the variable to read (vehicle ID), and the 0 and 0xFFFF are just "don't burnshift the result” and a mask to apply to the result... you don't really need to care about them here, they're just used in some other situations where you need to pull a specific group of bits out because OpenTTD does a lot of bit packing. Essentially if you don’t know what that means, just copy the values I gave and it’ll be fine - you don’t need to understand them here, don’t worry about it.

You can kinda think of those two lines as being equivalent to

Code: Select all

var offset = 1;
vehicle_id = get_variable_from_other_vehicle(offset, "vehicle_id");
... you just don't explicitly pass the offset variable in, because 0x61 just always uses 0x10F and so you put the value there for it to find

So you're then just switching on the vehicle ID of the next vehicle in the chain. If it matches one you want, you return a specific spriteset, otherwise you return the default one
Jon
User avatar
LaRoso
Traffic Manager
Traffic Manager
Posts: 144
Joined: 15 Sep 2017 22:29
Location: Freiburg, BW, Germany

Re: How to check if previous or next vehicle in train is a certain one in NML

Post by LaRoso »

audigex wrote: 20 Oct 2026 14:30 ...
Wow, thank you for the explanation. I think I understood now what happens here. I'll try to use this method. It looks like exactly what I've been trying to achieve
Making NewGRFs from time to time.
Post Reply

Return to “NewGRF Technical Discussions”

Who is online

Users browsing this forum: cheesepear and 2 guests