Turn one thing into another

Place to get help with not working mods / modding interface.
Anafemest
Manual Inserter
Manual Inserter
Posts: 1
Joined: Tue Mar 08, 2016 5:23 pm
Contact:

Turn one thing into another

Post 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.
daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

Re: Turn one thing into another

Post 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
quick links: log file | graphical issues | wiki
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Turn one thing into another

Post 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")
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

Re: Turn one thing into another

Post 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.
quick links: log file | graphical issues | wiki
Post Reply

Return to “Modding help”