Page 1 of 1

Train spawning

Posted: Sun Jul 17, 2016 11:52 am
by Rednax
Hello everyone,

I'm trying to destroy a train and spawn it somewhere else in space, time and even save file.

So far I have run into three problems I have yet to solve:
1. How do I get the direction of locomotives (conveniently)? And how do I set this direction when creating the locomotive entities? A binary direction suffices, since I can do the rest by checking the direction of the station that the train is at.
It seems the 'direction' field of the locomotive entities is always north. To obtain the direction I could do some magic involving the direction of the station or rail, and whether locomotive is a front or rear carriage. This would be a little wacky since I would need to find the station based on the location of the train and the name of the station, as the schedule does not include a reference to the station, only a name. But this does not solve the question on how to spawn the locomotives in different directions, since my tests also show that the 'direction' field has no effect on spawning the locomotive.

2. How do I get the name of the locomotive? The 'name' field is nil, even though the locomotive has the name of a backer. I can get things to work without storing the name, but it would be nice to to also copy the name.

3. Is there a way to access the remaining fuel level of a locomotive? It is possible to get the efficiency bonus percentage, but I did not see where to access the remaining fuel level. Again, this is just a nice-to-have as the player should be able to live with some fuel losses.

Re: Train spawning

Posted: Sun Jul 17, 2016 12:12 pm
by Choumiko
Rednax wrote: 1. How do I get the direction of locomotives (conveniently)? And how do I set this direction when creating the locomotive entities? A binary direction suffices, since I can do the rest by checking the direction of the station that the train is at.
It seems the 'direction' field of the locomotive entities is always north.
try

Code: Select all

local direction = round(locomotive.orientation * 8)
round is an actual rounding function, not math.ceil/floor (ceil/floor might work if you don't care about locomotives on curves)
2. How do I get the name of the locomotive? The 'name' field is nil, even though the locomotive has the name of a backer. I can get things to work without storing the name, but it would be nice to to also copy the name.
use locomotive.backer_name
3. Is there a way to access the remaining fuel level of a locomotive? It is possible to get the efficiency bonus percentage, but I did not see where to access the remaining fuel level. Again, this is just a nice-to-have as the player should be able to live with some fuel losses.
The fuel in the buffer? I don't think that's poosible

Aaaaaand, since when does creating locos/wagons work? Last time i tried it didn't (might have been 0.12) :shock:

Re: Train spawning

Posted: Sun Jul 17, 2016 2:10 pm
by Rednax
Many thanks!

I totally overlooked the orientation and did not consider the backer_name.

The documentation does not mention that orientation is also available for create_entity. Will have to check if that works too.

Re: Train spawning

Posted: Sun Jul 17, 2016 2:14 pm
by Arch666Angel
Choumiko wrote:
Rednax wrote: 1. How do I get the direction of locomotives (conveniently)? And how do I set this direction when creating the locomotive entities? A binary direction suffices, since I can do the rest by checking the direction of the station that the train is at.
It seems the 'direction' field of the locomotive entities is always north.
try

Code: Select all

local direction = round(locomotive.orientation * 8)
round is an actual rounding function, not math.ceil/floor (ceil/floor might work if you don't care about locomotives on curves)
2. How do I get the name of the locomotive? The 'name' field is nil, even though the locomotive has the name of a backer. I can get things to work without storing the name, but it would be nice to to also copy the name.
use locomotive.backer_name
3. Is there a way to access the remaining fuel level of a locomotive? It is possible to get the efficiency bonus percentage, but I did not see where to access the remaining fuel level. Again, this is just a nice-to-have as the player should be able to live with some fuel losses.
The fuel in the buffer? I don't think that's poosible

Aaaaaand, since when does creating locos/wagons work? Last time i tried it didn't (might have been 0.12) :shock:
If I remember correctly there was a mod which put trains and wagons on track automatically.

Re: Train spawning

Posted: Mon Jul 18, 2016 8:55 am
by Rednax
Arch666Angel wrote:
Choumiko wrote:
If I remember correctly there was a mod which put trains and wagons on track automatically.
The carriages already snap to the rail, even if your location is slightly off, similar to how the placement works for the player. Carriages also auto-connect when placed by a lua script in the same way they do for the player. Not sure what this mod would add to this.

Re: Train spawning

Posted: Mon Jul 18, 2016 1:57 pm
by DedlySpyder
Rednax wrote:
Arch666Angel wrote:
Choumiko wrote:
If I remember correctly there was a mod which put trains and wagons on track automatically.
The carriages already snap to the rail, even if your location is slightly off, similar to how the placement works for the player. Carriages also auto-connect when placed by a lua script in the same way they do for the player. Not sure what this mod would add to this.
I don't know if there was another, but I made a mod that could place entities in general, so it could place locos if one wanted to (I originally made it to compliment another mod of mine, but just incorporated it basically)