Page 1 of 1

Custom Item w/ Custom event

Posted: Wed Apr 13, 2016 10:37 pm
by imperium
So I am very new to modding factorio. I can do the basic add an item, etc etc. What I want to learn a bit more about is doing custom things. I am currently wanting to create an item that can either be thrown or placed (not both, I just dont know which will be easier). When said item is thrown/placed it "digs" the topsoil away and reveals some new ores.

What I do not know how to do, is create the event that transitions from "if(item is thrown/placed) then :spawn ore". Where and how would I go about creating an event for this based on this specific item? The only data I think I need is item_position (so I know where to spawn the ore). How do I go about finding that location?

I tried looking for tutorials and such for this but couldn't find much that helped my case. So if someone could let me know how to do this, or send me to a tutorial that covers this that would be amazing!

Re: Custom Item w/ Custom event

Posted: Wed Apr 13, 2016 11:05 pm
by SirRichie
Hi and welcome to modding.

You are probably looking to use the events.on_built_entity event. Register it via the bootstrap (just like on_init, just use on_event): http://lua-api.factorio.com/0.12.30/Lua ... l#on_event

Re: Custom Item w/ Custom event

Posted: Wed Apr 13, 2016 11:21 pm
by imperium
SirRichie wrote:Hi and welcome to modding.

You are probably looking to use the events.on_built_entity event. Register it via the bootstrap (just like on_init, just use on_event): http://lua-api.factorio.com/0.12.30/Lua ... l#on_event

Code: Select all

require "defines"

game.on_built_entity(topsoil)

function topsoil(x,y)
	local_player.game.surface.create_entity({name="copper-ore", amount=5, position={x,y}})
end
So here is my code so far. Its currently located in "control.lua". For now im okay to not test for water, but later how will I test to see if its placed next to water (I plan on it making a 5x5 grid of ores)?

Also how do I test to see if its a specific item being placed? And how do I remove the "used" item?

Re: Custom Item w/ Custom event

Posted: Wed Apr 13, 2016 11:37 pm
by prg
imperium wrote:So here is my code so far. Its currently located in "control.lua". For now im okay to not test for water, but later how will I test to see if its placed next to water (I plan on it making a 5x5 grid of ores)?
Depending on what exactly you want to do, maybe one of
surface.can_place_entity
surface.get_tile
imperium wrote:Also how do I test to see if its a specific item being placed? And how do I remove the "used" item?

Code: Select all

script.on_event(defines.events.on_built_entity, function(event)
    local e = event.created_entity
    if e.name == "my-entity" then
        --do stuff with e.position
        e.destroy()
    end
end)

Re: Custom Item w/ Custom event

Posted: Thu Apr 14, 2016 1:11 am
by imperium
Thanks a billion prg!

For those who may see this here is my final code:

Code: Select all

require "defines"

script.on_event(defines.events.on_built_entity, function(event)
    local e = event.created_entity
    if e.name == "accumulator-3" then
		game.get_surface(1).create_entity({name="copper-ore", amount=1, position={e.position.x,e.position.y}})
		e.destroy()
    end
end)
For the time being it places a single copper ore at the location i place my mk3 accumulator (just temp entities). LuaSurface was a big help.

Re: Custom Item w/ Custom event

Posted: Thu Apr 14, 2016 1:21 am
by daniel34
Instead of game.get_surface(1) you should use event.created_entity.surface (or in your case simply e.surface), otherwise your mod won't work properly with other mods that use different surfaces.

Re: Custom Item w/ Custom event

Posted: Thu Apr 14, 2016 5:37 pm
by imperium
thanks daniel. That works perfectly as well and is much better for future use

Re: Custom Item w/ Custom event

Posted: Fri Apr 15, 2016 12:20 am
by imperium
Ran into the issue that the game was letting me place multiple instances of the extractor item that I made over itself. I had it where unless it dug up some ore, then it didnt get destroyed and left behind the building. If you built over that it would layer until you had 20 buildings stacked on top of each other. Talked with prg in PM and finally figured out how to set the entity type as "simple-object" rather than "decorative". I couldn't find an example of that type until I looked in entities/demo-remnants.lua and found the wrecked ships. Works exactly like I want it now! Thanks everyone!