Jump to content

Recommended Posts

I want modify the grass grows in winter, I knew in grass prefab has the function "MakeNoGrowInWinter()"

I try to write down followings in the modmain.lua, but it cause an error: attempt to call global 'MakeNoGrowInWinter' (a nil value)

Can anyone help me? Thanks!

  1. local function GrassGrowInWinter(inst)
  2.     if MakeNoGrowInWinter(inst) then
  3.         MakeNoGrowInWinter(inst):Remove()
  4.     end
  5. end
  6. AddPrefabPostInit("grass", GrassGrowInWinter(inst))

this is nonsense :)
You do not remove any function. You change exisiting functions or add functions.
 

You have 2 options:
1) Copy the whole grass.lua , but remove the line with MakeNoGrowInWinter(inst). Then put the grass.lua in a prefabs folder into your mod. That way your mod will replace the old grass.lua with your new grass.lua.
Downside: When the game updates and made any changes to the original grass.lua, your grass.lua is not up to date anymore. Also it could cause problems with other mods that also cchange grass in any way. So not recommed if you don't know what you are doing.

2) Revert the changes done by the MakeNoGrowInWinter function. So first you search all the game files where this function is defined.
I found it in data\scripts\standardcomponents.lua.
It looks like this:

local function TogglePickable(pickable, iswinter)
    if iswinter then
        pickable:Pause()
    else
        pickable:Resume()
    end
end

function MakeNoGrowInWinter(inst)
    inst.components.pickable:WatchWorldState("iswinter", TogglePickable)
    TogglePickable(inst.components.pickable, TheWorld.state.iswinter)
end

Obviously the pickable component also handles the growth someone. Anyway, we will try to revert this.

So we have to do 2 things:
1) Do inst.components.pickable:Resume() to undo the TogglePickable.
2) Undo the WatchWorldState somehow.

2) -> Usually you would simply write: inst.components.pickable:StopWatchingWorldState("iswinter",TogglePickable)
[ "StopWatchingWorldState" found in data\scripts\entityscripts.lua]
Which would remove the TogglePickable from the winter watchlist. But unfortunately that TogglePickable function is "local" so we can not access it from our grass.lua ....
At the moment I don't know a good solution for that...
Instead of removing the TogglePickable you could try adding a new function to the watchlist, which is hopefully executed after the Toggle function.
So try:
inst.components.pickable:WatchWorldState("iswinter", function(inst) inst.components.pickable:Resume() end)

And hopefully, when winter starts, the game first executes the TogglePickable function, which will make "Pause()" and after that it calls your new function with does "Resume()" again.
Try it out and see if it works... although this solution is not that adviseable.

Edited by Serpens

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
×
  • Create New...