Custom Item w/ Custom event

Place to get help with not working mods / modding interface.
Post Reply
imperium
Burner Inserter
Burner Inserter
Posts: 7
Joined: Wed Oct 14, 2015 8:06 pm
Contact:

Custom Item w/ Custom event

Post 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!

SirRichie
Fast Inserter
Fast Inserter
Posts: 244
Joined: Wed Feb 25, 2015 4:50 pm
Contact:

Re: Custom Item w/ Custom event

Post 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

imperium
Burner Inserter
Burner Inserter
Posts: 7
Joined: Wed Oct 14, 2015 8:06 pm
Contact:

Re: Custom Item w/ Custom event

Post 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?

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Custom Item w/ Custom event

Post 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)
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

imperium
Burner Inserter
Burner Inserter
Posts: 7
Joined: Wed Oct 14, 2015 8:06 pm
Contact:

Re: Custom Item w/ Custom event

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

daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

Re: Custom Item w/ Custom event

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

imperium
Burner Inserter
Burner Inserter
Posts: 7
Joined: Wed Oct 14, 2015 8:06 pm
Contact:

Re: Custom Item w/ Custom event

Post by imperium »

thanks daniel. That works perfectly as well and is much better for future use

imperium
Burner Inserter
Burner Inserter
Posts: 7
Joined: Wed Oct 14, 2015 8:06 pm
Contact:

Re: Custom Item w/ Custom event

Post 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!

Post Reply

Return to “Modding help”