First Attempt At AI Engineering


New member
This will be my first attempt at using AI in a layout. I wonder if some experienced members can point me in the right direction.:)

Here is what I want to do. I am modeling Conemaugh & Blacklick Railroad a ex-Bethlehem Steel shortline railroad. I want the Mainline PPR to deliver Coal, Limestone, Iron Ore and Pick up Wire, Rails, Girders, emptys...etc.
The shortline will be operated manually (by me) and the main line by AI.

Here what I have done so far....

1.) Working on shortline building industries and mainline connections.
2.) Researched track plans for the mainline.

Here what little I know about AI.

1.) Track direction is important to AI. The main line will have two tracks east and two tracks west. Track will be built in the proper direction.
2.) The main receiving yards will be 5 tracks east and 5 tracks west connected to the main line on either side of the mainline with tracks running in the proper direction.
3.) Signaling - I will be using PRR NEC signals at intervals between 1/2 and 1 Mile per track plan.

The Vision:
Coal Train approaches from the east pulls off on East Receiving Yard on Track #3. Stops. Unhooks load. Preceeds to past turnout. Backs up and connects to Cars in Track #4. Preceeds East to Portal.

Can I do this with Trainz 2004?
What do I have to do as far as trackmarks or other commands?
I like complex trackwork but will the AI?
Any tips or other guidence would be much appreciated.

Btw I am asking now before I lay the mainline tracks and screw things up.:D
In 2004 the best solution to this is to use the "couple at trackmark" command. place trackmarks each end of the 5 tracks, named to suit the location (ie east track 1 and east track1a). Place a named trackmark at the point the engine will drive to after uncoupling. Your AI will have the following:-

Drive to trackmark 3a
Drive to trackmark past junction
Couple at trackmark 4
Drive to portal

The couple at trackmark does not need to know the name or number of any wagon you need to couple to. It will find that on its own.
Does the number and position of Crossovers Matter?

Does the number and position of Crossovers Matter to AI? What is the best signaling position not to confuse AI?

Any advice before I build?
The only advice i've got is to experiment...

Be VERY flexible in your plans as the AI is as thick as two short planks.

remember, you're designing for an idiot...

if the idiot can screw up, then you haven't done it right and need to think about it again.


Does the number and position of Crossovers Matter to AI? What is the best signaling position not to confuse AI?

Any advice before I build?

If you find a way so AI is not cunfused let us all know. There are so many bits of information flying around in trainz calculating everthing every time you add more instructions it alters the routine by a fraction.

My session-building experience (I'm pretty close to being a newbie) has mainly been with a couple of maps that came with TRS 2006. I have not spent time on track direction, which might be one of my problems.

Engines in proximity to each other - I'll have one train working fine, then introduce another consist with engine and have the first one get mixed up - go forward instead of reverse, fail to set a switch that it has set before, fail to back up and couple at trackmark, etc. This may happen when placing them on the same main or branch line, even when several signal blocks separate them. It can also happen when a second engine runs in from elsewhere to a siding that turns out from the branch or main on which is the 1st engine - and even affects what the first engine does if it is also on a siding turning out from that same branch or main.

AI engineers plan their own routes from point A to point B unless you specifically tell them how to go by using "via trackmark." This is another artefact of engines on the same route if it has enough trackage to make alternate routes possible - but they can be far apart, as though one calls the other and says "you go this way and I'll go that way". Maybe they do, but it fouls things up if you are waiting for a given engine to trip a trigger when it doesn't go that way.

Signals sometimes control traffic well, but sometimes they don't.

I've also found that backing an engine to push a consist may lead to problems, especially if they are more than a few cars in length. So, I've been inserting wyes and turntables to turn the engine and then using it on the front end as much as possible. Even if it has to push, it seems to do better when it is facing forward.

Something else I've found you can't do is manually couple a subconsist to the rear of an AI engineer's train. If you do, the whole list of commands for the AI engineer is wiped out.

Your vision is doable - I'm doing something similar now, with a loaded coal train running south and a consist of empties coming back north, using the same branch line. What complicates it is that I have one of them, as it runs north empty, also picking up loaded crude tankers at a point, and then have a switch engine take the crude for unloading while the coal train loads, then put the consist back together so that the empty tankers can be dropped off when the now-loaded coal train goes back south to a power plant. I keep the road engines running forward at the front of the trains by running the whole train around a loop on the north end and turning the engine on a wye at the south end. Then with a couple other engines, I manually run other "errands". But, I have to keep an eye on the AI engineers -- once in a while, one of them can't set a switch that he usually does, and he hangs up with a big red exclamation mark in his picture icon.

Seems that the problem with multiple engines may be the AI engineer in the cab. It's been suggested by stagecoach that I have an AI engineer move to another engine, more or less off scene, and then use the command "postmessage" to let another engineer know that the first is out of the way so that something like a couple-at-trackmark can be made. There is another command called "waitformessageandclear" that needs to be used to clear the "timetable" when the 2nd engineer gets the message. I have not been able to try this out yet, because the waitformessageandclear command available on the DLS has an error in a .gs file. I've sent an email to the author, but have heard nothing back yet.

Have fun!

Wow this is discouraging to say the least......

Thanks Dick for your insight. From what you said AI drivers choose their own path from point A to point B unless trackmarks are used. Multiple AI drivers cause problems and backing up is difficult......

Wow this discouraging to say the least.....:eek:

My true vision is much larger than stated. I am developing my steel mill processes to need resupplyed every 8 hours with coal, iron ore, and limestone. The typical consist will be 12 to 20 cars. My senerios last a few days 72 to 96 hours. So AI drivers need to do multiple trips.

Maybe the answer is provide the loops.

Here is my idea. Portal on each end of west bound tracks and a portal on each end of the east bound tracks. A loaded train of coal departs east terminal goes to east yard track #3. Stops. Unhooks load. Preceeds to trackmark and waits 3 hours. I manually take a switcher grab the loaded coal cars and take the emptys and hook them to the waiting engine or make a grade and drift the emptys to the waiting engine. The empty coal train departs to east terminal portal gets loaded in portal and repeats the process coming from the west portal thereby making a loop.

Will this work???? Anyones thoughts.

PS I will be away until Sunday. I will reply to any responses then.

Last edited:
Hi, Matt - first, don't get discouraged! But, be prepared, as Harry advised, to do a lot of experimentation.

Backing up is not always a problem - don't know what influences that. It is just that I've learned to keep it to the minimum. And to try to keep other engines away from the backup area - can't always do that, though, and sometimes it works OK. Experiment, try it out, etc.

Also learned to try things out with the full consist I intend to use - In one instance that is the subject of another thread, I used a short consist just to try things out - got my scheme working OK, then went to a longer consist, and my pushing switch engine suddenly could not throw the 3rd switch ahead, so got a red signal, and would not move any further. Nobody had an explanation. I put the switcher up front, and the plan worked again - had to back the consist out of a siding, including setting a 3rd switch, but that worked OK.

I have not tried drifting cars into a coupling with another consist or engine. As stated in the earlier post, I have tried to manually push up to hook cars to the rear of a consist that has an AI engine at the front - that is where the AI engineer up front lost all his "orders". That is the situation in which stagecoach suggested that I needed to temporarily move the up front engineer to another engine and send a postmessage command out. But, as I explained, I haven't been able to test that method yet.

There are several threads on portals - I think the tunnel-like portal is the only one that is fully functional in that the others can be programmed to emit only one timed train during the running of a session. But, the timing in the tunnel portal is constant - every 30 minutes, say, if that is what you set it for. At varying time intervals, you can send a train into a portal, have it loaded/unloaded and then sent back out again after some constant time interval. I don't know about sending a train out one portal, having it loaded/unloaded, then coming in again at a different portal. Maybe in iportal mode you could do that. Otherwise, you would need to set some constant time interval at which loaded coal trains enter your map, dispose of all the coal, and send the empties out before the next loaded train is due - or have a method of dealing with any build-up or shortages of coal that might occur.

I started out to build a map, too. But then I set it aside and began to work with session building on existing maps as an aid to understanding how to build maps. I'm finding that the maps I've worked with don't have nearly the number of track marks and triggers that are needed if you want to do things like drop or pick up subconsists, have one train wait on a holding track until another passes, etc. More trackage is sometimes needed, too. One map I've worked with has interactive industries sitting right on the main or branch line. That does not work well if you want to use multiple trains.

One problem with building sessions on existing routes that require map changes is that you have to jump through a number of hoops if you want to share your session. So, I'm just going to use what I'm doing now as a learning exercise. But, I'm having fun while I'm doing it, and a lot of folks on this forum are sure giving me a good education.

I have not tried drifting cars into a coupling with another consist or engine. As stated in the earlier post, I have tried to manually push up to hook cars to the rear of a consist that has an AI engine at the front - that is where the AI engineer up front lost all his "orders". That is the situation in which stagecoach suggested that I needed to temporarily move the up front engineer to another engine and send a postmessage command out. But, as I explained, I haven't been able to test that method yet.

Hi Dick,
For this bit all you have to do is uncouple the front loco before the switcher couples to the rear of the consist. From when the main loco backs into the siding track give him commands to uncouple, drive to trackmarkX, (just far enough away so the consist does not recouple when the switcher hits,) wait for XX seconds, while the switcher uncouples then couple at trackmark track4 or where he uncoupled from.

I have used this method frequently,
Hope this helps,
Thanks, Bill69 - I'll give it a try. I imagine you have to be a little careful about speed when you uncouple.

I like your signature - I find that I'm usually doing research! But then, I'n a retired researcher, so that's my normal state - not knowing what I'm doing, I mean.

AI operations

Hi Dick,

Not so much speed, your speed limit in a yard should be low anyway, about 15 mph. at the most. However the timing of your operations are the critical factor. although I said waitforXX seconds You may need a waitfor XX mins. depending on how far the switcher has to move the wagons being coupled to your original consist.

So you like my sig. I am a retired engineer but I was thrown in the deep end of computing in the 1970s, so had to do lots and lots of "research" in a very short time.

Postmessage really works!

Well, Bill69, I'm researching your suggestion alright! I can do the job manually, but whatever I've tried in AI has just wrecked the switcher consist - can't uncouple while moving and reverse the switcher fast enough. It wants to run in under the car it has uncoupled from. I'm using uncouplez. I've also tried using Stop after uncouplez as well as giving the switcher a destination behind it.

Hmmm - I've just reread our exchange - you are talking about a drifting couple-up aren't you? I've successfully used the AI push-up after decoupling and moving the road engine, with its engineer still aboard - but where I've done that I've found that I had to move the road engine clear out of the signal block. That's why stagecoach suggested moving the engineer to another engine and using the message commands.

I finally did what I've been intending to do for several days (I invented procraste --- prcrasti --- well, I'll look it up tomorrow) and downloaded waitformessageandclear for 2004 (I'm running 2006, and the 2006 version is the one with the error). Postmessage and waitfor... worked fine, mostly.

Mostly? The engineer moves out of the stopped road engine, sends his message, which the switcher picks up and moves up to couple at trackmark (road engine with engineer still coupled up front). But uncouplez does not work until you move to another trackmark. Your switcher still has the train, including controlling the road engine. If you happen to move the train forward to another trackmark, uncouplez works OK, but it uncouples behind the road engine, and your switcher still has the consist. So, you have to use uncouplez-from and designate the car ahead of the switcher. Better not have more than 20 cars.

I had to move forward because a couple of turnouts were behind my switcher, and when I moved backwards towards a trackmark on the other side of them, one of the switches was thrown when the consist was moving over it, and everything derailed. The first time, backing to a trackmark worked OK, and uncouplez under that condition uncoupled the switcher.

Surveyor is fun, fun, fun.

Postmessage and waitformessageandclear impress me as being really useful commands. No more trying to guess how long it takes for a train to complete its task so that another can do whatever. Thanks, stagecoach - and special thanks to its author.

Last edited:
I stand corrected

Pushing or pulling the consist with a road engine on the front is not necessary to uncoupling a switch engine at the rear of the consist, as I claimed in the preceding post.

Under AI, regardless of the fact that the switch engine's driver is in control of the train and that the road engine doesn't even have a driver, the uncouplez command only uncouples the road engine. In fact, if it is a multi-unit engine, it only uncouples the 1st unit.

So, if you use a switch engine with an AI driver to couple cars to the rear of a consist that is headed by another engine, and you then want to decouple the switch engine, you have to use the "uncouplez from" command and specify the count-from-the-front-end of the car ahead of the switcher as the "from" value.

If you have more than 20 cars, then you have to uncouple the road engine and move it - along the lines of what Bill69 and I talked about a couple posts back.

Update - Use Change Train Direction command

Matt - do you feel your thread has been hijacked? Sorry, but I'm doing research in Bill69's sense of the word - don't know what I'm doing, but running around any old way!

While working on coupling a subconsist to the rear of a train, I realized that the switch engine, when I pushed or pulled on the whole train, was doing what a helper engine would do. But the uncouplez problem seemed awkward. So, I started another thread called Helper Engines - partly becase I was straying from your thread subject, and partly because the only halfway decent way to find things in the forum is to search on titles only. It's at .

Stagecoach suggested trying the change train direction command - and it worked very well, not only when hooking up a helper (pusher), but when adding a subconsist and then uncoupling the switcher.

Since we've gone through several iterations of how, under AI control, to add cars to an existing consist with a road engine up front, let me summarize:

1. With the train stopped, the road engineer moves to an engine that is off to the side.
2. He sends a postmessage command (for the switcher engineer), then waits there until he receives a waitformessageandclear command from the switcher engineer.
3. The switcher pushes the cars up to the rear of the stopped train and couples up using the couple at trackmark command
4. A change train direction command makes the switcher the new head engine, and now it can decouple with uncouplez (or, if he wanted to keep any cars to take to another train in the yard, he could use the uncouplez from command).
5. After uncouplez, he sends a postmessage command and moves in reverse toward another destination.
6. The other engineer picks up the message, returns to the engine at the front end, and proceeds. He does not change direction again.

4, 5, and 6 happen very quickly -- the freight train moves on out, and the switcher is free to go about its business, more or less instantaneously.

The message commands are really useful - I've been going into some of my other sessions and replacing the wait for (x minutes) and wait for trigger commands with the message commands. With these commands, you don't have to think about whether a train follows the exact route that will cause it to trip a trigger that you are waiting for.

Thanks to Stagecoach for providing the keys to the above-described method.

The author of the two message commands kindly sent me a fix for the 2006 version, but since the 2004 version works just fine, there is no need to give the fix here.

Sorry to have turned your thread into a research experiment!