DoktorHolmes Posted May 4, 2016 Share Posted May 4, 2016 (edited) Hello everyone! I seem to be having an issue with another piece of code of mine. I am trying to make a custom component that adds a UI text object and updates the text with the value of a variable called "count." It all works just fine until I update the UI object itself where it won't recognize it at all, even as a variable built-in to the class. It always just comes up as having a nil value. The UI object I am trying to update is referred to as the "manabar" in the code, and the function that always returns the error is "mana:UpdateManaBar". Here it is; would anyone mind error checking it for me? local mana = Class(function(self, inst, count, maximum, countstring, manabar) self.inst = inst count = 1 countstring = tostring(count) maximum = 1000 manabar = "null" end) function mana:reset() mana.count = 1 mana.countstring = tostring(mana.count) mana.maximum = 1000 end function mana:updateCountString() mana.countstring = tostring(mana.count) mana:UpdateManaBar(mana.manabar) end function mana:lowermana(inst, lowercount) mana.count = mana.count - lowercount if mana.count < 0 then mana.count = 0 end mana:updateCountString() end function mana:addmana(inst, addcount) mana.count = mana.count + addcount if mana.count < 0 then mana.count = 0 end mana:updateCountString() end function mana:AddManaBar(inst) local Text = require "widgets/text" local easing = require "easing" local Widget = require "widgets/widget" inst:DoTaskInTime( 0.5, function() local controls = inst.HUD.controls mana.manabar = controls:AddChild(Text(NUMBERFONT, 28, "123")) mana.manabar:SetHAnchor(ANCHOR_MIDDLE) mana.manabar:SetVAnchor(0.4) mana.manabar:SetScale(1,.78,1) mana.manabar:Show() mana.manabar:SetString(mana.countstring) end) end function mana:UpdateManaBar(manabar) manabar:SetString(mana.countstring) end return mana Thank you so much! -DoktorHolmes Edited May 4, 2016 by DoktorHolmes Link to comment Share on other sites More sharing options...
Muche Posted May 4, 2016 Share Posted May 4, 2016 (edited) There seem to mixed up class and instance variables. Also constructor function mostly just sets values to local variables that get immediately forgotten. Maybe try something like this? local mana = Class(function(self, inst) self.inst = inst self.count = 1 self.maximum = 1000 self.manabar = nil end) function mana:reset() self.count = 1 self.maximum = 1000 end function mana:lowermana(lowercount) self.count = math.clamp(self.count - lowercount, 0, self.maximum) self:UpdateManaBar() end function mana:addmana(addcount) self.count = math.clamp(self.count + addcount, 0, self.maximum) self:UpdateManaBar() end function mana:AddManaBar() local Text = require "widgets/text" local easing = require "easing" local Widget = require "widgets/widget" self.inst:DoTaskInTime( 0.5, function() local controls = self.inst.HUD.controls self.manabar = controls:AddChild(Text(NUMBERFONT, 28, tostring(self.count))) self.manabar:SetHAnchor(ANCHOR_MIDDLE) self.manabar:SetVAnchor(0.4) self.manabar:SetScale(1,.78,1) self.manabar:Show() end) end function mana:UpdateManaBar() self.manabar:SetString(tostring(self.count)) end return mana Edited May 4, 2016 by Muche Link to comment Share on other sites More sharing options...
DoktorHolmes Posted May 4, 2016 Author Share Posted May 4, 2016 (edited) Thanks, I've tried to fix it up a bit (for example, the HUD.controls is suppposed to be assigned to the player) but it still doesn't seem to want to work. It's still returning a nil value for some of the components. local mana = Class(function(self, inst) self.inst = inst self.count = 1 self.maximum = 1000 self.manabar = nil end) function mana:reset() self.count = 1 self.maximum = 1000 end function mana:lowermana(lowercount) self.count = math.clamp(self.count - lowercount, self.maximum) self:UpdateManaBar() end function mana:addmana(addcount) self.count = math.clamp(self.count + addcount, self.maximum) self:UpdateManaBar() end function mana:AddManaBar(pl) local Text = require "widgets/text" local easing = require "easing" local Widget = require "widgets/widget" self.inst:DoTaskInTime( 0.5, function() local controls = pl.HUD.controls self.manabar = controls:AddChild(Text(NUMBERFONT, 28, tostring(self.count))) self.manabar:SetHAnchor(ANCHOR_MIDDLE) self.manabar:SetVAnchor(0.4) self.manabar:SetScale(1,.78,1) self.manabar:Show() end) end function mana:UpdateManaBar() self.manabar:SetString(tostring(self.count)) end mana:AddManaBar() return mana The error is the following: scripts/components/mana.lua(27,1): attempt to index field 'inst' (a nil value) It's occuring on the line "self.inst:DoTaskInTime( 0.5, function()" Any ideas? Edited May 4, 2016 by DoktorHolmes Link to comment Share on other sites More sharing options...
DoktorHolmes Posted May 5, 2016 Author Share Posted May 5, 2016 (edited) Okay, I've fixed that problem, but manabar is still being returned as a nil value. (Line 38): attempt to index field 'manabar' (a nil value) The script, once more: local mana = Class(function(self, inst) self.inst = inst self.count = 1 self.maximum = 1000 self.manabar = nil end) function mana:reset() self.count = 1 self.maximum = 1000 end function mana:lowermana(lowercount) self.count = math.clamp(self.count - lowercount, self.maximum) self:UpdateManaBar() end function mana:addmana(addcount) self.count = math.clamp(self.count + addcount, self.maximum) self:UpdateManaBar() end function mana:AddManaBar(pl) local Text = require "widgets/text" local easing = require "easing" local Widget = require "widgets/widget" self.inst:DoTaskInTime( 0.5, function() local controls = pl.HUD.controls self.manabar = controls:AddChild(Text(NUMBERFONT, 28, tostring(self.count))) self.manabar:SetHAnchor(ANCHOR_MIDDLE) self.manabar:SetVAnchor(0.4) self.manabar:SetScale(1,.78,1) self.manabar:Show() end) end function mana:UpdateManaBar() self.manabar:SetString(tostring(self.count)) end return mana This really is quite odd. Any help you can give is appreciated! Edited May 5, 2016 by DoktorHolmes Adding code Link to comment Share on other sites More sharing options...
DoktorHolmes Posted May 5, 2016 Author Share Posted May 5, 2016 (edited) Never mind! I have fixed it! Thanks for your help! Edited May 5, 2016 by DoktorHolmes Link to comment Share on other sites More sharing options...
Muche Posted May 5, 2016 Share Posted May 5, 2016 I'm glad you've got that fixed, I have a feeling it had to do with the way the mana instance was used in other parts. Anyways, math.clamp function (defined in mathutil.lua expects 3 parameters, number, min and max). In the code above I can see only two. I am not sure, if it's caused by the forum's formatting (I had to specifically set no syntax highlighting, otherwise it would alter the pasted code), or if it is indeed as such on your side as well. 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