Page 1 of 1

Trying to add lights to poles, is it possible?

Posted: Tue Jun 23, 2015 9:13 am
by gswindle
Hello all. This is my firs mod so i might be pretty far out of my element. I am trying to add lights to poles and the the biggest problem is there is no light. I load the mod successfully however when i place the pole it does nothing at night.... does anyone have any suggestions.. here is my entity code.

Code: Select all

data:extend(
{
	{
	    type = "electric-pole",
	    name = "small-electric-pole-with-lights",
	    icon = "__LightPole__/graphics/icons/small-electric-pole-with-light.png",
	    flags = {"placeable-neutral", "player-creation"},
	    minable = {hardness = 0.2, mining_time = 0.5, result = "small-electric-pole-with-lights"},
	    max_health = 35,
	    corpse = "small-remnants",
	    collision_box = {{-0.15, -0.15}, {0.15, 0.15}},
	    selection_box = {{-0.4, -0.4}, {0.4, 0.4}},
	    drawing_box = {{-0.5, -2.3}, {0.5, 0.5}},
	    maximum_wire_distance = 7.5,
	    supply_area_distance = 2.5,
	    { 
	       type ="lamp",
	         energy_source =
             {
               type = "electric",
               usage_priority = "secondary-input"
             },
	       energy_usage_per_tick = "1KW",
	       light = {intensity = 0.9, size = 40},
	    },
	    pictures =
	    {
	      filename = "__LightPole__/graphics/small-electric-pole/small-electric-pole.png",
	      priority = "extra-high",
	      width = 123,
	      height = 124,
	      axially_symetric = false,
	      direction_count = 4,
	      shift = {1.4, -1.1}
	    },
	    connection_points =
	    {
	      {
	        shadow =
	        {
	          copper = {2.7, 0},
	          red = {2.3, 0},
	          green = {3.1, 0}
	        },
	        wire =
	        {
	          copper = {0, -2.7},
	          red = {-0.4,-2.7},
	          green = {0.4,-2.7}
	        }
	      },
	      {
	        shadow =
	        {
	          copper = {2.7, -0.05},
	          red = {2.2, -0.35},
	          green = {3, 0.12}
	        },
	        wire =
	        {
	          copper = {-0.04, -2.8},
	          red = {-0.3, -2.9},
	          green = {0.2, -2.6}
	        }
	      },
	      {
	        shadow =
	        {
	          copper = {2.5, -0.1},
	          red = {2.55, -0.45},
	          green = {2.5, 0.25}
	        },
	        wire =
	        {
	          copper = {-0.2, -2.7},
	          red = {-0.05, -2.95},
	          green = {0, -2.4}
	        }
	      },
	      {
	        shadow =
	        {
	          copper = {2.30, -0.1},
	          red = {2.65, -0.40},
	          green = {1.75, 0.20}
	        },
	        wire =
	        {
	          copper = {0, -2.7},
	          red = {0.3, -2.85},
	          green = {-0.3, -2.5}
	        }
	      }
	    },
	copper_wire_picture =
    {
      filename = "__base__/graphics/entity/small-electric-pole/copper-wire.png",
      priority = "extra-high-no-scale",
      width = 224,
      height = 46
    },
    green_wire_picture =
    {
      filename = "__base__/graphics/entity/small-electric-pole/green-wire.png",
      priority = "extra-high-no-scale",
      width = 224,
      height = 46
    },
    radius_visualisation_picture =
    {
      filename = "__base__/graphics/entity/small-electric-pole/electric-pole-radius-visualization.png",
      width = 12,
      height = 12
    },
    red_wire_picture =
    {
      filename = "__base__/graphics/entity/small-electric-pole/red-wire.png",
      priority = "extra-high-no-scale",
      width = 224,
      height = 46
    },
    wire_shadow_picture =
    {
      filename = "__base__/graphics/entity/small-electric-pole/wire-shadow.png",
      priority = "extra-high-no-scale",
      width = 224,
      height = 46
    }
  },

})
thanks in advance!

Re: Trying to add lights to poles, is it possible?

Posted: Tue Jun 23, 2015 9:30 am
by ThaPear
Entity prototypes are quite rigid, certain types of prototypes accept only certain parameters. Only the light, player, car and train entities can emit light continuously.
I'm afraid you'll have to spawn a separate light entity at each pole. This can be easily done using control.lua:

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
You'll have to create the entity pole-light using the small-lamp prototype (demo-entities.lua).
You should add this line to it:

Code: Select all

selectable_in_game = false,
To make it invisible to the player.

Re: Trying to add lights to poles, is it possible?

Posted: Thu Jun 25, 2015 1:48 pm
by gswindle
I am now receiving the following error whenever I click generate.

Code: Select all

__LightPole__/control.lua:3: attempt to index global 'defines' (a nil value)
I have added a temporary light entity

Code: Select all

data:extend(
{
	{
	    type = "electric-pole",
	    name = "small-electric-pole-with-lights",
	    icon = "__LightPole__/graphics/icons/small-electric-pole-with-light.png",
	    flags = {"placeable-neutral", "player-creation"},
	    minable = {hardness = 0.2, mining_time = 0.5, result = "small-electric-pole-with-lights"},
	    max_health = 35,
	    corpse = "small-remnants",
	    collision_box = {{-0.15, -0.15}, {0.15, 0.15}},
	    selection_box = {{-0.4, -0.4}, {0.4, 0.4}},
	    drawing_box = {{-0.5, -2.3}, {0.5, 0.5}},
	    maximum_wire_distance = 7.5,
	    supply_area_distance = 2.5,
	       energy_usage_per_tick = "1KW",
	       light = {intensity = 0.9, size = 40},
	    pictures =
	    {
	      filename = "__LightPole__/graphics/small-electric-pole/small-electric-pole.png",
	      priority = "extra-high",
	      width = 123,
	      height = 124,
	      axially_symetric = false,
	      direction_count = 4,
	      shift = {1.4, -1.1}
	    },
	    connection_points =
	    {
	      {
	        shadow =
	        {
	          copper = {2.7, 0},
	          red = {2.3, 0},
	          green = {3.1, 0}
	        },
	        wire =
	        {
	          copper = {0, -2.7},
	          red = {-0.4,-2.7},
	          green = {0.4,-2.7}
	        }
	      },
	      {
	        shadow =
	        {
	          copper = {2.7, -0.05},
	          red = {2.2, -0.35},
	          green = {3, 0.12}
	        },
	        wire =
	        {
	          copper = {-0.04, -2.8},
	          red = {-0.3, -2.9},
	          green = {0.2, -2.6}
	        }
	      },
	      {
	        shadow =
	        {
	          copper = {2.5, -0.1},
	          red = {2.55, -0.45},
	          green = {2.5, 0.25}
	        },
	        wire =
	        {
	          copper = {-0.2, -2.7},
	          red = {-0.05, -2.95},
	          green = {0, -2.4}
	        }
	      },
	      {
	        shadow =
	        {
	          copper = {2.30, -0.1},
	          red = {2.65, -0.40},
	          green = {1.75, 0.20}
	        },
	        wire =
	        {
	          copper = {0, -2.7},
	          red = {0.3, -2.85},
	          green = {-0.3, -2.5}
	        }
	      }
	    },
	copper_wire_picture =
    {
      filename = "__base__/graphics/entity/small-electric-pole/copper-wire.png",
      priority = "extra-high-no-scale",
      width = 224,
      height = 46
    },
    green_wire_picture =
    {
      filename = "__base__/graphics/entity/small-electric-pole/green-wire.png",
      priority = "extra-high-no-scale",
      width = 224,
      height = 46
    },
    radius_visualisation_picture =
    {
      filename = "__base__/graphics/entity/small-electric-pole/electric-pole-radius-visualization.png",
      width = 12,
      height = 12
    },
    red_wire_picture =
    {
      filename = "__base__/graphics/entity/small-electric-pole/red-wire.png",
      priority = "extra-high-no-scale",
      width = 224,
      height = 46
    },
    wire_shadow_picture =
    {
      filename = "__base__/graphics/entity/small-electric-pole/wire-shadow.png",
      priority = "extra-high-no-scale",
      width = 224,
      height = 46
    }
  },
  {
  	type = "lamp",
  	name = "small-pole-light",
  	selectable_in_game = false,
  	--icon = "__LightPole__/graphics/icons/x.png",
  	--flags = {"placeable-neutral", "player-creation"},
  	--minable = {hardness = 0.2, mining_time = 0.5, result = "small-pole-light"},
  	max_health = 0,
  	--corpse = "small-remnants",
  	--collision_box = {{-0.15, -0.15}, {0.15, 0.15}},
  	--selection_box = {{-0.5, -0.5}, {0.5, 0.5}},
  	energy_source =
  	{
    	type = "electric",
    	usage_priority = "secondary-input"
    },
    energy_usage_per_tick = "0KW",
    light = {intensity = 0.9, size = 10},
   picture_off =
    {
   	 	filename = "__LightPole__/graphics/small-electric-pole/small-electric-pole.png",
	    priority = "high",
	    width = 83,
	    height = 75,
	    shift = {0, -0.1}
	 },
   picture_on =
  	{
    	filename = "__LightPole__/graphics/small-electric-pole/small-electric-pole.png",
   	priority = "high",
  	width = 83,
    	height = 75,
    	x = 83,
   	shift = {0, -0.1}
    }
  },

})
It seems like the functions are built correctly to me, but this is my first LUA experience. Any pointers would be helpful.

v/r

swindle

Re: Trying to add lights to poles, is it possible?

Posted: Thu Jun 25, 2015 1:55 pm
by semvoz
Something's wrong with your control.lua file, you should post it here so we can have a look into it.
(apparently around line 3 :))

Re: Trying to add lights to poles, is it possible?

Posted: Thu Jun 25, 2015 2:15 pm
by gswindle
Sorry, its the same as the one suggested by ThePear

Code: Select all

-- 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 = "small-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="small-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 == "small-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

Re: Trying to add lights to poles, is it possible?

Posted: Thu Jun 25, 2015 2:18 pm
by ThaPear
You need to add:

Code: Select all

require "defines"
To the top of the file.

Re: Trying to add lights to poles, is it possible?

Posted: Thu Jun 25, 2015 3:33 pm
by gswindle
wow, so simple... guess i need to dig deeper into the tutorials. But, getting there.

Image

Re: Trying to add lights to poles, is it possible?

Posted: Fri Jun 26, 2015 6:53 am
by semvoz
Looking good :)

Re: Trying to add lights to poles, is it possible?

Posted: Fri Jun 26, 2015 10:45 am
by Koub
Totally love this. Hope it will get added into vanilla at some point.
Is it possible to add lights to medium and big poles (2 lights for Medoum, 4 for big, so that bigger poles produce more light, and allow to see further) ?

Re: Trying to add lights to poles, is it possible?

Posted: Fri Jun 26, 2015 10:48 am
by semvoz
Koub wrote:Totally love this. Hope it will get added into vanilla at some point.
Is it possible to add lights to medium and big poles (2 lights for Medoum, 4 for big, so that bigger poles produce more light, and allow to see further) ?
Or just light with bigger light effect?
I have created medium and large lamp to cover bigger area, would avoid to create useless 'ghost' lamps maybe?

I mean it's easy to customize the lamp's light size :)

Re: Trying to add lights to poles, is it possible?

Posted: Sun Jun 28, 2015 2:42 pm
by gswindle
Im working out the details with the The ingrediants I am thinking.
1 lamp/5 small poles = 5 lit small poles == 1/4 radius
2 lamps / 2 med poles / 1 electric circuit = 2 lit med poles== 1/2 the radius
1 lamp / 1 big pole / 2 electric cir = 1 big. === this will have the radius of the small lamp

any ideas on times for crafting these?

difficulty is going to be in how i show the on off pic... That may be a difficult obstacle to overcome, the light shape is going to shift as the pole rotates and i don't believe the poles have a node for on off and likewise the lights don't have a node for rotating. So I may have to be slightly clever.

Re: Trying to add lights to poles, is it possible?

Posted: Sun Jun 28, 2015 4:17 pm
by Koub
What would be cool : that the light radius matches the maximum distance between poles. Like that, never ever would there be the need to build a standalone lamp again. If the area is powered, it's lit, and voilĂ  ! :).

Re: Trying to add lights to poles, is it possible?

Posted: Sun Jun 28, 2015 6:02 pm
by gswindle
my thought was that if i am walking down a street there is a small gap between where the light sources are. I'm running into some newb problems that I am trying to overcome. I figure I can hammer that stuff out last.

Re: Trying to add lights to poles, is it possible?

Posted: Sun Jun 28, 2015 7:12 pm
by gswindle
my current problem...sigh ( I've tried to fix this I'm not just giving up and letting other ppl solve my problems )

The 2 blue "+" signs are where I am assuming the ghost lights are being placed. This causes the problems below
Image

The ghost lamps are now stranded deleting the poles around them ( they don't remove the lamps placed w/ the poles specifically ).
Image

And after trying to remove the last one or a pole with a lamp not within its selection radius, I receive the following error.

Code: Select all

Error while running the event handler: __LightPole__/control.lua:31: attempt to index field '?' (a nil value)


Here is the control.lua, i believe that the issue may be here in the line posted below.

Code: Select all

game.createentity{name = "small-pole-light", position = entity.position, force = entity.force}
is it possible that the positions cannot be the same? so it defaults it to that location but when it pick them up it grabs them randomly?

Code: Select all

require"defines"

-- 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 = "small-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="small-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 == "small-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 
Here is the entity.lua for good measure.

Code: Select all

data:extend(
{
	{
	    type = "electric-pole",
	    name = "small-electric-pole-with-lights",
	    icon = "__LightPole__/graphics/icons/small-electric-pole-with-light.png",
	    flags = {"placeable-neutral", "player-creation"},
	    minable = {hardness = 0.2, mining_time = 0.5, result = "small-electric-pole-with-lights"},
	    max_health = 35,
	    corpse = "small-remnants",
	    collision_box = {{-0.15, -0.15}, {0.15, 0.15}},
	    selection_box = {{-0.4, -0.4}, {0.4, 0.4}},
	    drawing_box = {{-0.5, -2.3}, {0.5, 0.5}},
	    maximum_wire_distance = 7.5,
	    supply_area_distance = 2.5,
	       energy_usage_per_tick = "1KW",
	       light = {intensity = 0.9, size = 40},
	    pictures =
	    {
	      filename = "__LightPole__/graphics/small-electric-pole/small-electric-pole.png",
	      priority = "extra-high",
	      width = 123,
	      height = 124,
	      axially_symetric = false,
	      direction_count = 4,
	      shift = {1.4, -1.1}
	    },
	    connection_points =
	    {
	      {
	        shadow =
	        {
	          copper = {2.7, 0},
	          red = {2.3, 0},
	          green = {3.1, 0}
	        },
	        wire =
	        {
	          copper = {0, -2.7},
	          red = {-0.4,-2.7},
	          green = {0.4,-2.7}
	        }
	      },
	      {
	        shadow =
	        {
	          copper = {2.7, -0.05},
	          red = {2.2, -0.35},
	          green = {3, 0.12}
	        },
	        wire =
	        {
	          copper = {-0.04, -2.8},
	          red = {-0.3, -2.9},
	          green = {0.2, -2.6}
	        }
	      },
	      {
	        shadow =
	        {
	          copper = {2.5, -0.1},
	          red = {2.55, -0.45},
	          green = {2.5, 0.25}
	        },
	        wire =
	        {
	          copper = {-0.2, -2.7},
	          red = {-0.05, -2.95},
	          green = {0, -2.4}
	        }
	      },
	      {
	        shadow =
	        {
	          copper = {2.30, -0.1},
	          red = {2.65, -0.40},
	          green = {1.75, 0.20}
	        },
	        wire =
	        {
	          copper = {0, -2.7},
	          red = {0.3, -2.85},
	          green = {-0.3, -2.5}
	        }
	      }
	    },
		copper_wire_picture =
		{
		  filename = "__base__/graphics/entity/small-electric-pole/copper-wire.png",
		  priority = "extra-high-no-scale",
		  width = 224,
		  height = 46
		},
		green_wire_picture =
		{
		  filename = "__base__/graphics/entity/small-electric-pole/green-wire.png",
		  priority = "extra-high-no-scale",
		  width = 224,
		  height = 46
		},
		radius_visualisation_picture =
		{
		  filename = "__base__/graphics/entity/small-electric-pole/electric-pole-radius-visualization.png",
		  width = 12,
		  height = 12
		},
		red_wire_picture =
		{
		  filename = "__base__/graphics/entity/small-electric-pole/red-wire.png",
		  priority = "extra-high-no-scale",
		  width = 224,
		  height = 46
		},
		wire_shadow_picture =
		{
		  filename = "__base__/graphics/entity/small-electric-pole/wire-shadow.png",
		  priority = "extra-high-no-scale",
		  width = 224,
		  height = 46
		}
			},
			{
		  	type = "lamp",
		  	name = "small-pole-light",
		  	selectable_in_game = false,
		  	icon = "__LightPole__/graphics/icons/small-electric-pole-with-light.png",
		  	collision_box = {{0, 0}, {0, 0}},
		  	max_health = 0,
		  	energy_source =
		  	{
		    	type = "electric",
		    	usage_priority = "secondary-input"
		    },
		    energy_usage_per_tick = "175W",
		    light = {intensity = 0.9, size = 20},
		   picture_off =
		    {
		   	 	filename = "__LightPole__/graphics/small-electric-pole/empty.png",
			    priority = "high",
			    width = 1,
			    height = 1,
			 	y = 1,
			 	shift = {1.4, -1.1}
		
			 },
		   picture_on =
		  	{
		    	filename = "__LightPole__/graphics/small-electric-pole/empty.png",
		    	priority = "high",
		    	--width = 1,
		    	--height = 1,
		    	--axially_symetric = false
		    	--direction_count = 4,
		    	--x = 1,
		    	--shift = {0, -0.1}
		    	width = 1,
		    	height = 1,
		    	--axially_symetric = false,
		    	--direction_count = 4,
		    	--y = 124,
		    	shift = {1.4, -1.1}
		
		    }
  },

Re: Trying to add lights to poles, is it possible?

Posted: Sun Jun 28, 2015 9:40 pm
by orzelek
Since you are using find entities fileterd it will remove some light - no guarantees that it's the proper one.

I think you'd need to keep a table of pole-light mapping and store entities in it to make sure pairs are always correct. I'm not 100% sure if game will allow table to be created that has entity on both sides.

Re: Trying to add lights to poles, is it possible?

Posted: Mon Jun 29, 2015 2:12 pm
by ThaPear
orzelek wrote:Since you are using find entities fileterd it will remove some light - no guarantees that it's the proper one.

I think you'd need to keep a table of pole-light mapping and store entities in it to make sure pairs are always correct. I'm not 100% sure if game will allow table to be created that has entity on both sides.
Nope, entities cannot correctly be used as index in a table.

It will indeed move some light, but since the radius of the findentitiesfiltered is 0.5, it'll remove the correct one. (If it was actually in the right location)
However, it seems the new light doesn't seem to be spawning in the correct location.
Try changing OnBuilt with this:

Code: Select all

game.createentity{name = "small-pole-light", position = {x = entity.position.x - 1, y = entity.position.y - 1}, force = entity.force}

Re: Trying to add lights to poles, is it possible?

Posted: Tue Jun 30, 2015 8:48 am
by gswindle
Thepear I really appreciate all your help, and everyone else's! I have a back injury that limits the amount of time that I can sit at a desk and as soon as I can I will give this a try.

Re: Trying to add lights to poles, is it possible?

Posted: Wed Jul 01, 2015 12:39 am
by ljdp
You can check for entity equality using the equals method

Code: Select all

if entity.equals(otherEntity)
. This way you can keep entities as keys in a table, you need to iterate through the table the find the correct key and it's value.