What is surface.find_entities centered around?
What is surface.find_entities centered around?
What does find_entities get centered around? I want to make an entity that can detect when the player walks into it.
Re: What is surface.find_entities centered around?
find entities is defined by an area of co-ordinates, you can set that to anything...
if you're doing it around a entity, something like this is what you'll want:
if you're doing it around a entity, something like this is what you'll want:
Code: Select all
function MinedEntity(event)
if event.entity.name == "concrete-lamppost" then
local b = event.entity
local X = b.position.x
local Y = b.position.y
lamp = b.surface.find_entities_filtered{area = {{X -0.5, Y - 0.5 }, {X + 0.5 , Y +0.5 }}, name= "concrete-lamp"}
if lamp[1] ~= nil then
lamp[1].destroy() end
end
end
in this example, when the entity is mined, it finds the other entity nearby and destroys it. adapt this to suit your need
Re: What is surface.find_entities centered around?
Ok, thanks.
EDIT:
I'm checking for my entity in the on_tick event, and it detects everything fine, but it doesn't stop detecting it (As in it doesn't stop printing 'Found Cave Entrance').
How can I prevent it from redetecting my entity?
EDIT:
I'm checking for my entity in the on_tick event, and it detects everything fine, but it doesn't stop detecting it (As in it doesn't stop printing 'Found Cave Entrance').
Code: Select all
local X = game.player.position.x
local Y = game.player.position.y
local entrance = game.surfaces["nauvis"].find_entities_filtered{area = {{X,Y},{X+1,Y+1}}, name= "Cave-Entrance"}
if(entrance == nil) then return else
game.player.print("Found Cave Entrance!")
entrance = nil
end
Re: What is surface.find_entities centered around?
Code: Select all
local position_string = entrance[1].position.x .. "," .. entrance[1].position.y
if (not global.caves) then
-- https://forums.factorio.com/wiki/index.php?title=Lua/Data_Lifecycle#global
global.caves = {}
end
if (not global.caves[position_string]) then
game.player.print("Found Cave Entrance!")
global.caves[position_string] = true
end
Re: What is surface.find_entities centered around?
I need to always detect the cave entrance, as it needs to be reusable, and act as a teleporter of sorts, rather than detecting it once.
Re: What is surface.find_entities centered around?
Try storing game.tick instead of true, and only reacting if the tick is too long ago.
Or empty the caves array in on_tick if the player walks too far away.
Or empty the caves array in on_tick if the player walks too far away.
Re: What is surface.find_entities centered around?
I got it to work, if anyone needs help:
Although I haven't made it work with multiplayer yet.
Code: Select all
script.on_event(defines.events.on_tick, function(event)
local X = game.player.position.x
local Y = game.player.position.y
local entrance = game.surfaces["nauvis"].find_entities_filtered{area = {{X,Y},{X,Y}}, name= "Cave-Entrance"}
if (global.caves == nil) then
global.caves = {}
end
if(table.empty(entrance))then
return
end
local position_string = entrance[1].position.x .. "," .. entrance[1].position.y
local diff = 0
if(global.caves[position_string] ~= nil) then
diff = tonumber(event.tick)-tonumber(global.caves[position_string])
else
global.caves[position_string] = event.tick
return
end
--game.player.print(tostring(diff))
if(diff) then
if (diff>50) then
game.player.print("Found Cave Entrance!")
global.caves[position_string] = event.tick
game.player.teleport(game.player.position, game.surfaces.Caves)
end
end
end)