create_entity Error - Solved

Place to get help with not working mods / modding interface.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

create_entity Error - Solved

Post 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
Last edited by TheSAguy on Fri Nov 06, 2015 5:42 am, edited 1 time in total.
ZachAttackary
Inserter
Inserter
Posts: 20
Joined: Thu Oct 22, 2015 11:56 pm
Contact:

Re: create_entity Error

Post 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?
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5267
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: create_entity Error

Post by Klonan »

Your reference to 'entity' is wrong you need it to be 'event.entity'
Rseding91
Factorio Staff
Factorio Staff
Posts: 14265
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: create_entity Error

Post by Rseding91 »

Those events don't pass "event.surface" - use "event.created_entity.surface" and "event.entity.surface"
If you want to get ahold of me I'm almost always on Discord.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: create_entity Error

Post 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.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: create_entity Error

Post 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
Attachments
control.lua
Control
(1.69 KiB) Downloaded 167 times
ZachAttackary
Inserter
Inserter
Posts: 20
Joined: Thu Oct 22, 2015 11:56 pm
Contact:

Re: create_entity Error

Post 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
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: create_entity Error

Post 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.
Post Reply

Return to “Modding help”