Good way to go about growing trees?

Place to get help with not working mods / modding interface.
GotLag
Filter Inserter
Filter Inserter
Posts: 534
Joined: Sat May 03, 2014 3:32 pm
Contact:

Good way to go about growing trees?

Post by GotLag »

I'm looking to make a simple mod that allows growing trees. Craft or find saplings, plant them and they'll turn into full-sized trees after some delay.
I don't play multi and from memory last time I did control.lua modding there are things to avoid to maintain multiplayer sync.

I want to be able to place a sapling entity, and after some delay (fixed or random, not sure yet) it will be removed and replaced by a full-sized tree (don't have the art skills to do intermediate steps, but the same principle would apply).
So what's the best way to go about adding a whole bunch of timed events in a way that won't break multiplayer or slow the game down too much if the player throws down a large number?
User avatar
Arch666Angel
Smart Inserter
Smart Inserter
Posts: 1636
Joined: Sun Oct 18, 2015 11:52 am
Contact:

Re: Good way to go about growing trees?

Post by Arch666Angel »

Isn't that exactly what the treefarm mod does?
orzelek
Smart Inserter
Smart Inserter
Posts: 3928
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Good way to go about growing trees?

Post by orzelek »

There is also a Greenhouse mod that "grows" trees as a normal production recipe - much more performance friendly then treefarm but more limited (wood only).
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5410
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Good way to go about growing trees?

Post by Klonan »

GotLag wrote:I'm looking to make a simple mod that allows growing trees. Craft or find saplings, plant them and they'll turn into full-sized trees after some delay.
I don't play multi and from memory last time I did control.lua modding there are things to avoid to maintain multiplayer sync.

I want to be able to place a sapling entity, and after some delay (fixed or random, not sure yet) it will be removed and replaced by a full-sized tree (don't have the art skills to do intermediate steps, but the same principle would apply).
So what's the best way to go about adding a whole bunch of timed events in a way that won't break multiplayer or slow the game down too much if the player throws down a large number?
Ok so the best way i can see is as follows:

1. When you build the sapling, enter it into a global table,

Code: Select all

script.on_event(defines.on_built_entity, function(event)
if event.entity.name == "sapling" then table.insert(global.sapling, sapling)
end
2.Have a function everytick iterate over the table

Code: Select all

script.on_event(defines.on_tick, check_saplings)

function check_saplings()
for k, sapling in pairs (global.saplings) do
  if k % 600 == game.tick % 600 then //This is the magic, the 600 spreads all the checks over 10 seconds
    if math.random(0,1) > 0.6 then //This is how you can make it semi-random
      grow_sapling(sapling)
    end
  end
end
end
This is just a rough outline, but i hope it helps
Post Reply

Return to “Modding help”