Spawn ore when landfill is placed

Place to get help with not working mods / modding interface.
Post Reply
patfre
Burner Inserter
Burner Inserter
Posts: 14
Joined: Sun Nov 15, 2020 1:59 pm
Contact:

Spawn ore when landfill is placed

Post by patfre »

So i am making a mod and i need it so there's a chance that when landfill is placed an ore tile will spawn with a custom amount.
So far i only have an event to detect when the player has placed a tile but i am a bit stuck because it doesn't seem that the tiles property of the event's input is structured the way the factorio api says because it keeps making error when i do things that should work.
Here's the script i have so far

Code: Select all

function tilePlaced(event)
    for _, tile in ipairs(event.tiles) do -- makes error
        
    end
end

script.on_event(defines.events.on_player_built_tile, tilePlaced)
so can anyone help?

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Spawn ore when landfill is placed

Post by eradicator »

Works fine for me. Please post the exact error message.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

patfre
Burner Inserter
Burner Inserter
Posts: 14
Joined: Sun Nov 15, 2020 1:59 pm
Contact:

Re: Spawn ore when landfill is placed

Post by patfre »

eradicator wrote:
Thu Nov 26, 2020 12:20 pm
Works fine for me. Please post the exact error message.
it is a bit strange earlier it made error now it doesn't weird.
I do still need help though

patfre
Burner Inserter
Burner Inserter
Posts: 14
Joined: Sun Nov 15, 2020 1:59 pm
Contact:

Re: Spawn ore when landfill is placed

Post by patfre »

And......... no one seam to care :/

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Spawn ore when landfill is placed

Post by eradicator »

patfre wrote:
Thu Nov 26, 2020 1:37 pm
And......... no one seam to care :/
Maybe you should try stating what you need help with? This is "modding help" not "make this mod for me please". Also the topic isn't even 6 hours old. If you need instant help go to the modding discord.

Generally your idea seems quite simple. math.random() + surface.create_entity + tile position.
Last edited by eradicator on Thu Nov 26, 2020 1:43 pm, edited 1 time in total.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

patfre
Burner Inserter
Burner Inserter
Posts: 14
Joined: Sun Nov 15, 2020 1:59 pm
Contact:

Re: Spawn ore when landfill is placed

Post by patfre »

Maybe you should try stating what you need help with? This is "modding help" not "make this mod for me please".
i know this is modding help i am asking for help here not can someone make this mod for me.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Spawn ore when landfill is placed

Post by eradicator »

patfre wrote:
Thu Nov 26, 2020 1:42 pm
i am asking for help here
But what with? In your open post you say you have an error, but that was apparently solved. So what's next? The more precise your question is the easier it is to help.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

patfre
Burner Inserter
Burner Inserter
Posts: 14
Joined: Sun Nov 15, 2020 1:59 pm
Contact:

Re: Spawn ore when landfill is placed

Post by patfre »

i said i needed a way to spawn ores on the landfill that has been placed and i said this is the code i had so far and i needed help with not only the error but also the spawns ores on placed landfill

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Spawn ore when landfill is placed

Post by darkfrei »

patfre wrote:
Thu Nov 26, 2020 1:56 pm
i said i needed a way to spawn ores on the landfill that has been placed and i said this is the code i had so far and i needed help with not only the error but also the spawns ores on placed landfill
https://lua-api.factorio.com/latest/eve ... built_tile
https://lua-api.factorio.com/latest/Con ... ndPosition
https://lua-api.factorio.com/latest/Lua ... ate_entity

Code: Select all

script.on_event(defines.events.on_player_built_tile, function(event)
	local surface = game.surfaces[event.surface_index]
	local tiles = event.tiles
	for i, tile in pairs (tiles) do
		local position = tile.position
		surface.create_entity{name='iron-ore', position=position , amount=1}
	end
end)
Attachments
2020-11-26T15_19_22-Factorio 1.1.1.png
2020-11-26T15_19_22-Factorio 1.1.1.png (802.26 KiB) Viewed 2168 times

patfre
Burner Inserter
Burner Inserter
Posts: 14
Joined: Sun Nov 15, 2020 1:59 pm
Contact:

Re: Spawn ore when landfill is placed

Post by patfre »

darkfrei wrote:
Thu Nov 26, 2020 2:16 pm
patfre wrote:
Thu Nov 26, 2020 1:56 pm
i said i needed a way to spawn ores on the landfill that has been placed and i said this is the code i had so far and i needed help with not only the error but also the spawns ores on placed landfill
https://lua-api.factorio.com/latest/eve ... built_tile
https://lua-api.factorio.com/latest/Con ... ndPosition
https://lua-api.factorio.com/latest/Lua ... ate_entity

Code: Select all

script.on_event(defines.events.on_player_built_tile, function(event)
	local surface = game.surfaces[event.surface_index]
	local tiles = event.tiles
	for i, tile in pairs (tiles) do
		local position = tile.position
		surface.create_entity{name='iron-ore', position=position , amount=1}
	end
end)
this works thank you

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Spawn ore when landfill is placed

Post by eradicator »

patfre wrote:
Thu Nov 26, 2020 1:56 pm
i said i needed a way to spawn ores on the landfill that has been placed and i said this is the code i had so far and i needed help with not only the error but also the spawns ores on placed landfill
I gave you all the functions you need:
eradicator wrote:
Thu Nov 26, 2020 1:40 pm
Generally your idea seems quite simple. math.random() + surface.create_entity + tile position.
Giving you a ready-to-use function like @darkfrei is what i called "please make the mod for me". Which i will never do because i follow the teach a man how to fish rule, which dictates that giving you the function is the opposite of helping you learn. I'm not trying to be mean or anything.

Btw, assigning local position= in that loop is superfluous as it is only used once.

[Edit: Hooray, 4444 posts :P]
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Spawn ore when landfill is placed

Post by darkfrei »

eradicator wrote:
Thu Nov 26, 2020 2:48 pm
Giving you a ready-to-use function like @darkfrei is what i called "please make the mod for me". Which i will never do because i follow the teach a man how to fish rule, which dictates that giving you the function is the opposite of helping you learn. I'm not trying to be mean or anything.

Btw, assigning local position= in that loop is superfluous as it is only used once.

[Edit: Hooray, 4444 posts :P]
He is tried, but used ipairs instead of pairs.

I'm pretty sure that he can learn new stuff just by reading the code, all used Lua-API links are also here.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Spawn ore when landfill is placed

Post by eradicator »

darkfrei wrote:
Thu Nov 26, 2020 2:59 pm
He is tried, but used ipairs instead of pairs.
What's wrong with using ipairs to iterate an array? If anything that's more correct than your use of pairs.
[Edit: Meh, but i don't want to derails this into a discussion about code quality, or the handfull of things you could do to make that bit of code faster.]
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Spawn ore when landfill is placed

Post by darkfrei »

eradicator wrote:
Thu Nov 26, 2020 3:02 pm
darkfrei wrote:
Thu Nov 26, 2020 2:59 pm
He is tried, but used ipairs instead of pairs.
What's wrong with using ipairs to iterate an array? If anything that's more correct than your use of pairs.
[Edit: Meh, but i don't want to derails this into a discussion about code quality, or the handfull of things you could do to make that bit of code faster.]
Here was several issues about pairs and ipairs:
viewtopic.php?t=28599 (ipairs doesn't work)
viewtopic.php?p=233158#p233158 (pairs are faster)
viewtopic.php?t=27256 (ipairs doesn't work)

UPD: https://lua-api.factorio.com/latest/LuaCustomTable.html

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Spawn ore when landfill is placed

Post by eradicator »

darkfrei wrote:
Thu Nov 26, 2020 3:47 pm
Here was several issues about pairs and ipairs:
The only issue is that you need to understand that ipairs iterates arrays only - different functions do different things. And the people in those threads apparently didn't understand. But knowing the difference is essential to understanding when you can use a "for i=1,n" loop to improve performance, and when you can't.
No idea what "UPD" means, but the tiles table in on_player_built_tile is not a LuaCustomTable.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Modding help”