Page 1 of 1

Spawn ore when landfill is placed

Posted: Thu Nov 26, 2020 8:37 am
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?

Re: Spawn ore when landfill is placed

Posted: Thu Nov 26, 2020 12:20 pm
by eradicator
Works fine for me. Please post the exact error message.

Re: Spawn ore when landfill is placed

Posted: Thu Nov 26, 2020 12:39 pm
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

Re: Spawn ore when landfill is placed

Posted: Thu Nov 26, 2020 1:37 pm
by patfre
And......... no one seam to care :/

Re: Spawn ore when landfill is placed

Posted: Thu Nov 26, 2020 1:40 pm
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.

Re: Spawn ore when landfill is placed

Posted: Thu Nov 26, 2020 1:42 pm
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.

Re: Spawn ore when landfill is placed

Posted: Thu Nov 26, 2020 1:43 pm
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.

Re: Spawn ore when landfill is placed

Posted: Thu Nov 26, 2020 1:56 pm
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

Re: Spawn ore when landfill is placed

Posted: Thu Nov 26, 2020 2:16 pm
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)

Re: Spawn ore when landfill is placed

Posted: Thu Nov 26, 2020 2:35 pm
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

Re: Spawn ore when landfill is placed

Posted: Thu Nov 26, 2020 2:48 pm
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]

Re: Spawn ore when landfill is placed

Posted: Thu Nov 26, 2020 2:59 pm
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.

Re: Spawn ore when landfill is placed

Posted: Thu Nov 26, 2020 3:02 pm
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.]

Re: Spawn ore when landfill is placed

Posted: Thu Nov 26, 2020 3:47 pm
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

Re: Spawn ore when landfill is placed

Posted: Thu Nov 26, 2020 3:55 pm
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.