Page 1 of 1

on_player_pre_built

Posted: Wed May 24, 2017 4:57 am
by sparr
I want an event that triggers before the player builds something. Specifically I would use this to find the underground belt that's about to connect to the one being placed, and get its current neighbour.

Re: on_player_pre_built

Posted: Wed May 24, 2017 5:09 am
by Rseding91
What would you imagine in such an event?

The way Factorio works either an entity was built successfully or it wasn't built at all so there's no real middle ground. If such an event existed it would contain a lot of false positives when building failed.

Re: on_player_pre_built

Posted: Wed May 24, 2017 5:16 am
by sparr
It would be like the pre_mined event, used for getting info about the game state before the entity disappears. In this case you would get to check the game state before the item is built.

Re: on_player_pre_built

Posted: Wed May 24, 2017 6:05 am
by Rseding91
The "on_put_item" event is exactly that. http://lua-api.factorio.com/latest/even ... n_put_item

Re: on_player_pre_built

Posted: Wed May 24, 2017 6:22 am
by SilverB1rd
a pre build event with the option to cancel the placement would be extremely useful for custom scenarios where you want to restrict where the player can build.

I'm currently do this with

Code: Select all

script.on_event(defines.events.on_built_entity, function(event)
  local player = game.players[event.player_index]
  local created = event.created_entity
  local pos = created.position

  if pos.x > -32 and pos.x < 32 or pos.y >-32 and pos.y <32 then
    if not player.cursor_stack.valid_for_read then
      player.cursor_stack.set_stack{name = created.name, count = 1}
    elseif player.cursor_stack.name == created.name then
      player.cursor_stack.count = player.cursor_stack.count + 1
    end
    created.destroy()
  end

end)
But this creates the entity then removes it right away which is kinda spammy if the player is click dragging.

if this was possible with something like this, it would be much cleaner in code.

Code: Select all

script.on_event(defines.events.on_pre_built_entity, function(event)
  local pos =  event.created_entity.position

  if pos.x > -32 and pos.x < 32 or pos.y >-32 and pos.y <32 then
    event.allowed = false --[[ or some variation here, like .cancel = true ]]--
  end

end)
where it would be the same as if the building location was invalid from another entity already.

a good usage example would be in something like wave defense where the player is not allowed to build in the bitter spawning area.

Re: on_player_pre_built

Posted: Wed May 24, 2017 5:00 pm
by sparr
Rseding91 wrote:The "on_put_item" event is exactly that. http://lua-api.factorio.com/latest/even ... n_put_item
excellent!