Page 1 of 1

Allow an item to be build depending on its position

Posted: Sat Aug 13, 2016 3:02 pm
by Hermios
Hi
So, This is my very first mod in Factorio, so my question may be simple.
How can I allow - or not - an entity to be buiid, related to its position. Typically, I want my entity to be build only if i is near a rail (exactly like for the train stop)?
Thanks

Niko

Re: Allow an item to be build depending on its position

Posted: Sun Aug 14, 2016 9:39 am
by daniel34
Generally I would listen to the on_built_entity event for your entity and if it is in a position where you don't want the player to build it then you remove it and give the player the item back. I'm not sure though how that would work with construction robots and ghost entities.

In your specific case where you only want to allow the exact same positions as train stops (or signals) you could make a dummy entity which is actually a train stop. The player will only be able to build it next to rails and after he placed it you can remove that entity and place your actual entity instead. The player will also get the possible position markers next to the rails.

Re: Allow an item to be build depending on its position

Posted: Sun Aug 14, 2016 10:01 am
by Nexela
daniel34 wrote:Generally I would listen to the on_built_entity event for your entity and if it is in a position where you don't want the player to build it then you remove it and give the player the item back. I'm not sure though how that would work with construction robots and ghost entities.
You have to make sure you account for it by not giving the item back if entity-name = entity-ghost
you also might have to check for entity.ghost_name to when placing

In your specific case where you only want to allow the exact same positions as train stops (or signals) you could make a dummy entity which is actually a train stop. The player will only be able to build it next to rails and after he placed it you can remove that entity and place your actual entity instead. The player will also get the possible position markers next to the rails.
This is nice to know. I didn't even think about it when I needed something like this.


edit:fixed qoutebooboo

Re: Allow an item to be build depending on its position

Posted: Sun Aug 14, 2016 10:28 am
by Hermios
Hey
Thanks for the answer.
I understand the idea, however, I tried to put a "Place_result" for my train-stop. This place_result would be my final item. Unfortunately, it doesn't work.It says that my train-stop required a string order.
If I set the place result as train-stop, it works, so the problem comes definitely from it.
Any idea

Re: Allow an item to be build depending on its position

Posted: Sun Aug 14, 2016 11:55 am
by Nexela
You need to place the dummy entity then destroy it and create your "real" entity

entity/item = my-fake-trainstop

entity2 = what-I-Really-want


on_built_entity
if created_entity.name = "my-fake=trainstop" then
local pos, surface, force = created_entity.position, created_entity.surface, created_entity.force
created_entity.destroy()
surface.create_entity(name="what-I-really-want", fill in the rest here)

Re: Allow an item to be build depending on its position

Posted: Sun Aug 14, 2016 12:21 pm
by Hermios
It works. Awsome!
Many thanks