Code help needed

Place to get help with not working mods / modding interface.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Code help needed

Post by TheSAguy »

Hi,

I've created a "Seed Bomb". It's a rocked that plants trees when fired.
The "Basic" version works fine, but then I tried to add a Advanced version that also changes the terrain. (Fertilizes it, before planting the tree.)

This version seems to suck up a lot of CPU power and I was hoping someone could help me optimize the code.

I use the "on_trigger_created_entity" for the Basic ammo, but had to do something different for the Advanced, so I placed a dummy entity that triggered and swamped it out for the same entity as in the Basic version, since I did not know how else to tell the difference between the basic and advanced. I think if there is a way, it would improve performance a lot already.

It's hard to explain, best to load the mod and sheet the Seed Bomb using the rocket launcher.
Pic
I don't think that the trees are actually growing with the Advanced version :(
Code
Attachments
Bio_Industries_2.1.0.zip
MOD
(8.37 MiB) Downloaded 66 times
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Code help needed

Post by darkfrei »

It seems that you need to make part of this code by every tick, but not whole code in one tick.
Bilka
Factorio Staff
Factorio Staff
Posts: 3470
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Code help needed

Post by Bilka »

Disclaimer: I only skimmed over your code.

I think you might be experiencing slowdowns due to

Code: Select all

table.insert(global.bi.tree_growing, {position = position, time = event.tick + max_grow_time, surface = surface})
table.sort(global.bi.tree_growing, function(a, b) return a.time < b.time end)
Not only are you accessing global. , which is relatively slow, you are also doing table.insert and table.sort, both of which are rather slow.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Code help needed

Post by TheSAguy »

Bilka wrote:Disclaimer: I only skimmed over your code.

I think you might be experiencing slowdowns due to

Code: Select all

table.insert(global.bi.tree_growing, {position = position, time = event.tick + max_grow_time, surface = surface})
table.sort(global.bi.tree_growing, function(a, b) return a.time < b.time end)
Not only are you accessing global. , which is relatively slow, you are also doing table.insert and table.sort, both of which are rather slow.
Bilka,

With what will I need to replace that with than? I need to store the grow times, since if you load a game you still need them.
darkfrei wrote:It seems that you need to make part of this code by every tick, but not whole code in one tick.
Well, this is how I currently do it:

Code: Select all


---- Growing Tree
Event.register(defines.events.on_tick, function(event)	


	while #global.bi.tree_growing > 0 do
		if event.tick < global.bi.tree_growing[1].time then
			break 
		end

		Grow_tree(global.bi.tree_growing[1].position, global.bi.tree_growing[1].surface)
		table.remove(global.bi.tree_growing, 1)
	end

end)

Again, the normal seed bomb works okay. it's when I need to both switch out the terrain and my dummy seed that things go very slow...
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Code help needed

Post by eradicator »

Bilka wrote: Not only are you accessing global. , which is relatively slow, you are also doing table.insert and table.sort, both of which are rather slow.
Accessing global during runtime should™ not be any slower than accessing any other table. And table.insert() most certainly is not a slow operation. At least not slow in the sense that you'd notice it ingame when used less than a few thousand times. It sounds more like OP is creating lots of tiles but calls surface.set_tiles() once for each tile instead of calling it once for all tiles. The API doc specifically states that it is recommended to call it only once. Though the reason it states is tile correction logic, which was removed in 0.16 so the effect might not be as bad now, it is probably still a significant performance drain and would fit the described lag pattern of "only when using the rocket". Also it would be better to store the result of <if game.active_mods["alien-biomes"]> than to call it every time as it's not going to change. Other than that....try using log() to determine which parts of the code are actually slow. Log prints a timestamp to the console/log when uesd.
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.
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1456
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Code help needed

Post by TheSAguy »

My latest version is not to bad. I created duplicate seed entities that I don't need to switch out.
Attached.
Attachments
Bio_Industries_2.1.0.zip
Mod
(8.31 MiB) Downloaded 72 times
Post Reply

Return to “Modding help”