AIStationList stations seems to fail

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
Green-Devil
Engineer
Engineer
Posts: 18
Joined: 10 Jan 2007 19:09
Location: Denmark

AIStationList stations seems to fail

Post by Green-Devil »

Toying around with NoAI - but got some issues retrieving the list of stations.
I fire up the AI, and then team up with it. Then I proceed to build a couple of railroad stations in several towns.

Start function:

Code: Select all

function Start()
{
    local sl = AIStationList(AIStation.STATION_TRAIN);
                
    foreach (station in sl) {
        AILog.Info("Station near " + AITown.GetName(AIStation.GetNearestTown(station)));
    }
}
For each station, a line is printed, but only the first town name is used. For instance: If I build a station in Kiel, Hamburg and Berlin, the output is then:

Code: Select all

Station near Kiel
Station near Kiel
Station near Kiel
Same happens if I use GetLocation on the Station - only the location of the first station is printed.

Is it even possible to do what I attempt to do? Or does the stationList only return stations built by the AI?

edit:
Minor mistake in that one: Either, game is paused until the stations have been built, or a while(true) loop is around the content of the entire Start() function :)
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: AIStationList stations seems to fail

Post by Yexo »

Green-Devil wrote:

Code: Select all

    local sl = AIStationList(AIStation.STATION_TRAIN);
                
    foreach (station in sl) {
Every AIList contains key=>value pairs. What you do above is loop through all values, while you actually want the keys. That it prints any name at all is pure luck.

Change the foreach line to this to fix your problem:

Code: Select all

foreach (station,_ in sl) {
Green-Devil
Engineer
Engineer
Posts: 18
Joined: 10 Jan 2007 19:09
Location: Denmark

Re: AIStationList stations seems to fail

Post by Green-Devil »

Silly me :oops:

Well - thanks a bunch. That did the trick :)
User avatar
Kev!
Engineer
Engineer
Posts: 37
Joined: 21 Jun 2011 16:30
Location: In Da House!
Contact:

Re: AIStationList stations seems to fail

Post by Kev! »

Howdy,

I'm new but I would have thought...


sl.Valuate(AIStation.GetNearestTown);

foreach (station, town in sl) {

Kev!
I DO like a nice caboose...
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: AIStationList stations seems to fail

Post by Yexo »

Yes, that works too.
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 9 guests