ClownTech Posted April 25, 2023 Share Posted April 25, 2023 (edited) I've made a mod which uses widgets and after attempting to set x and y values for on screen i get this error Attempt to perform arithmetic on field 'x' (a nil value) local widget = require "widgets/widget" local text = require "widgets/text" local DeathCountUI = Class(widget, function(self, inst, anchor_x, anchor_y) self.inst = inst self.x = anchor_x self.y = anchor_y widget._ctor(self, "deathcountUI") self.title = self:AddChild(text(BODYTEXTFONT, 33, "Death Count: ", UICOLOURS.WHITE)) self.title:SetPosition(self.x - 100, self.y + 20) self.deaths = self.AddChild(text(BODYTEXTFONT, 33)) self.deathsval = 0 inst:ListenForEvent("killed", function(inst, data) self.deathsval = self.deathsval + 1 end) -- self:StartUpdating() end) return DeathCountUI I'm Still relatively new to coding and don't understand this error Edited April 25, 2023 by ClownTech Edited the code Link to comment https://forums.kleientertainment.com/forums/topic/147375-attempt-to-perform-arithmetic-on-field-x-a-nil-value/ Share on other sites More sharing options...
-LukaS- Posted April 25, 2023 Share Posted April 25, 2023 field - a variable that's part of a class nil - An empty value (doesn't exist) You're attempting to perform a mathematical operation on 'nothing'. You must be creating a DeathCountUI object without passing in parameters. DeathCountUI() -- WRONG DeathCountUI(inst, 150, 200) -- CORRECT Link to comment https://forums.kleientertainment.com/forums/topic/147375-attempt-to-perform-arithmetic-on-field-x-a-nil-value/#findComment-1631825 Share on other sites More sharing options...
ClownTech Posted April 25, 2023 Author Share Posted April 25, 2023 (edited) Sorry, im not quite sure where this slots into this, I have the code local deathCountUI = Class(widget, function(self, inst, anchor_x, anchor_y) which creates the class, I Dont understand where your deathCountUI(inst, 150, 200) Comes into this Edited April 25, 2023 by ClownTech Link to comment https://forums.kleientertainment.com/forums/topic/147375-attempt-to-perform-arithmetic-on-field-x-a-nil-value/#findComment-1631827 Share on other sites More sharing options...
-LukaS- Posted April 25, 2023 Share Posted April 25, 2023 DeathCountUI is the name of your class that, I'm assuming, you're using to add a widget to PlayerHud. Since the error is that x is nil you must be using DeathCountUI without providing any parameters. DeathCountUI needs 3 parameters inst, anchor_x, anchor_y. 'self' is a hidden parameter so you don't need to pass in any value there. The 150 and 200 that I used are just as an example, you can use any numerical values. Link to comment https://forums.kleientertainment.com/forums/topic/147375-attempt-to-perform-arithmetic-on-field-x-a-nil-value/#findComment-1631836 Share on other sites More sharing options...
ClownTech Posted April 25, 2023 Author Share Posted April 25, 2023 im adding deathcountUI to StatusDisplay, is what your trying to say this? local deathcountUI = Class(widget, function(inst, 150, 200) instead of local deathcountUI = Class(widget, function(self, inst, anchor_x, anchor_y) Link to comment https://forums.kleientertainment.com/forums/topic/147375-attempt-to-perform-arithmetic-on-field-x-a-nil-value/#findComment-1631840 Share on other sites More sharing options...
-LukaS- Posted April 25, 2023 Share Posted April 25, 2023 Where is the line that adds DeathCountUI to StatusDisplays, can you send it? local deathcountUI = Class(widget, function(self, inst, anchor_x, anchor_y) This line does not add the death counter to StatusDisplay. It only declares the DeathCountUI class. Link to comment https://forums.kleientertainment.com/forums/topic/147375-attempt-to-perform-arithmetic-on-field-x-a-nil-value/#findComment-1631844 Share on other sites More sharing options...
ClownTech Posted April 25, 2023 Author Share Posted April 25, 2023 Im just going to send ModMain aswell as The code in the widgets folder Modmain; Spoiler local deathcountUI = require "/widgets/deathcounterwidget" AddClassPostConstruct("widgets/statusdisplays", function(self) self.owner.deathcounterwidget = self:AddChild(deathcountUI(self.owner)) end) Widget Spoiler local widget = require "widgets/widget" local text = require "widgets/text" local deathcountUI = Class(widget, function(inst, anchor_x, anchor_y) self.inst = inst self.x = anchor_x self.y = anchor_y widget._ctor(self, "deathcountUI") self.title = self:AddChild(text(BODYTEXTFONT, 33, "Death Count: ", UICOLOURS.WHITE)) self.title:SetPosition(self.x - 100, self.y + 20) self.deaths = self.AddChild(text(BODYTEXTFONT, 33)) self.deathsval = 0 inst:ListenForEvent("killed", function(inst, data) self.deathsval = self.deathsval + 1 end) -- self:StartUpdating() end) return deathcountUI Link to comment https://forums.kleientertainment.com/forums/topic/147375-attempt-to-perform-arithmetic-on-field-x-a-nil-value/#findComment-1631845 Share on other sites More sharing options...
-LukaS- Posted April 25, 2023 Share Posted April 25, 2023 Ah, here's the problem: local deathcountUI = require "/widgets/deathcounterwidget" AddClassPostConstruct("widgets/statusdisplays", function(self) self.owner.deathcounterwidget = self:AddChild(deathcountUI(self.owner)) end) See, here you're calling 'deathcountUI' with 'self.owner' as the only parameter. You need to add 2 numerical values to that since the class takes 3 parameters. Without them these 2 values: self.x = anchor_x self.y = anchor_y don't exist and so you're getting the 'x is nil' error. Link to comment https://forums.kleientertainment.com/forums/topic/147375-attempt-to-perform-arithmetic-on-field-x-a-nil-value/#findComment-1631849 Share on other sites More sharing options...
ClownTech Posted April 25, 2023 Author Share Posted April 25, 2023 (edited) So your saying all i need is to add 2 numericals like this? local deathcountUI = require "/widgets/deathcounterwidget" AddClassPostConstruct("widgets/statusdisplays", function(self) self.owner.deathcounterwidget = self:AddChild(deathcountUI(self.owner, 150, 200)) end) I tried this, I think this is what you mean but then i get the error that line 5 widget code, that self isn't declared, im wondering if this is because i removed self from the code like you told me might've been the original problem? Edited April 25, 2023 by ClownTech Link to comment https://forums.kleientertainment.com/forums/topic/147375-attempt-to-perform-arithmetic-on-field-x-a-nil-value/#findComment-1631851 Share on other sites More sharing options...
-LukaS- Posted April 25, 2023 Share Posted April 25, 2023 Oh, you shouldn't have removed 'self' from class declaration: local deathcountUI = Class(widget, function(self, inst, anchor_x, anchor_y) When I was talking about omitting 'self' i meant this part: self.owner.deathcounterwidget = self:AddChild(deathcountUI(self.owner, 150, 200)) Just as a little tip. But you're fine like this. Just keep these 2 lines how they're here. Keep 'self.owner' and you can change 150 and 200 to a different number if you want. Link to comment https://forums.kleientertainment.com/forums/topic/147375-attempt-to-perform-arithmetic-on-field-x-a-nil-value/#findComment-1631854 Share on other sites More sharing options...
ClownTech Posted April 25, 2023 Author Share Posted April 25, 2023 (edited) And then arises a new problem, it decides to give me this error which isnt even part of my code. i dont even know what to do at this point EDIT (Fixed this) had self.AddChild instead of self:AddChild Edited April 25, 2023 by ClownTech Link to comment https://forums.kleientertainment.com/forums/topic/147375-attempt-to-perform-arithmetic-on-field-x-a-nil-value/#findComment-1631856 Share on other sites More sharing options...
ClownTech Posted April 25, 2023 Author Share Posted April 25, 2023 58 minutes ago, -t- said: Oh, you shouldn't have removed 'self' from class declaration: local deathcountUI = Class(widget, function(self, inst, anchor_x, anchor_y) When I was talking about omitting 'self' i meant this part: self.owner.deathcounterwidget = self:AddChild(deathcountUI(self.owner, 150, 200)) Just as a little tip. But you're fine like this. Just keep these 2 lines how they're here. Keep 'self.owner' and you can change 150 and 200 to a different number if you want. i did do this and after fixing another issue i get the error :15: attempt to index local 'inst' (a nil value) i dont understand alot of the nil stuff and don't understand how to fix alot of it Link to comment https://forums.kleientertainment.com/forums/topic/147375-attempt-to-perform-arithmetic-on-field-x-a-nil-value/#findComment-1631867 Share on other sites More sharing options...
-LukaS- Posted April 25, 2023 Share Posted April 25, 2023 Can you send both of the files again? Link to comment https://forums.kleientertainment.com/forums/topic/147375-attempt-to-perform-arithmetic-on-field-x-a-nil-value/#findComment-1631870 Share on other sites More sharing options...
ClownTech Posted April 25, 2023 Author Share Posted April 25, 2023 (edited) 9 minutes ago, -t- said: Can you send both of the files again? i fixed most of this, the game loads the files, except for the fact that it dosent load the numbers and title, so i cant see what its loading Spoiler local widget = require "widgets/widget" local text = require "widgets/text" local deathcountUI = Class(widget, function(self, inst, anchor_x, anchor_y) self.inst = inst self.x = anchor_x self.y = anchor_y widget._ctor(self, "deathcountUI") self.title = self:AddChild(text(BODYTEXTFONT, 33, "Death Count: ", UICOLOURS.WHITE)) -- dosent show any of these on screen, so i cant see if its working self.title:SetPosition(self.x - 100, self.y + 20) self.deaths = self:AddChild(text(BODYTEXTFONT, 33)) self.deathsval = 0 self.title:Show() inst:ListenForEvent("killed", function(inst, data) self.deathsval = self.deathsval + 1 end) -- self:StartUpdating() end) return deathcountUI Spoiler local deathcountUI = require "/widgets/deathcounterwidget" AddClassPostConstruct("widgets/statusdisplays", function(self) self.owner.deathcounterwidget = self:AddChild(deathcountUI(self.owner, 150, 200)) end) Edited April 25, 2023 by ClownTech Link to comment https://forums.kleientertainment.com/forums/topic/147375-attempt-to-perform-arithmetic-on-field-x-a-nil-value/#findComment-1631872 Share on other sites More sharing options...
-LukaS- Posted April 25, 2023 Share Posted April 25, 2023 You might have to adjust the positioning. 150 and 200 might be too much. Try starting with 0, 0 and changing it as fit. If you can't see it no matter the position values there might be a different problem. Link to comment https://forums.kleientertainment.com/forums/topic/147375-attempt-to-perform-arithmetic-on-field-x-a-nil-value/#findComment-1631875 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