Jump to content

Need help with a single line of code.


Recommended Posts

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 by Batmanwarrior
Link to comment
Share on other sites

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()

​etc
Just look at the source file and you will find all you need.

Link to comment
Share on other sites

@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
Share on other sites

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	endend

So 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
Share on other sites

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 = true

And 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.")	endend

I 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
Share on other sites

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
Share on other sites

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 = true

And 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.")	endend

I 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 by Kzisor
Link to comment
Share on other sites

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.current

I 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
Share on other sites

@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
Share on other sites

@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 by Kzisor
Link to comment
Share on other sites

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 then

Maybe 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
Share on other sites

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
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...