Page 1 of 1
Good way to go about growing trees?
Posted: Fri Feb 19, 2016 1:22 am
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?
Re: Good way to go about growing trees?
Posted: Fri Feb 19, 2016 1:55 am
by DaveMcW
Re: Good way to go about growing trees?
Posted: Fri Feb 19, 2016 10:42 am
by Arch666Angel
Isn't that exactly what the treefarm mod does?
Re: Good way to go about growing trees?
Posted: Fri Feb 19, 2016 11:03 am
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).
Re: Good way to go about growing trees?
Posted: Fri Feb 19, 2016 12:46 pm
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