Jump to content

Some help about Giant Crops with Auto Stack mod


Recommended Posts

(Sorry for my bad English, also I'm 100% new for lua

Giant crops will auto stack by mod, but when smash them they always give 3 crops and 2 seeds, don't matter how many giant crops has been stacked together.

 

My idea is let Giant crops work like stone fruit, I think this code is how stone fruit work:

-- Finally, remove the actual stack items we just consumed
    local top_stack_item = inst.components.stackable:Get(num_fruits_worked)
    top_stack_item:Remove()

This is how I understand it:
"stackable:Get(X)" can take X stuff from the stack then return a table,and "Remove()" will remove the last thing in that table.

I'm not sure which part really remove the thing in stack,but if "Remove()" are both in giant crop's ans stome fruit's code, so I think the answer is"Get()"

 

This is (at least I think) the code of giant crops:

local function oversized_onfinishwork(inst, chopper)
    inst.components.lootdropper:DropLoot()
    inst:Remove()
end

It should work when I add Get between DropLoot and Remove, but i don't know how to change a local function in veggies.lua by modmain.lua, I google it and most of them just say there is no way to do it.
So is there any stuff like an api that I can use in this case? Or can "Let it drop X times of crops and seeds when there are X giant crops stack together" accomplish?

(Sorry for my bad English again

Edited by HuangET
change some wrong word
Link to comment
Share on other sites

Hi, I'm fairly new in modding as well, but I've a made a mod that deals heavily with veggies and farm_plants scripts.

On 7/15/2022 at 3:19 PM, HuangET said:

It should work when I add Get between DropLoot and Remove, but i don't know how to change a local function in veggies.lua by modmain.lua, I google it and most of them just say there is no way to do it.

There is a way, actually, which I use in my mod. There's this "Upvalue Hacker" tool by Rezecib that allows you to edit local variables and functions that you can "trace" back to some accessible prefabs or objects. You can get it here:

https://github.com/rezecib/Rezecib-s-Rebalance/blob/master/scripts/tools/upvaluehacker.lua

So you would put upvaluehacker.lua in your mod folder's scripts/tools, add this line on top of your modmain.lua,

local UpvalueHacker = GLOBAL.require("tools/upvaluehacker")

and then you would be able to edit the local function you mentioned by something like this:

AddPrefabPostInit("world", function(inst)
    local function modded_oversized_onfinishwork(inst, chopper) --you can change the arguments, too
      ... --enter your desired function contents here
    end
    UpvalueHacker.SetUpvalue(GLOBAL.Prefabs.potato_oversized.fn, modded_oversized_onfinishwork, "oversized_onfinishwork") --even though it only says potato_oversized,
    --it would also affect all objects including all giant crops in veggies.lua
    --because you would change the local oversized_onfinishwork function that they all share
end)

This is just one way to do it. You can also save the original function locally and return it in addition to your custom function contents if you don't want to completely replace the original function as well.

Edited by GrowthMindset
GLOBAL.Prefabs instead of just Prefabs
Link to comment
Share on other sites

Thanks for helping and answer, but I got some more issue here, hope you don't mind that I ask more question.

This is what I writted in modmain.lua:

local UpvalueHacker = GLOBAL.require("tools/upvaluehacker")

AddPrefabPostInit("veggies", function(inst)
    local function modded_oversized_onfinishwork(inst, chopper) --you can change the arguments, too
		inst.components.lootdropper:DropLoot()
		top_stack_item = inst.components.stackable:Get(1)
		top_stack_item:Remove()
    end
	
    UpvalueHacker.SetUpvalue(Prefabs.potato_oversized.fn, modded_oversized_onfinishwork, "oversized_onfinishwork") 
	--even though it only says potato_oversized,
    --it would also affect all objects including all giant crops in veggies.lua
    --because you would change the local oversized_onfinishwork function that they all share
end)

At first I use "world" in AddPrefabPostInit but I can't start the server, it fixed after I changed it to "veggies".
I changed the function contents but I don't think it changed anything in game, so I write a new part that should changed the way to smash (destory?) from hammer to pickaxe so I can check the mod is work or not. The result it's not.

This is a part of what I writed for hammer to pickaxe (the full code is about 90 lines):

AddPrefabPostInit("veggies", function(inst)
    local function modded_fn_oversized()
    	--long codes, pass
	inst:AddComponent("workable")
	inst.components.workable:SetWorkAction(ACTIONS.MINE) --pickaxe
        inst.components.workable:SetOnFinishCallback(oversized_onfinishwork)
        inst.components.workable:SetWorkLeft(OVERSIZED_MAXWORK)
        --pass
    end
    
    UpvalueHacker.SetUpvalue(Prefabs.potato_oversized.fn, modded_fn_oversized, "fn_oversized") 
end)
    

I use "world" in AddPrefabPostInit but I can't start the server, so the mod itself is working. I'm not sure it's because I use the Upvalue Hacker in a wrong way or just wrong function cuntents.
My upvaluehacker.lua is installed in \scripts\tools, the way I installed it is creat a lua file called upvaluehacker and then copy the code into that file (I don't know how to install file on github)

  • Like 1
Link to comment
Share on other sites

Ahh, I'm so sorry. The reason why "world" doesn't work is because Prefabs is not defined, I forgot to define that.

You can add this on top of your file and it should work:

local Prefabs = GLOBAL.Prefabs

or, just add "GLOBAL." in the UpvalueHacker line instead:

UpvalueHacker.SetUpvalue(GLOBAL.Prefabs.potato_oversized.fn, modded_fn_oversized, "fn_oversized")

 

Edit: For you to be able to analyze bugs/errors on your own, I strongly recommend that you check the game's logs whenever your game crashes. For Windows, it's at Documents\Klei\DoNotStarveTogether\

while for Linux, it's at .klei/DoNotStarveTogether/ (server logs is master_server_log.txt)

Edited by GrowthMindset
tip
  • Like 1
Link to comment
Share on other sites

Thank you for helping! I success!

Now it will work like stone fruit.

--sorry for my bad English
local UpvalueHacker = GLOBAL.require("tools/upvaluehacker")

AddPrefabPostInit("world", function(inst)
    local function modded_oversized_onfinishwork(inst, chopper) --you can change the arguments, too
		--the workleft is 1 in the original code, it mean you only can smash it once. SetWorkLeft here is kind of "reset" the workleft.
		inst.components.workable:SetWorkLeft(1)
		inst.components.lootdropper:DropLoot()
		--take 1 from the stack, if there is 5 stack togather, Get and Remove will take one off and let it become 4
		top_stack_item = inst.components.stackable:Get(1)
		top_stack_item:Remove()
    end
	
    UpvalueHacker.SetUpvalue(GLOBAL.Prefabs.potato_oversized.fn, modded_oversized_onfinishwork, "oversized_onfinishwork") 
	--even though it only says potato_oversized,
    --it would also affect all objects including all giant crops in veggies.lua
    --because you would change the local oversized_onfinishwork function that they all share
end)

 

  • Like 1
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...