Jump to content

Recommended Posts

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 by ClownTech
Edited the code

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

 

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 by ClownTech

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.

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) 

 

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.

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

 

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.

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 by ClownTech

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.

And then arises a new problem, it decides to give me this error which isnt even part of my code.image.thumb.png.e79edd6dd61260f5eef6b2892076c2a9.png

i dont even know what to do at this point

 

EDIT (Fixed this)

had self.AddChild instead of self:AddChild

Edited by ClownTech
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

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 by ClownTech

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
×
  • Create New...