Controlling trains by script using dummy?

Place to get help with not working mods / modding interface.
Post Reply
Melfish
Inserter
Inserter
Posts: 42
Joined: Mon Nov 17, 2014 1:20 pm
Contact:

Controlling trains by script using dummy?

Post by Melfish »

Hey guys, I am currently in the process of making a mod in which I need to control trains by script instead of a schedule.
The API does mention it is possible to control a train by script, and using the player I can use its riding_state to make it work. (Even though it is marked as read-only, but I could not find any other way and tried it anyway.)

To not have to use the player, I have to use some kind of in-game dummy player, but is this even possible?

User avatar
Sirenfal
Long Handed Inserter
Long Handed Inserter
Posts: 50
Joined: Tue Jan 10, 2017 1:42 am
Contact:

Re: Controlling trains by script using dummy?

Post by Sirenfal »

Use the LuaTrain object, not the entity. This can be accessed by the train property on any of the entities composing the train (cargo wagons or locomotives).

http://lua-api.factorio.com/latest/LuaE ... tity.train

http://lua-api.factorio.com/latest/LuaTrain.html

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Controlling trains by script using dummy?

Post by Adil »

You don't have to use dummy characters, `riding_state` is available in the vehicle itself.

And yes, it is possible to define a dummy entity of type "character" to use for your own needs.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

Melfish
Inserter
Inserter
Posts: 42
Joined: Mon Nov 17, 2014 1:20 pm
Contact:

Re: Controlling trains by script using dummy?

Post by Melfish »

The LuaTrain does not seem to have the riding_state, as I get an error that it does not contain the key... (adding it doesn't work)
I have also tried to use it on the train entity (just to try everything) , nothing happens. Trying to read the state gives an error that it can only be used on player and car entities.

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Controlling trains by script using dummy?

Post by Adil »

Melfish wrote:The LuaTrain does not seem to have the riding_state, as I get an error that it does not contain the key... (adding it doesn't work)
I have also tried to use it on the train entity (just to try everything) , nothing happens. Trying to read the state gives an error that it can only be used on player and car entities.
Oww. I guess it means that it can only be used on cars and players then :oops: . I've never tried to control trains.
However, "player" is actually a type of entities that usually represents player's character. (And there also happens to be an entity of type "player" named "player")
So you could try to do something like:

Code: Select all

driver = locomotive.surface.nauvis.create_entity{name='player',position={0,0},force=locomotive.force}
locomotive.passenger=driver
driver.riding_state=...---and so on
Maybe this will even work.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Controlling trains by script using dummy?

Post by Nexela »

You can take a peek at the current version of the FARL mod to see how this is handled. My guess is very similar to the Adil's example

Post Reply

Return to “Modding help”