Allow an item to be build depending on its position

Place to get help with not working mods / modding interface.
Hermios
Long Handed Inserter
Long Handed Inserter
Posts: 80
Joined: Sat Aug 13, 2016 2:57 pm
Contact:

Allow an item to be build depending on its position

Post 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
daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

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

Post 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.
quick links: log file | graphical issues | wiki
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

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

Post 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
Last edited by Nexela on Sun Aug 14, 2016 11:48 am, edited 2 times in total.
Hermios
Long Handed Inserter
Long Handed Inserter
Posts: 80
Joined: Sat Aug 13, 2016 2:57 pm
Contact:

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

Post 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
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

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

Post 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)
Hermios
Long Handed Inserter
Long Handed Inserter
Posts: 80
Joined: Sat Aug 13, 2016 2:57 pm
Contact:

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

Post by Hermios »

It works. Awsome!
Many thanks
Post Reply

Return to “Modding help”