Page 1 of 1

[0.12]forces.chart method problem

Posted: Fri Jul 31, 2015 12:05 am
by Scyth3
I'm having some trouble understanding Lua tables. What I'm trying to do as a learning excercice is adding automatic charting to every chunk generated with the chunk generation event.

I'm using the Ore Expansion 1.0.1 mod as a base since I'm pretty new to Lua but so far I haven't managed to do so successfully as I'm getting "specified surface doesn't have a chart".

Could someone with some Lua experience give me a hand? I've tried the wiki and googling around but no luck so far.

Troublesome line is the last one.

Code: Select all

require "defines"

if not orexpmult then orexpmult = {} end


game.on_event(defines.events.on_chunk_generated, function(event)

	local distX = math.abs(event.area.left_top.x)
	local distY = math.abs(event.area.left_top.y)
	local mult = 1 + (math.floor(math.sqrt((distX * distX) + (distY * distY))) / 1024)
	orexpmult = mult
	--game.player.print(distX)
	--game.player.print(distY)
	--game.player.print(orexpmult)

	if mult > 1 then
		for _,ore in pairs(event.surface.find_entities_filtered({area = {{event.area.left_top.x, event.area.left_top.y}, {event.area.right_bottom.x, event.area.right_bottom.y}}, type="resource"})) do
			ore.amount = math.floor(ore.amount * mult)
		end
	end

	game.forces.player.chart(event.surface,{lefttop = {x = event.area.left_top.x, y = event.area.left_top.y}, rightbottom = {x = event.area.right_bottom.x, y = event.area.right_bottom.y}})

end)

Re: [0.12]forces.chart method problem

Posted: Fri Jul 31, 2015 12:14 am
by orzelek
No idea why it errors out.
But I think you are trying to do something strange in general. On chunk generated event is actually triggered by charting of the chunk in question.

Re: [0.12]forces.chart method problem

Posted: Fri Jul 31, 2015 12:21 am
by Scyth3
No practical purpose just for science really. I'd just like both of them to go hand in hand.

My extremely wild guess is that chunks generate before the player spawns and because of this there is no player reference to chart to. Also changing

Code: Select all

game.forces.player.chart(event.surface,{lefttop = {x = event.area.left_top.x, y = event.area.left_top.y}, rightbottom = {x = event.area.right_bottom.x, y = event.area.right_bottom.y}})
to

Code: Select all

game.forces.player.chart(game.player.surface],{lefttop = {x = event.area.left_top.x, y = event.area.left_top.y}, rightbottom = {x = event.area.right_bottom.x, y = event.area.right_bottom.y}})
gives a similar error even though it works perfectly in the ingame console by manually replacing the x,y coordinates.

Re: [0.12]forces.chart method problem

Posted: Fri Jul 31, 2015 8:05 am
by jorgenRe
I did trye out the:
game.on_event(defines.events.on_chunk_generated, function(event)
a few days ago, but i also ended up with an error so my guess was that something has changed with it due to the introduction of multiple surfaces(worlds) :/

Re: [0.12]forces.chart method problem

Posted: Fri Jul 31, 2015 8:47 am
by jorgenRe
I did trye out the:
game.on_event(defines.events.on_chunk_generated, function(event)
a few days ago, but i also ended up with an error so my guess was that something has changed with it due to the introduction of multiple surfaces(worlds) :/