Jump to content

How to patch other creator's mod?


SenL

Recommended Posts

Hi,

 

How do you create a mod to patch other creator's mod?

In this case, I'm patching "x"

 

Content of x.lua:


...

local canteat = { meatballs = true, fishsticks = true, baconeggs = true, monsterlasagna = true, monstermeat = true, cookedmonstermeat = true, monstermeat_dried = true }

...

local function canonlywearcertainhat(inst)

  local _Equip = inst.components.inventory.Equip

  inst.components.inventory.Equip = function(self, item, old_to_active)

  if not item or not item.components.equippable or not item:IsValid() then

    return

  end

  if item.components.equippable.equipslot == EQUIPSLOTS.HEAD then

    if not (item.prefab == "certainhat") then

      self:DropItem(item)

      self:GiveItem(item)

      return

    end

  end

  return _Equip(self, item, old_to_active)

  end

end

 

local function cannotcertainfood(inst)

  inst.components.eater.caneattest = function(self, food)

  if food ~= nil and canteat[food.prefab] then

    return false

  end

  return true

end

end

...

local fn = function(inst)

  ...

  inst.components.combat.damagemultiplier = 1.5

  ...

  canonlywearcertainhat(inst)

  cannotcertainfood(inst)

  ...

end

 

return MakePlayerCharacter("x", prefabs, assets, fn, start_inv)


 

Here is my patch:


function xPreNerfed(inst)

  inst.components.combat.damagemultiplier = 2.0 --was nerfed to 1.5

  inst.components.eater.caneattest = function() return true end

end

 

AddPrefabPostInit("x", xPreNerfed)


 

So far so good but I can't patch the "canonlywearcertainhat" bit.

How do you do that one?

 

Thanks!

Link to comment
Share on other sites

@SenL The code is kinda messy, but it seems like it's just overwriting this function:

inst.components.inventory.Equip

You can just overwrite it again yourself in the same way.

local old_Equip = inst.components.inventory.Equip -- in case you need itinst.components.inventory.Equip = function(self, item, old_to_active)    -- ...end
Link to comment
Share on other sites

Edit:

It doesn't seem to work.

 

Another question: Is it possible to overwrite a function of other mod?

 

Reason: this character gains sanity for killing butterfly (it's fine) but the character says a bad word (not really a fan).

 

Link to comment
Share on other sites

Priority of the original mod is 0.00493284025 

Do I put mine after or before?

I use -1 and it seems to load after.

 

I ended up copying the content of Equip function in the inventory.lua. It's definitely not the way to go but it works.

I'm not sure how else to patch this so that the character could wear any hats, because the mod already "wraps" the original inventory:Equip function.

Link to comment
Share on other sites

Patching another mod is actually really easy - make sure that in the priority list, your patch-mod is loaded after theirs. Basically, all you need to do is put your own version of the thing you want to patch as your mod.

 

For example, if I wanted to alter the stats found in someone's CharacterX.lua, I make my mod with a copy of that CharacterX.lua file, with the changes added. Everything else will be loaded from the original mod, but since the patch was loaded after, it will replace the loaded lua file.

 

I've created patches for my own mod:

Base http://steamcommunity.com/sharedfiles/filedetails/?id=179124884

Text patch http://steamcommunity.com/sharedfiles/filedetails/?id=220011987

Costume and stat patch http://steamcommunity.com/sharedfiles/filedetails/?id=227043510

 

For a long time, I've been wanting to make a template for translation patches for my mod items' names, descriptions, and examine text.

Link to comment
Share on other sites

Patching another mod is actually really easy - make sure that in the priority list, your patch-mod is loaded after theirs. Basically, all you need to do is put your own version of the thing you want to patch as your mod.

 

For example, if I wanted to alter the stats found in someone's CharacterX.lua, I make my mod with a copy of that CharacterX.lua file, with the changes added. Everything else will be loaded from the original mod, but since the patch was loaded after, it will replace the loaded lua file.

 

I've created patches for my own mod:

Base http://steamcommunity.com/sharedfiles/filedetails/?id=179124884

Text patch http://steamcommunity.com/sharedfiles/filedetails/?id=220011987

Costume and stat patch http://steamcommunity.com/sharedfiles/filedetails/?id=227043510

 

For a long time, I've been wanting to make a template for translation patches for my mod items' names, descriptions, and examine text.

 

Hi, I love your links mods.

Back to topic: I actually learned from how you mod your own links. However let's say the one you want to mod is already modifying (wrapping) core function (like inst.components.inventory.Equip) ... how do you undo this wrapping?

 

Example:

 

local _equip = inst.components.inventory.Equip

inst.components.inventory.Equip = ... -- can only wear certain hat

...

 

 

 

Thanks.

Link to comment
Share on other sites

All I'd do is patch the mod with a duplicate of the .lua in question with the part you don't want deleted or commented out.

With the right priority order, it will load the edited version with the part you don't like removed, and thus it will never try to run that little function.

 

Think of it like, if a teacher wanted to update the page of a book, in class.

All the students have a copy of the book, but the teacher has prepared an alternate Page 7 and given it to the students.

When the good little girls and boys reach Page 7, they know to read the new page, not the one that was in the original book. Then they can continue on to page 8 in the book like normal!

(I know that sounds a little contrived, but it's basically how the patching works.)

Link to comment
Share on other sites

I see, a duplicate of the lua file with part I don't want deleted/commented out.

 

What if the author of the book added something new/changed something in page 7?

Won't your (the teacher's) page 7 be out of date?

Link to comment
Share on other sites

I see, a duplicate of the lua file with part I don't want deleted/commented out.

 

What if the author of the book added something new/changed something in page 7?

Won't your (the teacher's) page 7 be out of date?

 

Yes, that is a problem - you must keep the altered duplicate up to date yourself.

It's a tad inelegant, but very simple. Just make sure you update yours when theirs is updated. (If that file changes at all.) =)

Link to comment
Share on other sites

@SenL Alternately..

-- In modmain where inst is the instance of whatever you're modifying-- e.g. in AddSimPostInit(function(inst)  end)inst.components.inventory.Equip = function(self, item, old_to_active)    -- Original Equip function (which was replaced by the mod)end

would only affect that function. You won't have to update it if that particular function (Equip) isn't changed in newer versions.

 

Edit: Reading above, I think you already tried this and it worked? If so, disregard this post.

Link to comment
Share on other sites

@SenL Alternately..

-- In modmain where inst is the instance of whatever you're modifying-- e.g. in AddSimPostInit(function(inst)  end)inst.components.inventory.Equip = function(self, item, old_to_active)    -- Original Equip function (which was replaced by the mod)end

would only affect that function. You won't have to update it if that particular function (Equip) isn't changed in newer versions.

 

Edit: Reading above, I think you already tried this and it worked? If so, disregard this post.

 

I tried that and didn't work. Having a duplicate (then patched) lua file and set the priority to -1 (original mod priority is 0.00something) seems to work.

 

I would like to know if it's possible to overwrite (update) a modded function (like that one, inventory.Equip) and make the function behave as original (unmodded).

 

Example (same as first post)

local _Equip = inst.components.inventory.Equip
  inst.components.inventory.Equip = function(self, item, old_to_active)
  if not item or not item.components.equippable or not item:IsValid() then
    return
  end
  if item.components.equippable.equipslot == EQUIPSLOTS.HEAD then
    if not (item.prefab == "certainhat") then
      self:DropItem(item)
      self:GiveItem(item)
      return
    end
  end
  return _Equip(self, item, old_to_active)
  end
...
This one overwrote the original inventory.Equip. If the headgear is not "certainhat" then un-equip else call old/original Equip function.
How do you update this function to allow all headgears to be worn again?
 
Sorry for repeat question.
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...