Jump to content

Need help editing ACTIONS from modmain


Recommended Posts

I have been messing around with a character mod and want to allow ONLY THIS CHARACTER to pickup an in-game mob, similar to how Webber can pick up spiders. Currently, I have gotten so far as to allow everyone to pickup the mob while sleeping. I noticed that developers used...

ACTIONS.PICKUP.fn = function(act)  to limit picking up spiders to only webber via

        if (act.target:HasTag("spider") and act.doer:HasTag("spiderwhisperer")) and
           (act.target.components.follower.leader ~= nil and act.target.components.follower.leader ~= act.doer) then
            return false, "NOTMINE_SPIDER"
        end

In the end I want to do something very similar from modmain, but I do not know how to do it.  I was looking at modutil and thought perhaps AddAction or AddComponentAction might be the way to alter the current file, but looking at how others have used these utilities, I am not so certain. I also plan to put a check in place to verify ACTIONS.PICKUP.fn has not changed in patches as editing ACTIONS can likely cause trouble.  If you know another way to preform the same end result, let me know that too.

Reply if you can help, thx.

 

Link to comment
Share on other sites

Hello! ACTIONS.PICKUP.fn is a global function, meaning any script can mess with it, including us! What we'll want to do though to maintain compatibility is hook the function, instead of replacing it, that way even if Klei or another mod changes this value, it SHOULD still work fine.

In modmain.lua, we'll start by storing the current version of the pickup function in our own new variable, oldpickupfn, like this

local oldpickupfn = GLOBAL.ACTIONS.PICKUP.fn -- Need to specify global here since we're in modmain

Now that we have that stored, we'll replace the current one outright. In my example below I made it so the pickup action fn prints something to console, then runs the old one we stored inside oldpickupfn.

-- Hook into ACTIONS.PICKUP.fn
GLOBAL.ACTIONS.PICKUP.fn = function(act)
    -- Run stuff before any normal pickup code would run
    print("We've hooked pickup!")

    oldpickupfn(act) -- Run the old pickup fn
end

 

You can put any code you want in place of where I've put the print, just remember that you want to code it to prevent others from picking up your mob, not allow your character, as oldpickupfn needs to run in order to properly pick up an item.

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...