Page 1 of 1

Turn one thing into another

Posted: Tue Mar 08, 2016 5:32 pm
by Anafemest
Hello!

Is it possible, to turn one thing into another after a certain recipe?

For example I have an object resembling a "Assembling mashine", in which I put the stuff for crafting the car (8 engine unit, 20 iron plates, 5 steel plates). After a certain time, this "Assembling mashine" becomes a car.
That is, the car is already on the ground and I can go, to fill it, sit down and go away.

Re: Turn one thing into another

Posted: Tue Mar 08, 2016 7:48 pm
by daniel34
You can't turn one entity into another, but you can remove the assembling machine by calling destroy() on it and then create a new entity (the car) on the same spot by calling game.get_surface(1).create_entity().

Example code:

Code: Select all

function replaceEntity(sourceEntity, targetEntityName)
	local entity = game.get_surface(1).create_entity{
		name = targetEntityName, 
		position = {sourceEntity.position.x, sourceEntity.position.y}, 
		force = sourceEntity.force}
	-- add code here to modify the new entity if required
	sourceEntity.destroy()
end

Re: Turn one thing into another

Posted: Wed Mar 09, 2016 7:29 am
by prg
Why would you hardcode game.get_surface(1) if you could just use sourceEntity.surface?

(Also instead of "position = {sourceEntity.position.x, sourceEntity.position.y}" you could just use "position = sourceEntity.position")

Re: Turn one thing into another

Posted: Wed Mar 09, 2016 1:17 pm
by daniel34
prg wrote:Why would you hardcode game.get_surface(1) if you could just use sourceEntity.surface?

(Also instead of "position = {sourceEntity.position.x, sourceEntity.position.y}" you could just use "position = sourceEntity.position")
Thanks for the tips. I'm still new to modding Factorio (besides a few quick fixes) and in new code I already use surfaces and other objects provided by the entities. It's probably time to revisit my old code and make these easy changes.