Page 1 of 1

create_entity Error - Solved

Posted: Thu Nov 05, 2015 5:00 am
by TheSAguy
Can someone please tell me what I'm doing wrong in my code for Create_Entity
I've tries several different syntax, but can't figure it out...

Control.lua code:

Code: Select all

require "defines"
require "util"


---------------------------------------------
script.on_event(defines.events.on_robot_built_entity, function(event) On_Built(event) end)
script.on_event(defines.events.on_built_entity, function(event) On_Built(event) end)
script.on_event({defines.events.on_entity_died,defines.events.on_robot_pre_mined_item,defines.events.on_preplayer_mined_item,},function(event) On_Remove(event) end)


---------------------------------------------
function On_Built(event)
     
   --- Bio Farm has been built
	if event.created_entity.name == "bf_bio_farm" then
	local surface = game.surfaces['nauvis'] -- Old Code, need to fix
	event.surface.create_entity{name = "bf_bio_farm_light", position = entity.position, force = entity.force}
	
	--game.create_entity{name = "bf_bio_farm_light", position = entity.position, force = entity.force}
	--game.surface.create_entity{name = "bf_bio_farm_light", position = entity.position, force = entity.force}
	--event.create_entity{name = "bf_bio_farm_light", position = entity.position, force = entity.force}
	--event.created_entity.surface.create_entity{name = "bf_bio_farm_light", position = entity.position, force = entity.force}
	
	end
end

---------------------------------------------
function On_Remove(event)
	
	--- Bio Farm has been removed
   	if event.created_entity.name == "bf_bio_farm" then
		res = game.get_surface(1).find_entities_filtered{name="bf_bio_farm_light", area=GetArea(event.created_entity.position, 0.5)}
		if #res then
         -- If we've found it, destroy it.
         res[1].destroy()
		end
	end
	--- Bio Farm's Light Source has been removed
   	if event.created_entity.name == "bf_bio_farm_light" then		
		res = game.get_surface(1).find_entities_filtered{name="bf_bio_farm", area=GetArea(event.created_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

Re: create_entity Error

Posted: Thu Nov 05, 2015 6:09 am
by ZachAttackary
TheSAguy wrote: event.surface.create_entity{name = "bf_bio_farm_light", position = entity.position, force = entity.force}
I may be just dense, but where are you declaring entity?

Re: create_entity Error

Posted: Thu Nov 05, 2015 6:54 am
by Klonan
Your reference to 'entity' is wrong you need it to be 'event.entity'

Re: create_entity Error

Posted: Thu Nov 05, 2015 8:18 am
by Rseding91
Those events don't pass "event.surface" - use "event.created_entity.surface" and "event.entity.surface"

Re: create_entity Error

Posted: Thu Nov 05, 2015 4:51 pm
by TheSAguy
Thanks guys!
Rseding, I totally forgot about your item collector mod!
I could not find an example and had it all the time...

Thanks again.

Re: create_entity Error

Posted: Fri Nov 06, 2015 12:29 am
by TheSAguy
Now I'm having trouble with On_Remove.
The problem is with line 29: "if event.created_entity.name == "bf_bio_farm" then"
I'm guessing it's because the entity is not being created ,but mined...

Code: Select all

function On_Remove(event)
	
	--- Bio Farm has been removed
   	if event.created_entity.name == "bf_bio_farm" then
		res = game.get_surface(1).find_entities_filtered{name="bf_bio_farm_light", area=GetArea(event.created_entity.position, 0.5)}

		if #res then
         -- If we've found it, destroy it.
         res[1].destroy()
		end
	end
	
	--- Bio Farm's Light Source has been removed
   	if event.created_entity.name == "bf_bio_farm_light" then		
		res = game.get_surface(1).find_entities_filtered{name="bf_bio_farm", area=GetArea(event.created_entity.position, 0.5)}

		if #res then
         -- If we've found it, destroy it.
         res[1].destroy()
		end
	end
        
end
Error:
Image

I got the On_Build working:

Code: Select all

function On_Built(event)
     
    --- Bio Farm has been built
	if event.created_entity.name == "bf_bio_farm" then
	
    local surface = event.created_entity.surface
    local force = event.created_entity.force
	surface.create_entity({name = "bf_bio_farm_light", position = event.created_entity.position, force = force})
	
	end
	
end
Thanks

Re: create_entity Error

Posted: Fri Nov 06, 2015 2:06 am
by ZachAttackary
Either the on_robot_pre_mined or the on_preplayer_mined_item to access the entity it would be event.entity.

so in your case I think you are going to want event.entity.name == "bf_bio_farm" then

Re: create_entity Error

Posted: Fri Nov 06, 2015 5:41 am
by TheSAguy
ZachAttackary wrote:Either the on_robot_pre_mined or the on_preplayer_mined_item to access the entity it would be event.entity.

so in your case I think you are going to want event.entity.name == "bf_bio_farm" then
Thanks ZachAttackary, that was it.