What is surface.find_entities centered around?

Place to get help with not working mods / modding interface.
Post Reply
memcallen
Inserter
Inserter
Posts: 34
Joined: Sat Mar 28, 2015 1:22 am
Contact:

What is surface.find_entities centered around?

Post by memcallen »

What does find_entities get centered around? I want to make an entity that can detect when the player walks into it.

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5156
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: What is surface.find_entities centered around?

Post by Klonan »

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:

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



memcallen
Inserter
Inserter
Posts: 34
Joined: Sat Mar 28, 2015 1:22 am
Contact:

Re: What is surface.find_entities centered around?

Post by memcallen »

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').

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
How can I prevent it from redetecting my entity?

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3705
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: What is surface.find_entities centered around?

Post by DaveMcW »

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

memcallen
Inserter
Inserter
Posts: 34
Joined: Sat Mar 28, 2015 1:22 am
Contact:

Re: What is surface.find_entities centered around?

Post by memcallen »

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.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3705
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: What is surface.find_entities centered around?

Post by DaveMcW »

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.

memcallen
Inserter
Inserter
Posts: 34
Joined: Sat Mar 28, 2015 1:22 am
Contact:

Re: What is surface.find_entities centered around?

Post by memcallen »

I got it to work, if anyone needs help:

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)
Although I haven't made it work with multiplayer yet.

Post Reply

Return to “Modding help”