Place entity behind ore when it is generated
Posted: Fri Apr 01, 2016 6:28 pm
I am trying to make a new ore glow by adding an invisible rail signal behind it when the game generates it, but i am having difficulty with the Lua script to spawn these in. It has been ages since I did any Lua coding, and at some point all the event have been totally overhauled and I no longer have any idea how it all works
The original code I wanted to use was taken from this thread viewtopic.php?f=25&t=13164:
I have modified this to :
This works more than the original does, but lines 8 and 16 do not work, I don't know what entity.name should be replaced with
Any help will be much appreciated!
The original code I wanted to use was taken from this thread viewtopic.php?f=25&t=13164:
Code: Select all
-- Make sure our functions get called for specific events.
-- A robot built one.
game.onevent(defines.events.onrobotbuiltentity, function(event) OnBuilt(event.createdentity) end)
-- A player built one.
game.onevent(defines.events.onbuiltentity, function(event) OnBuilt(event.createdentity) end)
-- A player removed one.
game.onevent(defines.events.onpreplayermineditem, function(event) OnRemoved(event.entity) end)
-- A robot removed one.
game.onevent(defines.events.onrobotpremined, function(event) OnRemoved(event.entity) end)
-- It died.
game.onevent(defines.events.onentitydied, function(event) OnRemoved(event.entity) end)
function OnBuilt(entity)
-- We've just built the pole.
if entity.name == "small-electric-pole-with-lights" then
-- Spawn a light source at its position.
game.createentity{name = "pole-light", position = entity.position, force = entity.force}
end
end
function OnRemoved(entity)
-- The pole died, remove the "pole-light".
if entity.name == "small-electric-pole-with-lights" then
-- Search the area around the pole for the light.
res = game.findentitiesfiltered{name="pole-light", area=GetArea(entity.position, 0.5)}
if #res then
-- If we've found it, destroy it.
res[1].destroy()
end
end
-- The "pole-light" died, remove the pole.
if entity.name == "pole-light" then
res = game.findentitiesfiltered{name="small-electric-pole-with-lights", area=GetArea(entity.position, 0.5)}
if #res then
res[1].destroy()
end
end
end
function GetArea(pos, radius)
-- This calculates a box of the given radius around the given position.
return {{x = pos.x - radius, y = pos.y - radius}, {x = pos.x + radius, y = pos.y + radius}}
end
Code: Select all
script.on_event(defines.events.on_chunk_generated, function(event) OnBuilt(event.createdentity) end)
-- A player mines the ore out.
script.on_event(defines.events.on_resource_depleted, function(event) OnRemoved(event.entity) end)
function OnBuilt(entity)
-- When ore spawned
if entity.name == "glow-ore" then
-- Spawn a light source at its position.
game.createentity{name = "ore-light", position = entity.position, force = entity.force}
end
end
function OnRemoved(entity)
-- The ore is mined out, remove the "ore-light".
if entity.name == "glow-ore" then
-- Search the area around the ore for the light.
res = game.findentitiesfiltered{name="ore-light", area=GetArea(entity.position, 0.5)}
if #res then
-- If we've found it, destroy it.
res[1].destroy()
end
end
end
function GetArea(pos, radius)
-- This calculates a box of the given radius around the given position.
return {{x = pos.x - radius, y = pos.y - radius}, {x = pos.x + radius, y = pos.y + radius}}
end
Any help will be much appreciated!