Page 1 of 1

[1.1.33] Rotated locomotive returns wrong direction

Posted: Sat May 15, 2021 3:24 pm
by ickputzdirwech
I am a bit hesitant to post this here since I fear I just did something wrong but I am 99% certain this is not my fault. Anyway I wrote a mod that stops a train if one of its rolling stock gets destroyed and creates a ghost to replace it.

Code: Select all

script.on_event(defines.events.on_entity_died, function(event)
    	local entity = event.entity
	local train = entity.train
	-- stops the train
   	train.speed = 0
	-- creates ghost entity
    	game.surfaces[entity.surface.name].create_entity{
        	name = "entity-ghost",
       		inner_name = entity.name,
       		position = entity.position,
		direction = entity.direction,
        	force = entity.force,
        	create_build_effect_smoke = false,
        	 }
end, {{filter = "rolling-stock"}})
The issue I am having is that the created locomotive ghosts are the wrong way around if the destroyed locomotives got rotated before they got built.

Reproduction steps
  • Paste the code above into control.lua
  • Start a game and build some rails
  • Take a locomotive item
  • Press R
  • Build the locomotive
  • Shoot the locomotive
  • Observe that the created ghost is the wrong way around
If you rotate the locomotive after you build it you get the same issue.
If you do it without pressing R the locomotive has the right direction.

Re: [1.1.33] Rotated locomotive returns wrong direction

Posted: Sat May 15, 2021 5:57 pm
by DaveMcW

Re: [1.1.33] Rotated locomotive returns wrong direction

Posted: Sun May 16, 2021 6:41 am
by boskid
Thanks for the report however i am moving this to Not a bug because your code is broken.

Your assumption is that the LuaEntity::direction will return direction of a rolling stock, but rolling stocks do not use directions so it always returns North. Please use LuaEntity::orientation and do create_entity{orientation=...} instead.

Re: [1.1.33] Rotated locomotive returns wrong direction

Posted: Sun May 16, 2021 8:05 am
by ickputzdirwech
Thanks for your answer. That fixed it! :D :oops: In my defense: it's not documented in https://lua-api.factorio.com/latest/Lua ... ate_entity. I'll post it in documentation improvements.