WinterStar Posted January 16, 2018 Share Posted January 16, 2018 A friend of mine helped me create a character mod in which every time you eat you get a grenade type item but the problem is: Each time i eat something it keeps giving more and more items Example: When eating 20 steaks in a row, you get 20 grenades on the 20th time instead of just one This is making my character too op and i dont know much about DST scripting so i need help I want just ONE grenade to appear each time my character eats Spoiler local yoshibomb_assets = { Asset("ANIM", "anim/yoshibomb.zip"), Asset("ANIM", "anim/swap_yoshibomb.zip"), Asset("ATLAS", "images/inventoryimages/yoshibomb.xml"), Asset("IMAGE", "images/inventoryimages/yoshibomb.tex"), } local yoshibomb_prefabs = { "explode_small", } local function DoExplode(self) local explosiverange = 5 local stacksize = self.inst.components.stackable ~= nil and self.inst.components.stackable:StackSize() or 1 local totaldamage = self.explosivedamage * stacksize local x, y, z = self.inst.Transform:GetWorldPosition() -- Players are off limits now local ents = TheSim:FindEntities(x, y, z, explosiverange, nil, { "INLIMBO", "player" }) for i, v in ipairs(ents) do if v ~= self.inst and v:IsValid() and not v:IsInLimbo() then if v.components.workable ~= nil and v.components.workable:CanBeWorked() then v.components.workable:WorkedBy(self.inst, self.buildingdamage) end --Recheck valid after work if v:IsValid() and not v:IsInLimbo() then if self.lightonexplode and v.components.fueled == nil and v.components.burnable ~= nil and not v.components.burnable:IsBurning() and not v:HasTag("burnt") then v.components.burnable:Ignite() end if v.components.combat ~= nil then v.components.combat:GetAttacked(self.inst, totaldamage, nil) end v:PushEvent("explosion", { explosive = self.inst }) end end end end local function OnExplodeFn(inst) SpawnPrefab("explode_small").Transform:SetPosition(inst.Transform:GetWorldPosition()) DoExplode(inst.components.explosive) end local function OnHitWater(inst, attacker, target) inst.components.explosive:OnBurnt() end local function common_fn(bank, build, anim, tag, isinventoryitem) local inst = CreateEntity() inst.entity:AddTransform() inst.entity:AddAnimState() inst.entity:AddSoundEmitter() inst.entity:AddNetwork() if isinventoryitem then MakeInventoryPhysics(inst) else inst.entity:AddPhysics() inst.Physics:SetMass(1) inst.Physics:SetCapsule(0.2, 0.2) inst.Physics:SetFriction(0) inst.Physics:SetDamping(0) inst.Physics:SetCollisionGroup(COLLISION.CHARACTERS) inst.Physics:ClearCollisionMask() inst.Physics:CollidesWith(COLLISION.WORLD) end if tag ~= nil then inst:AddTag(tag) end inst.AnimState:SetBank(bank) inst.AnimState:SetBuild(build) inst.AnimState:PlayAnimation(anim, true) inst.entity:SetPristine() if not TheWorld.ismastersim then return inst end inst:AddComponent("locomotor") inst:AddComponent("complexprojectile") return inst end local function onequip(inst, owner) owner.AnimState:OverrideSymbol("swap_object", "swap_yoshibomb", "swap_yoshibomb") owner.AnimState:Show("ARM_carry") owner.AnimState:Hide("ARM_normal") end local function onunequip(inst, owner) owner.AnimState:Hide("ARM_carry") owner.AnimState:Show("ARM_normal") end local function onthrown(inst) inst:AddTag("NOCLICK") inst.persists = false inst.AnimState:PlayAnimation("spin_loop", true) inst.Physics:SetMass(1) inst.Physics:SetCapsule(0.2, 0.2) inst.Physics:SetFriction(0) inst.Physics:SetDamping(0) inst.Physics:SetCollisionGroup(COLLISION.CHARACTERS) inst.Physics:ClearCollisionMask() inst.Physics:CollidesWith(COLLISION.WORLD) inst.Physics:CollidesWith(COLLISION.OBSTACLES) inst.Physics:CollidesWith(COLLISION.ITEMS) end local function ReticuleTargetFn() local player = ThePlayer local ground = TheWorld.Map local x, y, z --Attack range is 8, leave room for error --Min range was chosen to not hit yourself (2 is the hit range) for r = 6.5, 3.5, -.25 do x, y, z = player.entity:LocalToWorldSpace(r, 0, 0) if ground:IsPassableAtPoint(x, y, z) then return Vector3(x, y, z) end end return Vector3(x, y, z) end local function yoshibomb_fn() local inst = common_fn("yoshibomb", "yoshibomb", "idle", "projectile", true) inst:AddComponent("reticule") inst.components.reticule.targetfn = ReticuleTargetFn inst.components.reticule.ease = true if not TheWorld.ismastersim then return inst end inst:AddComponent("explosive") inst.components.explosive:SetOnExplodeFn(OnExplodeFn) inst.components.explosive.explosivedamage = TUNING.GUNPOWDER_DAMAGE -- So explosion doesn't affect anything, including players inst.components.explosive.explosiverange = 0 inst.components.complexprojectile:SetHorizontalSpeed(15) inst.components.complexprojectile:SetGravity(-35) inst.components.complexprojectile:SetLaunchOffset(Vector3(.25, 1, 0)) inst.components.complexprojectile:SetOnLaunch(onthrown) inst.components.complexprojectile:SetOnHit(OnHitWater) inst:AddComponent("weapon") inst.components.weapon:SetDamage(0) inst.components.weapon:SetRange(8, 10) inst:AddComponent("inspectable") inst:AddComponent("inventoryitem") inst.components.inventoryitem.imagename = "yoshibomb" inst.components.inventoryitem.atlasname = "images/inventoryimages/yoshibomb.xml" inst:AddComponent("stackable") inst:AddComponent("equippable") inst.components.equippable:SetOnEquip(onequip) inst.components.equippable:SetOnUnequip(onunequip) inst.components.equippable.equipstack = true return inst end STRINGS.NAMES.YOSHIBOMB = "Yoshi Egg!" STRINGS.CHARACTERS.GENERIC.DESCRIBE.YOSHIBOMB = "Yoshi!" return Prefab("yoshibomb", yoshibomb_fn, yoshibomb_assets, yoshibomb_prefabs) Link to comment https://forums.kleientertainment.com/forums/topic/86500-need-help-with-bug-getting-an-item-when-eating-on-my-mod/ Share on other sites More sharing options...
Aquaterion Posted January 17, 2018 Share Posted January 17, 2018 The code you provided doesn't contain the code that is related to eating or spawning the grenade. Link to comment https://forums.kleientertainment.com/forums/topic/86500-need-help-with-bug-getting-an-item-when-eating-on-my-mod/#findComment-993545 Share on other sites More sharing options...
WinterStar Posted January 17, 2018 Author Share Posted January 17, 2018 sorry for that perhaps its this one? \Don't Starve Together\mods\Yoshi\scripts\prefabs\character.lua local MakePlayerCharacter = require "prefabs/player_common" local assets = { Asset("SCRIPT", "scripts/prefabs/player_common.lua"), } local prefabs = {} -- Custom starting items local start_inv = { } local function giveitem(inst) local item = SpawnPrefab("yoshibomb") inst.components.inventory:GiveItem(item) end local function oneat(inst, food) --if food and food.components.edible and food.components.edible.foodtype == "HELLO" then if food and food.components.edible then inst:ListenForEvent("oneat", giveitem) end end -- When the character is revived from human local function onbecamehuman(inst) -- Set speed when reviving from ghost (optional) inst.components.locomotor:SetExternalSpeedMultiplier(inst, "yoshi_speed_mod", 1) end local function onbecameghost(inst) -- Remove speed modifier when becoming a ghost inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "yoshi_speed_mod") end -- When loading or spawning the character local function onload(inst) inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman) inst:ListenForEvent("ms_becameghost", onbecameghost) if inst:HasTag("playerghost") then onbecameghost(inst) else onbecamehuman(inst) end end -- This initializes for both the server and client. Tags can be added here. local common_postinit = function(inst) -- Minimap icon inst.MiniMapEntity:SetIcon( "yoshi.tex" ) end -- This initializes for the server only. Components are added here. local master_postinit = function(inst) -- choose which sounds this character will play inst.soundsname = "yoshi" -- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used --inst.talker_path_override = "dontstarve_DLC001/characters/" -- Stats inst.components.health:SetMaxHealth(150) inst.components.hunger:SetMax(300) inst.components.sanity:SetMax(100) inst.components.eater:SetOnEatFn(oneat) -- Damage multiplier (optional) inst.components.combat.damagemultiplier = 1 -- Hunger rate (optional) inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE inst.OnLoad = onload inst.OnNewSpawn = onload end return MakePlayerCharacter("yoshi", prefabs, assets, common_postinit, master_postinit, start_inv) Link to comment https://forums.kleientertainment.com/forums/topic/86500-need-help-with-bug-getting-an-item-when-eating-on-my-mod/#findComment-993709 Share on other sites More sharing options...
Aquaterion Posted January 17, 2018 Share Posted January 17, 2018 ok your issue is here: local function giveitem(inst) local item = SpawnPrefab("yoshibomb") inst.components.inventory:GiveItem(item) end local function oneat(inst, food) --if food and food.components.edible and food.components.edible.foodtype == "HELLO" then if food and food.components.edible then inst:ListenForEvent("oneat", giveitem) end end the function oneat is being run every time the character eats, but instead of spawning a yoshibomb when its eaten, you added a listener, adding an extra listener each eat, that also checks when the player eats, and you're adding this each time, so every time he eats, it's running multiple times. The fix: --giveitem function is no longer required local function oneat(inst, food) if food and food.components.edible then local item = SpawnPrefab("yoshibomb") inst.components.inventory:GiveItem(item) end end Link to comment https://forums.kleientertainment.com/forums/topic/86500-need-help-with-bug-getting-an-item-when-eating-on-my-mod/#findComment-993721 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