Help with Code - Growable Crops


MidrealmDM

Recommended Posts

Originally posted here ( http://forums.kleientertainment.com/topic/50676-growable-crop-seeds/ )

 

I have written up some new crops and crop seeds and the prefabs for them work fine.

You can feed the items to a caged bird and it makes the proper seeds and those will grow the right crop.

 

But what I can't seem to do is get the generic seeds found in the game to have a chance to produce these new crops.

 

I have found a set of data in veggies.lua in the game prefab folder that defines the weight for the chance of given seeds to grow into certain crops.

 

How can I insert additional data into the veggies.lua or failing that give a chance to grow my crops instead?

 

from veggies.lua

VEGGIES =
{
carrot = MakeVegStats(COMMON,    TUNING.CALORIES_SMALL,    TUNING.HEALING_TINY,    TUNING.PERISH_MED, 0, TUNING.CALORIES_SMALL,    TUNING.HEALING_SMALL,    TUNING.PERISH_FAST, 0),

corn = MakeVegStats(COMMON, TUNING.CALORIES_MED,    TUNING.HEALING_SMALL,    TUNING.PERISH_MED, 0, TUNING.CALORIES_SMALL,    TUNING.HEALING_SMALL,    TUNING.PERISH_SLOW, 0),

 

pumpkin = MakeVegStats(UNCOMMON,    TUNING.CALORIES_LARGE,    TUNING.HEALING_SMALL,    TUNING.PERISH_MED, 0, TUNING.CALORIES_LARGE,    TUNING.HEALING_MEDSMALL,    TUNING.PERISH_FAST, 0),

-- other crops, etc. etc.

}

 

The game file seeds.lua actually interprets the data to determine the chance of growing different crops.

local function pickproduct(inst)

    
    local total_w = 0
    for k,v in pairs(VEGGIES) do
        total_w = total_w + (v.seed_weight or 1)
    end
    
    local rnd = math.random()*total_w
    for k,v in pairs(VEGGIES) do
        rnd = rnd - (v.seed_weight or 1)
        if rnd <= 0 then
            return k
        end
    end
    
    return "carrot"
end

 

Here is what I was trying -- it doesn't work (that I can tell) but it isn't crashing the game either.... so its either not doing anything or doing something that doesn't matter.

This was placed in my modmain.lua

NEWVEGGIES =
{
    crop1 = MakeVegStats(RARE,    TUNING.CALORIES_SMALL,    TUNING.HEALING_SMALL,    TUNING.PERISH_FAST, 0,
                                    TUNING.CALORIES_SMALL,    TUNING.HEALING_MEDSMALL,    TUNING.PERISH_SUPERFAST, TUNING.SANITY_SUPERTINY),
    
    crop2 = MakeVegStats(UNCOMMON,    TUNING.CALORIES_SMALL,    TUNING.HEALING_SMALL,    TUNING.PERISH_FAST, 0,
                                    TUNING.CALORIES_TINY,    TUNING.HEALING_TINY,    TUNING.PERISH_SUPERFAST, 0),
                                
    crop3 = MakeVegStats(RARE,    TUNING.CALORIES_TINY,    TUNING.HEALING_SMALL,    TUNING.PERISH_FAST, 0,
                                    TUNING.CALORIES_SMALL,    TUNING.HEALING_MEDSMALL*2,    TUNING.PERISH_SUPERFAST, TUNING.SANITY_SUPERTINY*2),
}

 

for i, v in ipairs(NEWVEGGIES) do table.insert (VEGGIES, i, v) end

 

If anyone can tell me how to make this work I would appreciate it.

My knowledge of lua is basically nil.

Link to comment
Share on other sites

Ok - So I am attempting a new approach...

I thought why not give just a flat chance to override the game crop picking function and grow one of my crops

 

The error I am getting is

...s/common/dont_starve/data/../mods/waiter/modmain.lua:188: attempt to index field 'seeds' (a nil value)
LUA ERROR stack traceback:

 

Here is the relevant code from my modmain.lua

 

require = "prefabs/seeds"

function wpicker(inst)

    local oldpicker = inst.components.seeds.pickproduct -- This is Line 188

-- this line is supposed to create a copy of the pickproduct function from seeds.lua

    inst.components.seeds.pickproduct = function(inst)
-- this line is supposed to create a new pickproduct function

   
        local wrnd = math.random()*10
    
    if wrnd <=0.5 then return "crop1"
        else if wrnd <=1.5 then return "crop2"
            else if wrnd <=2 then return "crop3"

-- the above should give a random (20% chance) to use on of my custom crops

                else oldpicker(inst)
-- this line should run the original pickproduct function if one of my crops is not selected
        end
        end
    end
    
    end
    
end    

AddPrefabPostInit("seeds", wpicker)

-- This should add the wpicker function into seeds.lua

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.