Jump to content

How to override defaults function?


Recommended Posts

Hello,

I was looking at the source, and wanted to implement a new verification on the function DoToolWork in "actions.lua".
Whats the correct way to make it work? I guess if I change the source and zip again, it wont work with steam, I think.

Link to comment
Share on other sites

the easiest solution would be to place your new "actions.lua" into same-named directory in your mod. That way your actions.lua would override the game file and you can upload it to steam.
The downside is, that your new actions.lua would also override the changes from all other mods for actions which were loaded before your mod. And if someone else also has a mod that replaces the actions.lua, your and this mod can not work together obviously.

So it is better to use things like "ComponentPostInit" or "ClassPostConstruct". But I never changed things in actions.lua so far, so I'm not sure how to change functions from that file..

Edited by Serpens
Link to comment
Share on other sites

43 minutes ago, Serpens said:

the easiest solution would be to place your new "actions.lua" into same-named directory in your mod. That way your actions.lua would override the game file and you can upload it to steam.
The downside is, that your new actions.lua would also override the changes from all other mods for actions which were loaded before your mod. And if someone else also has a mod that replaces the actions.lua, your and this mod can not work together obviously.

So it is better to use things like "ComponentPostInit" or "ClassPostConstruct". But I never changed things in actions.lua so far, so I'm not sure how to change functions from that file..

But if I create a new actions.lua, I can override just one function? or it will override the whole file and I'll need to copy the entire file?


ps. I'm looking at PostConstruct, and I think it should be the better way of making it, override just the function I want inside the PostConstruct should work fine right?

Edited by caioketo
Link to comment
Share on other sites

you should be able to do this in modmain:

local function DoToolWork(act, workaction)
    if act.target.components.workable ~= nil and
        act.target.components.workable:CanBeWorked() and
        act.target.components.workable.action == workaction then
        act.target.components.workable:WorkedBy(
            act.doer,
            (   act.invobject ~= nil and
                act.invobject.components.tool ~= nil and
                act.invobject.components.tool:GetEffectiveness(workaction)
            ) or
            (   act.doer ~= nil and
                act.doer.components.worker ~= nil and
                act.doer.components.worker:GetEffectiveness(workaction)
            ) or
            1
        )
    end
    return true
end

GLOBAL.ACTIONS.CHOP.fn = function(act)
    return DoToolWork(act, ACTIONS.CHOP)
end

GLOBAL.ACTIONS.MINE.fn = function(act)
    return DoToolWork(act, ACTIONS.MINE)
end

GLOBAL.ACTIONS.HAMMER.fn = function(act)
    return DoToolWork(act, ACTIONS.HAMMER)
end

GLOBAL.ACTIONS.DIG.fn = function(act)
    return DoToolWork(act, ACTIONS.DIG)
end

and then edit the first function

Link to comment
Share on other sites

9 minutes ago, caioketo said:

But if I create a new actions.lua, I can override just one function? or it will override the whole file and I'll need to copy the entire file?

it will override the whole file yes.

But there is another way I saw in one of recezibs mods:
Instead of using the ComponentPostIniti command he does:

local Domesticatable = require("components/domesticatable")

function Domesticatable:OnEat(inst, data)
	--...
end

eg in modmain.
That way you first load the whole file, using the require and then altering only some of the functions, instead of all.

But I'm sure Aquaterion already posted a better solution while I wrote this :D

Edited by Serpens
Link to comment
Share on other sites

7 minutes ago, Aquaterion said:

you should be able to do this in modmain:


local function DoToolWork(act, workaction)
    if act.target.components.workable ~= nil and
        act.target.components.workable:CanBeWorked() and
        act.target.components.workable.action == workaction then
        act.target.components.workable:WorkedBy(
            act.doer,
            (   act.invobject ~= nil and
                act.invobject.components.tool ~= nil and
                act.invobject.components.tool:GetEffectiveness(workaction)
            ) or
            (   act.doer ~= nil and
                act.doer.components.worker ~= nil and
                act.doer.components.worker:GetEffectiveness(workaction)
            ) or
            1
        )
    end
    return true
end

GLOBAL.ACTIONS.CHOP.fn = function(act)
    return DoToolWork(act, ACTIONS.CHOP)
end

GLOBAL.ACTIONS.MINE.fn = function(act)
    return DoToolWork(act, ACTIONS.MINE)
end

GLOBAL.ACTIONS.HAMMER.fn = function(act)
    return DoToolWork(act, ACTIONS.HAMMER)
end

GLOBAL.ACTIONS.DIG.fn = function(act)
    return DoToolWork(act, ACTIONS.DIG)
end

and then edit the first function

That's exactly what I need, thanks so much!


But, if any other mod modifies this functions it will not work right? I guess its fine for now. thanks

Edited by caioketo
Link to comment
Share on other sites

When I return false in this function it will always says "Cant do that". Is there a way to add new reason for failing?
I saw that it uses data.reason to send the string, but can't find where should I change it, thanks in advanced.

Link to comment
Share on other sites

Sorry for digging up the old thread.
But in case anyone curious about the last question of OP, just search NOTMASTERCHEF.
When you return false in action just also return similar string name like this.
 

return false, NOTCOOLENOUGH 

Then edit speech of similar place, in this case, in STORE and RUMMAGE.

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