Casciel Posted July 23, 2017 Share Posted July 23, 2017 I've been trying to connect a custom weapon's damage to the current sanity of the character using the weapon. However, the game keeps crashing on the second line of this code ThePlayer:ListenForEvent("sanitydelta", function(inst, data) inst.components.weapon:SetDamage(1 * (ThePlayer.components.sanity.current)) end) The crash report says "attempt to index field 'weapon'(a nil value)" That line of code works fine when I put it in a "SetOnAttack" function but since that function is only called after attacking it stores the sanity value from the last time the player used this weapon to attack and I need it to do the damage that is equal to the player's current sanity at the time of attack. Other info: I've been writing the code in the weapon's file. I don't know if that is wrong but I don't see why it would be, though I'm not incredibly experienced in programming so maybe that is part of it? Link to comment Share on other sites More sharing options...
ZupaleX Posted July 23, 2017 Share Posted July 23, 2017 Hi, weapon is not a component. And the listener is the player os in your case inst would be the player If you want to retrieve the weapon it should be something along these lines inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS) that's under the condition this piece of code is executed on the server, otherwise components.inventory would not exists Link to comment Share on other sites More sharing options...
Casciel Posted July 23, 2017 Author Share Posted July 23, 2017 Thank you, it actually works now! Link to comment Share on other sites More sharing options...
Kronas Posted July 23, 2017 Share Posted July 23, 2017 4 hours ago, Casciel said: Thank you, it actually works now! wait so explain me what that code do and can you show me the full code please ? that can help me out too ;w; Link to comment Share on other sites More sharing options...
Casciel Posted July 24, 2017 Author Share Posted July 24, 2017 Quote wait so explain me what that code do and can you show me the full code please ? that can help me out too ;w; I know how you feel, too many times I've found an old forum post that looked like it would help but the post ended before I knew what was going on. Even now I'm still a bit confused about everything I'm doing but slowly getting better. I'm not one who should really be teaching because of that reason but if anyone comes along and is able to correct or explain anything better. Even point out flaws in my logic, that would be appreciated. So what I was doing was creating a magical spell book that does damage based on the player's current sanity. So if their sanity is 100 it should do 100 damage and if their sanity is 5 it should only do 5 damage. The part I had an issue with in this post was getting the game to constantly change the value of damage the weapon did as the player's sanity changed. After getting help here that is no longer a problem. Seems all I had to do was change " inst " to "ThePlayer" in the argument for the "ListenForEvent" function. I kept the other code they mentioned commented out because I can imagine how to use that to fix any issues that I may come across if I do. However, for the moment, everything seems to run fine as it is. The part you are looking for ThePlayer:ListenForEvent("sanitydelta", function(ThePlayer, data) --inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS) inst.components.weapon:SetDamage(1 * (ThePlayer.components.sanity.current)) end) So, as far as I know, and if anyone knows better please don't hesitate to correct this, but from my knowledge. "ListenForEvent" tells the game to look for "sanitydelta" so a change in the sanity, and because later in the code I added a weapon component to my item, it then uses the "SetDamage" function to change the damage of the item when used as a weapon to what is in the brackets "(1 * (ThePlayer.components.sanity.current))". The "(1 * " doesn't really do anything, it is just there because it prevents further issues. However "ThePlayer.components.sanity.current" tells the game to get the player's current sanity and puts it in the brackets and finally changes the weapon's damage altogether. You also requested the full code. I hope this helps and don't trust me one hundred percent because I'm still learning too and for all I know I might find I'm just pulling the sanity of the host and not actually the player who is holding the item and may have more problems to fix later, but as of now it works just fine when playing alone. local assets= { Asset("ANIM", "anim/forbiddentome.zip"), Asset("ANIM", "anim/swap_forbiddentome.zip"), Asset("ATLAS", "images/inventoryimages/forbiddentome.xml"), Asset("IMAGE", "images/inventoryimages/forbiddentome.tex"), --Asset("ANIM", "anim/books.zip"), Asset("ANIM", "anim/lightning.zip"), } local prefabs = { } local function fn(colour) local function OnEquip(inst, owner) owner.AnimState:OverrideSymbol("swap_object", "swap_forbiddentome", "forbiddentome") 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 inst = CreateEntity() local trans = inst.entity:AddTransform() local anim = inst.entity:AddAnimState() MakeInventoryPhysics(inst) anim:SetBank("forbiddentome") anim:SetBuild("forbiddentome") anim:PlayAnimation("idle") inst:AddComponent("inventoryitem") inst.components.inventoryitem.imagename = "forbiddentome" inst.components.inventoryitem.atlasname = "images/inventoryimages/forbiddentome.xml" inst:AddComponent("equippable") inst.components.equippable:SetOnEquip( OnEquip ) inst.components.equippable:SetOnUnequip( OnUnequip ) inst.components.equippable.dapperness = 4 * TUNING.CRAZINESS_MED MakeHauntableLaunch(inst) inst:AddTag("irreplaceable") inst:AddComponent("inspectable") -- ThePlayer:ListenForEvent("sanitydelta", function(ThePlayer, data) --inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS) inst.components.weapon:SetDamage(1 * (ThePlayer.components.sanity.current)) end) -- local function onattack_bolt(inst, attacker, victim, proxy) local pt = victim:GetPosition() local num_lightnings = 1 ThePlayer:ShakeCamera(CAMERASHAKE.FULL, .7, .02, .5, proxy, 40) attacker.components.sanity:DoDelta(-35) attacker:StartThread(function(pos) for k = 0, num_lightnings do local pos = pt local inst = CreateEntity() TheWorld:PushEvent("screenflash", .5) inst:AddTag("FX") inst.entity:SetCanSleep(false) inst.persists = false inst.entity:AddTransform() inst.entity:AddAnimState() inst.entity:AddLight() inst.Transform:SetPosition(pos:Get()) inst.Transform:SetScale(2, 2, 2) inst.entity:AddSoundEmitter() inst.SoundEmitter:PlaySound("dontstarve/rain/thunder_close") inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh") inst.AnimState:SetLightOverride(1) inst.AnimState:SetBank("lightning") inst.AnimState:SetBuild("lightning") inst.AnimState:PlayAnimation("anim") inst:ListenForEvent("animover", inst.Remove) end end) return true end inst:AddComponent("weapon") inst.components.weapon:SetDamage(1 * (ThePlayer.components.sanity.current)) inst.components.weapon:SetRange(20, 20) inst.components.weapon:SetOnAttack(onattack_bolt) return inst end return Prefab("common/inventory/forbiddentome", fn, assets, prefabs) Link to comment Share on other sites More sharing options...
Draukel Posted September 15, 2017 Share Posted September 15, 2017 I guess I can make the same thing but not with weapon especially. I mean with global damage. Hit stronger when the sanity is low. I guess I can do Quote ThePlayer:ListenForEvent("sanitydelta", function(ThePlayer, data) inst.components.combat.damagemultiplier(1 * (ThePlayer.components.sanity.current)) end) But I think I'm wrong, something seem empty in it, but I don't find what. 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