Jump to content

Is it possible to replace a fully grown plant in farm by another prefab ?


Recommended Posts

Hi,

I would like to replace a fully grown plant in a farm by another prefab when an event triggers. Like, replacing a carrot by, let's say, a monster plant (new prefab). Is it possible ? Does someone have an idea to where to start ?

Also, if it's possible i would like to replace like 5-8 farm crop at the same time (so i don't replace all crop if the player have many farm), number are an example.

Link to comment
Share on other sites

do you want to replace the crop with another crop ? Or with a another prefab?

So replace planted carrot with planted melon?
or
replace planted plant with something not planted, like a hound.
or
When planted melon is harvested, put not a melon in inventory, but prefab x ?

Do you already have the event, that should trigger it?
And should it have a range effect, so only crops in range should convert? Or all in the world? if only ~5 in the world, what function to choose which ones?

Edited by Serpens
Link to comment
Share on other sites

What i would like to do is, on event "newmoon" (so probably something like inst:WatchWorldState("isnewmoon", MyFunction) ), replace an existing crop on farm by a nightmare crop (custom prefab), that you could obtain only this way (so not a prefab usually growing on farm). And do this for 5-8 crop in the world. I don't know how to choose which one, randomly is fine if possible ?

So for example, 5-8 crop plan fully grown in farm (melon, carrot, eggplant...) are replaced by a nightmare crop.nightmarecrop.png

It's just an idea that i like and that could fit the theme (if i remember well, long ago klei thought about doing something similar but they never did). But if it's too complex, tell me, it's not that important, just something i find nice :)

Link to comment
Share on other sites

It is possible.

I think the best would be to mod the farms itself. Downside is that you can't know all future "mod farms", but you can include all you know by now. (there is a farm mod where crops also grow in winter).

So when this event is fired, every farm with a full grown plant has a chance of ~10% (or any value you prefer) to transform their crop.

I will take a look at the farm code, to see how this is done and write you the code ;)

Link to comment
Share on other sites

... took me an hour to find out how the growing of plants works in farms O.ô

This code will work for all farms which use the "grower" component. So also mod farms.

local function OnIsNewmoon(inst,data)
    if inst.components and inst.components.grower then 
        local matured = true
        for mycrop,v in pairs(inst.components.grower.crops) do -- for some reason it is a list of crops per farm, not only one. So we now check, if every crop on the farm is full grown.
            if mycrop.components and mycrop.components.crop and not mycrop.components.crop.matured then
                matured = false
            end
        end
        if matured and math.random() >= 0.9 then -- ~10% chance
            inst.components.grower:Reset() -- remove previous plants
            item = GLOBAL.SpawnPrefab("dragonfruit_seeds")
            inst.components.grower:PlantItem(item)      -- plant our new plant
            for mycrop,v in pairs(inst.components.grower.crops) do
                if mycrop.components and mycrop.components.crop then 
                    mycrop.AnimState:PlayAnimation("grow_pst")
                    mycrop.components.crop:Mature()          -- and make them fully grown
                    if mycrop.components.crop.task ~= nil then
                        mycrop.components.crop.task:Cancel()
                        mycrop.components.crop.task = nil
                    end
                end
            end
        end
    end
end

AddComponentPostInit("grower",function(self)
    self.inst:WatchWorldState("isnewmoon", OnIsNewmoon)
end)

replace dragonfruit seed with your seed.

Edited by Serpens
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...