on_entity_replace

Place to get help with not working mods / modding interface.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

on_entity_replace

Post by darkfrei »

Hi all!

Is it possible to make on_entity_replace easy?

Code: Select all

script.on_event(defines.events.on_preplayer_mined_item, function (event)
	event_if_replace (event, "first")
end)

script.on_event(defines.events.on_built_entity, function (event)
	event_if_replace (event, "second")
end)

function event_if_replace (event, tstring)
	local player_index = event.player_index
	local entity = event.entity or event.created_entity
	try_init ()
	local tick = game.tick
	if tstring == "first" then
		global.event_replace[tick] = {}
		global.event_replace_valid = true
		global.event_replace[tick].first = {}
		global.event_replace[tick].first.name = entity.name
		global.event_replace[tick].first.type = entity.type
		global.event_replace[tick].first.position = entity.position
		global.event_replace[tick].first.player_index = player_index
	elseif ((tstring == "second") and 
			(global.event_replace_valid) and 
			(global.event_replace[tick])) then
		local first_position = global.event_replace[tick].first.position
		local second_position = entity.position
		if comparePositions (first_position, second_position) then
			local first = global.event_replace[tick].first
			local second = {}
				second.name = entity.name
				second.type = entity.type
				second.position = entity.position
				second.player_index = player_index
				handler_event_on_replace (first, second)
				global.event_replace_valid = false
		end
	elseif global.event_replace[tick] then
		-- do nothing
	else 
		global.event_replace = nil
	end
end

----------------------------- EVENT ON REPLACE --------------------
function handler_event_on_replace (first, second)
	printAll("it was replacing!")
end

function try_init ()
	if not (global) then global = {} end
	if not (global.event_replace) then global.event_replace = {} end
end

-- -- other stuff -- --
function comparePositions (pos1, pos2)
	if not (pos1 and pos2) then return false end
	if not ((type (pos1) == "table") and (type (pos2) == "table")) then return false end
	if ((pos1.x == pos2.x) and (pos1.y == pos2.y)) then return true end
	printAll("not same position, how?")
	return false
end

function printAll(text)
	for player_index, player in pairs (game.players) do
		game.players[player_index].print (text)
	end
end

321freddy
Fast Inserter
Fast Inserter
Posts: 125
Joined: Fri Sep 23, 2016 10:16 pm
Contact:

Re: on_entity_replace

Post by 321freddy »

  • Have the global table contain some kind of cache for every player.
  • In on_preplayer_mined_item store the event tick in the cache of the player who did the mining.
  • Then in on_built_entity if the tick equals the saved tick in the cache of the player a fast replace was done.
A single player can't pick up and place a building in the same tick (except for fast replace because it happens on exactly the same tick).
I used a similar trick to detemine if a player did a stack transfer in my even distribution mod.

Here some pseudo code:

Code: Select all

script.on_event(defines.events.on_preplayer_mined_item, function (event)
   local cache = global.cache[event.player_index] -- init if needed
   cache.tick = event.tick
   cache.mined = event.entity.name
end)

script.on_event(defines.events.on_built_entity, function (event)
   local cache = global.cache[event.player_index]
   if cache.tick == event.tick then
      game.print("replaced "..cache.mined.." with "..event.created_entity.name)
   end
end)
Untested but should work. ;)
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: on_entity_replace

Post by darkfrei »

321freddy wrote:Untested but should work. ;)
Thanks! And I need to compare wat was built and new entity.
Post Reply

Return to “Modding help”