Jump to content

Growable Crop Seeds


Recommended Posts

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

 

 

I would prefer a solution that wouldn't disrupt other people's attempts to do the same task with other mods.

 

Link to comment
Share on other sites

Here is what I have -- 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 is in the 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

pickproduct is a local function, not a child of the seed component (In fact, there is no such thing as the seeds component).
 
You can see that the seeds prefab assigns its pickproduct function to its plantable component's product.
inst.components.plantable.product = pickproduct

You can access and override that in the same way.

local old_pick_funct = inst.components.plantable.productinst.components.plantable.product = your_pick_funct

But I don't think you'll need to do that, the code below worked for me.

_G = GLOBALTUNING = _G.TUNINGrequire "prefabs/veggies"local function MakeVegStats(seedweight, hunger, health, perish_time, sanity, cooked_hunger, cooked_health, cooked_perish_time, cooked_sanity)	return {		health = health,		hunger = hunger,		cooked_health = cooked_health,		cooked_hunger = cooked_hunger,		seed_weight = seedweight,		perishtime = perish_time,		cooked_perishtime = cooked_perish_time,		sanity = sanity,		cooked_sanity = cooked_sanity	}endlocal COMMON = 3local UNCOMMON = 1local RARE = .5local 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 key, val in pairs(NEWVEGGIES) do	_G.VEGGIES[key] = valendfor k, v in pairs(_G.VEGGIES) do	print(k, v)end

 

For future reference, remember that you can not access local variables or functions of another module (file). If there is no way to access them through children of the module, you must copy them to your own code.

More on variable scope in lua.

 

Link to comment
Share on other sites

 

pickproduct is a local function, not a child of the seed component (In fact, there is no such thing as the seeds component).
 
But I don't think you'll need to do that, the code below worked for me.
_G = GLOBALTUNING = _G.TUNINGrequire "prefabs/veggies"local function MakeVegStats(seedweight, hunger, health, perish_time, sanity, cooked_hunger, cooked_health, cooked_perish_time, cooked_sanity)	return {		health = health,		hunger = hunger,		cooked_health = cooked_health,		cooked_hunger = cooked_hunger,		seed_weight = seedweight,		perishtime = perish_time,		cooked_perishtime = cooked_perish_time,		sanity = sanity,		cooked_sanity = cooked_sanity	}endlocal COMMON = 3local UNCOMMON = 1local RARE = .5local 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 key, val in pairs(NEWVEGGIES) do	_G.VEGGIES[key] = valendfor k, v in pairs(_G.VEGGIES) do	print(k, v)end

 

For future reference, remember that you can not access local variables or functions of another module (file). If there is no way to access them through children of the module, you must copy them to your own code.

More on variable scope in lua.

 

 

Ok - wow thank that is good to know (I'm really new to lua and have no background in programming languages)

 

I used the code you suggested (above) in my modmain.lua and after 30 test plantings I am still not getting any of the new veggies. I even changed them all to common for testing and still the same old crops over and over.

 

You said the code worked for you, am I missing something?

Link to comment
Share on other sites

@MidrealmDM,

The VEGGIES variable was being overwritten by veggies.lua after you inserted your values.

Use AddSimPostInit to ensure that your values get inserted correctly when the world loads.

AddSimPostInit(function(inst)	for key, val in pairs(NEWVEGGIES) do		_G.VEGGIES[key] = val	endend)

I also tested the seed's plantable component's product function, which had the new values coming up occasionally as they should.

 

Edit: Whoops, mentioned myself, haha.

Link to comment
Share on other sites

@MidrealmDM,

The VEGGIES variable was being overwritten by veggies.lua after you inserted your values.

Use AddSimPostInit to ensure that your values get inserted correctly when the world loads.

 

Oodalolly!! That did it!

I have been working on this fakakta mod all weekend with nothing to show for it.

But that got me past the hurdle - I should make some leaping progress now. :lemo:

 

:happypigs: Thank you! Thank you! Thank you! :happypigs:

 

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.

×
  • Create New...