can_place_entity help [fixed]

Place to get help with not working mods / modding interface.
matjojo
Filter Inserter
Filter Inserter
Posts: 338
Joined: Wed Jun 17, 2015 6:08 pm
Contact:

can_place_entity help [fixed]

Post by matjojo »

Hi, I am working on a new mod, and for the mod I need to place belt entities. I found game.get_surface(1).can_place_entity{...} to check if it would work on lua-api.factorio.com. But I can't get it to work.

Code: Select all

script.on_event(defines.events.on_built_entity, function(event)

	if event.created_entity.type == "transport-belt" then
		BB_var_name = event.created_entity.name
		BB_var_X = event.created_entity.position.x
		BB_var_Y = event.created_entity.position.Y
		BB_var_dir = event.created_entity.direction
		BB_var_force = event.created_entity.force
		if game.get_surface(1).can_place_entity{name = BB_var_name, position = {x = (BB_var_X + 1), y = (BB_var_Y)}, direction = BB_var_dir, force = BB_var_force} then
			game.get_surface(1).create_entity{name = BB_var_name, position = {x = (BB_var_X + 1), y = (BB_var_Y)}, direction = BB_var_dir, force = BB_var_force}
		end
	end

end)
But, when I place a basic transport belt, the game gives this error:

Code: Select all

Error while running the event handler: __BeltBrush__/control.lua:11: bad argument #-1 to 'can_place_entity' (number expected, got nil)
Line 11 is the line with can_place_entity in it.
With this code I though that because of the 'bad argument #-1' part the problem must be in the third argument, (one from the last.) But that variable is not nil, its the same direction as the placed entity, and printing that direction with game.player.print does not crash, or return nil.

I tried changing the variable to 'defines.direction.north' just for testing, but the same error occurred.

What is it that I'm doing wrong?

Matjojo
Last edited by matjojo on Wed Mar 16, 2016 1:54 pm, edited 1 time in total.
Oxyd
Former Staff
Former Staff
Posts: 1428
Joined: Thu May 07, 2015 8:42 am
Contact:

Re: can_place_entity help

Post by Oxyd »

matjojo wrote:

Code: Select all

BB_var_Y = event.created_entity.position.Y
Since event.created_entity.position has no member called Y, BB_var_Y will end up nil. You probably meant to use lowercase y there.
matjojo
Filter Inserter
Filter Inserter
Posts: 338
Joined: Wed Jun 17, 2015 6:08 pm
Contact:

Re: can_place_entity help

Post by matjojo »

Oxyd wrote:
matjojo wrote:

Code: Select all

BB_var_Y = event.created_entity.position.Y
Since event.created_entity.position has no member called Y, BB_var_Y will end up nil. You probably meant to use lowercase y there.
well, I should have probably seen that myself, any reason the error said #-1? what does that mean if it does not meant that it is the first from the last argument?
Post Reply

Return to “Modding help”