Batmanwarrior Posted January 21, 2015 Share Posted January 21, 2015 (edited) Hi, I have made a custom weapon that can heal the caster or target, and I'm planning on using the sanity as "mana". The code for the healfunction is:local function heal_func(inst, self, target) print(inst, target) local sanityPercent = self.components.sanity:GetPercent() local sanityMax = self.components.sanity.max() -- PROBLEM HERE! local caster = inst.components.inventoryitem.owner local tar = target or caster if not caster then caster = tar end if (sanityPercent * sanityMax) > 50 then self.components.sanity:DoDelta(-50) target.components.health:DoDelta(20) else --inst.components.talker:Say("I need more sanity.") inst.components.talker:Say("Sanity : ".. (sanityPercent * sanityMax)) endend I want a single line of code that checks the caster's current sanity, but I don't know how to access it from the weapon.lua. I thought self.components would do the trick. It seems to be working for the percent, but not for the max sanity. If you can help me with the current sanity, I don't have to do a workaround like I do now (percent * max). Thanks in advance! Edited January 21, 2015 by Batmanwarrior Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/ Share on other sites More sharing options...
Maris Posted January 21, 2015 Share Posted January 21, 2015 See scripts/components/sanity.lua file:local Sanity = Class(function(self, inst) self.inst = inst self.max = 100 self.current = self.max self.rate = 0 self.sane = true self.fxtime = 0 self.dapperness = 0 self.inducedinsanity = nil self.night_drain_mult = 1 self.neg_aura_mult = 1 self.penalty = 0 self.ghost_drain_mult = 0 self._oldissane = self:IsSane() self._oldpercent = self:GetPercent() self.inst:StartUpdatingComponent(self) self:Recalc(0)end,nil,{ max = onmax, current = oncurrent, rate = onrate, sane = onsane, inducedinsanity = oninducedinsanity, penalty = onpenalty, ghost_drain_mult = onghostdrainmult,})So use this:self.components.sanity.maxself.components.sanity.currentself.components.sanity:GetPercent()etcJust look at the source file and you will find all you need. Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-604366 Share on other sites More sharing options...
rezecib Posted January 21, 2015 Share Posted January 21, 2015 @Batmanwarrior, self holds the component object. In every component I can remember offhand, self.inst refers to the entity that has that component, so you want self.inst.components.sanity:GetPercent(). But if self.components is working in your code, then perhaps the component's self isn't actually getting passed in there as the argument. Where do you attach the heal_func? Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-604373 Share on other sites More sharing options...
Batmanwarrior Posted January 21, 2015 Author Share Posted January 21, 2015 Thanks Maris, I tried self.components.sanity.current() and self.components.sanity.max() with the extra unnecessary "()". It now works. BUT the function doesn't want to take the self-argument. So I had to make a new function(inst, self), but I don't know how to call that function inside the heal_func.. I tried:local function Sanitycheck(inst, self) print(inst, self) local CurrentSanity = self.components.sanity.current if CurrentSanity > 50 then self.components.sanity:DoDelta(-50) return 1 else return 2 endendSo I thought that if I type "local SanityLevel = SanityCheck(inst, self)" it would register the returned int 1 or 2, and then I could make a function like "if SanityLevel == 1 then", but it doesnt seem like the function return anything because it always goes straight to "else".. :/ Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-604402 Share on other sites More sharing options...
Batmanwarrior Posted January 21, 2015 Author Share Posted January 21, 2015 rezecib Hi, I put these lines in the fn():inst:AddComponent("spellcaster") inst.components.spellcaster:SetSpellFn(heal_func) inst.components.spellcaster.canuseontargets = true inst.components.spellcaster.canusefrominventory = true inst.components.spellcaster.canonlyuseonlocomotors = trueAnd the heal_func and Sanitycheck looks like this:local function Sanitycheck(inst, self) print(inst, self) local CurrentSanity = self.components.sanity.current if CurrentSanity > 50 then self.components.sanity:DoDelta(-50) return 1 else return 2 endendlocal function heal_func(inst, target) print(inst, target) local SanityLevel = Sanitycheck(inst, self) local caster = inst.components.inventoryitem.owner local tar = target or caster if not caster then caster = tar end if SanityLevel == 1 then target.components.health:DoDelta(20) else inst.components.talker:Say("I need more sanity.") endendI tried to add "self" as an argument to heal_func but the client doesnt like it. It keeps saying that self is undeclared or nil.The easiest way would be to add the self argument to the heal_func, but it doesn't seem to work that way.Both functions work but not at the same time.If I change SetSpellFn(heal_func) to SetSpellFn(Sanitycheck), then the Sanitycheck function works, but not the heal_func :/ Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-604403 Share on other sites More sharing options...
Maris Posted January 21, 2015 Share Posted January 21, 2015 First of all, start to learn Lua language. It's not so hard as you think. It takes me about 15 minutes. Your questions are mostly about Lua.(Lua in 15 minutes) Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-604412 Share on other sites More sharing options...
Batmanwarrior Posted January 21, 2015 Author Share Posted January 21, 2015 Maris Thanks, but I looked through the guide and I'm still stuck at the same problem. I'm new to programming and its hard to learn everything like that. I learn new stuff everyday, mostly by looking through other people's code or gamefiles. But right now I don't come much further, I need a quick hint about arguments to the function or some way to link those functions together. Hopefully rezecib can help me out when he gets back. Thanks anyways Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-604422 Share on other sites More sharing options...
Kzisor Posted January 21, 2015 Share Posted January 21, 2015 (edited) rezecib Hi, I put these lines in the fn():inst:AddComponent("spellcaster") inst.components.spellcaster:SetSpellFn(heal_func) inst.components.spellcaster.canuseontargets = true inst.components.spellcaster.canusefrominventory = true inst.components.spellcaster.canonlyuseonlocomotors = trueAnd the heal_func and Sanitycheck looks like this:local function Sanitycheck(inst, self) print(inst, self) local CurrentSanity = self.components.sanity.current if CurrentSanity > 50 then self.components.sanity:DoDelta(-50) return 1 else return 2 endendlocal function heal_func(inst, target) print(inst, target) local SanityLevel = Sanitycheck(inst, self) local caster = inst.components.inventoryitem.owner local tar = target or caster if not caster then caster = tar end if SanityLevel == 1 then target.components.health:DoDelta(20) else inst.components.talker:Say("I need more sanity.") endendI tried to add "self" as an argument to heal_func but the client doesnt like it. It keeps saying that self is undeclared or nil.The easiest way would be to add the self argument to the heal_func, but it doesn't seem to work that way.Both functions work but not at the same time.If I change SetSpellFn(heal_func) to SetSpellFn(Sanitycheck), then the Sanitycheck function works, but not the heal_func :/ In your sanity check function it should be inst.components.sanity not self.components.sanity as you aren't passing a "self" variable from the heal function. The self variable is only populated if you have self: or ComponentName: in front of the function name, otherwise Lua ignores passing the self variable. You could in theory remove any implemented variable for self in the Sanityheck function because it should also be nil. Edited January 21, 2015 by Kzisor Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-604476 Share on other sites More sharing options...
Batmanwarrior Posted January 21, 2015 Author Share Posted January 21, 2015 Hi Kzisor and thanks for looking into this. I tried to remove self, and replace it with inst. But then I got an error message telling me something about sanity being a nil value at this row: local CurrentSanity = inst.components.sanity.currentI also tried to pass self as an argument to the heal_func by adding self: before the functionname, but then i get an error message like this: [string "../mods/necrostaff/scripts/prefabs/necrostaff.lua"]:59: '(' expected near ':'LUA ERROR stack traceback:I also tried to put SpellCaster: before the heal_func but got the same results as above =/ Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-604590 Share on other sites More sharing options...
Kzisor Posted January 21, 2015 Share Posted January 21, 2015 @Batmanwarrior, if you wouldn't mind at this point I think it would be best if you uploaded the particular files associates with the necrostaff so that we can see exactly how you are using the functions. Without the code it's hard to tell you exactly what exactly what is happening as to not work correctly. Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-604592 Share on other sites More sharing options...
Batmanwarrior Posted January 21, 2015 Author Share Posted January 21, 2015 Kzisor Sure, no problem. Thanks necrostaff.lua Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-604597 Share on other sites More sharing options...
Kzisor Posted January 22, 2015 Share Posted January 22, 2015 (edited) @Batmanwarrior, try the following code. local function SanityCheck(inst, level) level = level or inst.components.sanity.current if level > 50 then inst.components.sanity:DoDelta(-50) return true endreturn falseendlocal function HealFunc(inst, target) print(inst, target) local caster = inst.components.inventoryitem.owner if not caster then caster = target or caster end if SanityCheck(inst) then inst.components.sanity:DoDelta(-50) target.components.health:DoDelta(20) else inst.components.talker:Say("I need more sanity.") endend Let me know if it works or not, I think it should work. Edited January 22, 2015 by Kzisor Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-604687 Share on other sites More sharing options...
Batmanwarrior Posted January 22, 2015 Author Share Posted January 22, 2015 Kzisor I tried to replace the current functions with your code but still got the "sanity is nil" error at this line:local function SanityCheck(inst, level) level = level or inst.components.sanity.current <---- THIS LINE if level > 50 thenMaybe the sanitycomponent isnt passed with inst-argument? Im not an expert at lua, but when I coded my custom character prefab inst worked fine for all components. =/ Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-604775 Share on other sites More sharing options...
Kzisor Posted January 22, 2015 Share Posted January 22, 2015 @Batmanwarrior, change this line for me and see if it works right. fromif SanityCheck(inst) then toif SanityCheck(caster.inst) then Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-604841 Share on other sites More sharing options...
Batmanwarrior Posted January 22, 2015 Author Share Posted January 22, 2015 KzisorNow it complains about inst being a nil value. "attempt to index local 'inst' (a nil value)" level = level or inst.components.sanity.currentOh man, this is really frustrating.. This is the last part for the staff, everything else works as intended. Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-604917 Share on other sites More sharing options...
DarkXero Posted January 23, 2015 Share Posted January 23, 2015 inst in this case is the weapon instance, right?owner = inst.components.inventoryitem.ownerlevel = owner.components.sanity.current Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-605047 Share on other sites More sharing options...
Kzisor Posted January 23, 2015 Share Posted January 23, 2015 @DarkXero, so what you are saying is that it should just be caster instead of caster.inst in the following line? @Batmanwarrior, based on what DarkXero stated, we need to change the following. Change:if SanityCheck(caster.inst) then To:if SanityCheck(caster) then Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-605053 Share on other sites More sharing options...
Batmanwarrior Posted January 23, 2015 Author Share Posted January 23, 2015 Thank you very much Kzisor and DarkXero, it now works as intended.I changed SanityCheck(caster.inst) to SanityCheck(caster), and also removed the inst.components.sanity.DoDelta(-50) in the HealFunc (got a nil variable error). The sanity is being drained correctly in the SanityCheck function anyways =) Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-605111 Share on other sites More sharing options...
Kzisor Posted January 23, 2015 Share Posted January 23, 2015 Thank you very much Kzisor and DarkXero, it now works as intended.I changed SanityCheck(caster.inst) to SanityCheck(caster), and also removed the inst.components.sanity.DoDelta(-50) in the HealFunc (got a nil variable error). The sanity is being drained correctly in the SanityCheck function anyways =) Inside the heal function it has to be caster.components.sanity:DoDelta(-50) as the inst is of the wand. I haven't done much with items so this was additional knowledge for myself as well. Link to comment https://forums.kleientertainment.com/forums/topic/49495-need-help-with-a-single-line-of-code/#findComment-605149 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