JudgeDeaths Posted January 27, 2020 Share Posted January 27, 2020 I'm trying to make my modded character have a damage multiplier when equipped with a basic spear. I'm pretty new to coding for Don't Starve, and lua in general but my understanding is that in order for the character to have this kind of interaction with an item already in the game, I'll have to add the script to my Modmain.lua and not my prefab character.lua however I've been unsuccessful thus far. I tried to add this code for a sanity off a tutorial here increase while equipped with a spear to see if I could maybe piggyback off this and change it to something like inst.components.combat.damagemultiplier = 1.25 AddPrefabPostInit("Spear", function(inst) if GetPlayer().prefab == "MyCharacter" then inst:AddComponent("dapperness") inst.components.dapperness.dapperness = TUNING.DAPPERNESS_MED inst.components.equippable.dapperness = TUNING.DAPPERNESS_MED end end) However I can't even get that to work. I saw that it might be due to coding for DS working differently than DST which this mod is for. Could someone point me in the right direction? Link to comment Share on other sites More sharing options...
Desblat Posted January 27, 2020 Share Posted January 27, 2020 AddprefabPostInit function procs ONLY ONE TIME - once item is created in the world. GetPlayer() doesnt work in DST. Here is an approach for adding Health regen to char if he has specific item equiped. There is no such a function currently, you will need to make it by yourself. You will need to track player equiping the item and start a task, if player unequips item you cancel task. This goes into OnEquip code: --- 1st we must remove regen task if there is one already so we don't create memory leak if owner.item_regen_task then owner.item_regen_task:Cancel() owner.item_regen_task = nil end ---- 2nd add regen local period = 1 owner.item_regen_task = owner:DoPeriodicTask(period, function() ownder.components.health:DoDelta(1) end) This goes into OnUnEquip code: ---- remove regen if owner.item_regen_task then owner.item_regen_task:Cancel() owner.item_regen_task = nil end 1 Link to comment Share on other sites More sharing options...
JudgeDeaths Posted January 27, 2020 Author Share Posted January 27, 2020 Thanks for taking the time. So just to clarify for myself, I've taken some time to look through the scripts that are already in the game and I see the OnEquip/OnUnEquip codes often placed on the items themselves does that same code work on a player? Since I'm not trying to create a new item, but simply modify my character when using an already existing item (ie: spear) would this be code be placed in my character's prefab.lua or modmain.lua. Sorry if these are rudimentary questions. It seems like the easier solution would be to simply make a new spear that he does more damage with, but I want him to have this interaction with the default spear and Wigfrid's spear. Link to comment Share on other sites More sharing options...
Desblat Posted January 28, 2020 Share Posted January 28, 2020 (edited) 23 hours ago, JudgeDeaths said: Thanks for taking the time. So just to clarify for myself, I've taken some time to look through the scripts that are already in the game and I see the OnEquip/OnUnEquip codes often placed on the items themselves does that same code work on a player? Since I'm not trying to create a new item, but simply modify my character when using an already existing item (ie: spear) would this be code be placed in my character's prefab.lua or modmain.lua. Sorry if these are rudimentary questions. It seems like the easier solution would be to simply make a new spear that he does more damage with, but I want him to have this interaction with the default spear and Wigfrid's spear. On 27.01.2020 at 3:19 AM, JudgeDeaths said: AddPrefabPostInit("spear", function(inst) end) Note that prefab name should be in lower case. This function can be used to replace OnEquip functions of objects. This goes into modmain.lua : local function MyOnEquipFunction(inst, owner) --- owner is player --- inst is item ---- 1 following code makes item appear in hands visually: owner.AnimState:OverrideSymbol("swap_object", "swap_des_qstaff", "swap_des_qstaff") owner.AnimState:Show("ARM_carry") owner.AnimState:Hide("ARM_normal") ---- ---- 2nd here you can put my code for health regen, but use it for sanity restoration instead ---- something ----- end) AddPrefabPostInit("spear", function(inst) --- 1 here we replace old OnEquip and OnUnEquip functions used by spear with our function: inst.components.equippable:SetOnEquip(MyOnEquipFunction) inst.components.equippable:SetOnUnequip(MyOnUnequipFunction) --- end) --------------------------------------------------------- Don't forget to remove your buff using MyOnUnequip function. Edited January 28, 2020 by Desblat 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now