Train spawning

Place to get help with not working mods / modding interface.
Post Reply
Rednax
Burner Inserter
Burner Inserter
Posts: 10
Joined: Sat Jul 16, 2016 1:11 pm
Contact:

Train spawning

Post 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.

Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: Train spawning

Post 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:

Rednax
Burner Inserter
Burner Inserter
Posts: 10
Joined: Sat Jul 16, 2016 1:11 pm
Contact:

Re: Train spawning

Post 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.

User avatar
Arch666Angel
Smart Inserter
Smart Inserter
Posts: 1636
Joined: Sun Oct 18, 2015 11:52 am
Contact:

Re: Train spawning

Post 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.

Rednax
Burner Inserter
Burner Inserter
Posts: 10
Joined: Sat Jul 16, 2016 1:11 pm
Contact:

Re: Train spawning

Post 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.

User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 253
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Re: Train spawning

Post 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)

Post Reply

Return to “Modding help”