Attemp to index nil with number error

So, i have print function, and got that error:

function Environment:print()
	local pv = self.P:getXY()
	
	-- wmap generation
	
	local wmap = {}
	for j=1, self.n do
		wmap[j] = {}
		for i=1, self.n do
			wmap[j][i] = "_"
		end
	end
	
	self.wmap = wmap
	
	-- wmap generation
	
	-- setup wmap values
	
	self.wmap[pv.x][pv.y] = "@" -- error here
	
	for i, en in pairs(self.ens) do
		local ev = en:getXY()
		self.wmap[ev.x][ev.y] = "#" -- and sometimes here too
	end
	
	-- print wmap
	
	local wmapString = ""
	
	for j, r in pairs(self.wmap) do
		if wmapString:len() > 0 then wmapString = wmapString .. "\n" end
		for i, c in pairs(r) do
			wmapString = wmapString .. c
		end
	end
	
	Print.Text = wmapString
	
end

Anybody can help me with this please?

Not sure what’s up. Could you like elaborate which one you’re trying to set? I’m assuming pv.y

Im trying to print simulation environment, and by self.wmap[pv.x][pv.y] = “@” i setup agent character and etc.
so i think problem is in agent moving, i trying fix it, damn

I’m a liiiiitle bit confused. So, youre trying to set the value of pv.y which is inside pv.x to “@”. Am I right?

Yeah, im trying to place a character “@” to [pv.x][pv.y] position in wmap array

Have you tried printing [pv.x][pv.y] to check if it nil or not?

Nope, but i know it agent position, so i think it make error cus of agent movement
Anyway im gonna try to print it

But can you try reverse because i think is gonna not print it error.



now im see the problem

1 Like

Uhhh, which one is it? chsrsrs

okay so, its problem of agent movement, so i need fix it, damn

1 Like

Both, when agent moves on (0, 0) or (0, 1) and etc. it print error cus minimum array index it (1, 1)

1 Like

what does getXY() return in that line

Alright! Glad you found it out!

Assuming it returns a table. chsr

so, it returns position of actor

function Actor.new(self, x, y)
	self.x = x
	self.y = y
	
	self.actions = {
		Vector2.new(1, 0),
		Vector2.new(-1, 0),
		Vector2.new(0, 1),
		Vector2.new(0, -1),
		Vector2.new(1, 1),
		Vector2.new(-1, -1),
		Vector2.new(1, -1),
		Vector2.new(-1, 1),
	}
	
	self.getXY = function ()
		return Vector2.new(self.x, self.y)
	end
end

yea I know but the problem might be in the function because it’s returning nil

Well, is this your getXY function?

oh, so when u call Actor.new and have X and Y as the arguments they won’t update and they’ll stay the way it was when u passed it as a argument

no, its don’t returns nil