on_player_pre_built

Things that already exist in the current mod API
Post Reply
sparr
Smart Inserter
Smart Inserter
Posts: 1329
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

on_player_pre_built

Post 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.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13219
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: on_player_pre_built

Post 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.
If you want to get ahold of me I'm almost always on Discord.

sparr
Smart Inserter
Smart Inserter
Posts: 1329
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: on_player_pre_built

Post 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.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13219
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: on_player_pre_built

Post by Rseding91 »

The "on_put_item" event is exactly that. http://lua-api.factorio.com/latest/even ... n_put_item
If you want to get ahold of me I'm almost always on Discord.

SilverB1rd
Inserter
Inserter
Posts: 47
Joined: Fri Mar 17, 2017 9:19 pm
Contact:

Re: on_player_pre_built

Post 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.

sparr
Smart Inserter
Smart Inserter
Posts: 1329
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: on_player_pre_built

Post by sparr »

Rseding91 wrote:The "on_put_item" event is exactly that. http://lua-api.factorio.com/latest/even ... n_put_item
excellent!

Post Reply

Return to “Already exists”