"Attempt to index global x a (a nil value)" error

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
MichaelShort
Prole
Posts: 14
Joined: Mon Sep 28, 2015 3:28 pm

"Attempt to index global x a (a nil value)" error

Post by MichaelShort »

What I'm trying to do is make the variable x for the textbutton, so that I can continue positioning and sizing x after I've created it. I'm using this https://love2d.org/wiki/Easy_GUI_System for the GUI.

One of the solutions I've tried using:

Code: Select all

for i=1, 3 do
	x = "button"..i
	TextButton:new(x)
	x.position = {10, 10}
end
and I've tried using:

Code: Select all

for i=1, 3 do
	x = TextButton:new("button"..i)
	x.position = {10, 10}
end
Sorry for being confusing and thanks for any help.
DireMitten
Prole
Posts: 9
Joined: Thu Jun 23, 2016 3:07 pm

Re: "Attempt to index global x a (a nil value)" error

Post by DireMitten »

Hmm. Seems like the second one should work. Could you try with this, and tell us what the console prints? You can run Love2D games with the console just by dragging and dropping your project folder onto lovec.exe

Code: Select all

for i=1, 3 do
	print(i, "button" .. i)
	x = TextButton:new("button"..i)
	print(x, x.position)
	x.position = {10, 10}
	print(x.position)
end
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: "Attempt to index global x a (a nil value)" error

Post by yetneverdone »

I think you should use reference the x variable as local, since each iteration in the for loop would override it.

Code: Select all

for i=1,3 do
	local x = "button"..i
	TextButton:new(x)
	x.position = {1,1}
end
MichaelShort
Prole
Posts: 14
Joined: Mon Sep 28, 2015 3:28 pm

Re: "Attempt to index global x a (a nil value)" error

Post by MichaelShort »

@DireMitten, the output prints "1 Button1" and then it errors again with "attempt to index global x (a nil value)."

@yetneverdone, that solution also outputs "attempt to index global x (a nil value)."
Lucyy
Citizen
Posts: 51
Joined: Thu Oct 15, 2015 6:57 am
Contact:

Re: "Attempt to index global x a (a nil value)" error

Post by Lucyy »

Could you post your code? I believe it'd be easier to find the problem that way
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: "Attempt to index global x a (a nil value)" error

Post by davisdude »

To me, it looks like the library sets the variable to whatever is passed as the string. So you would access it using something like this:

Code: Select all

x = _G["button" .. i]
x.position = { 1, 1 }
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: "Attempt to index global x a (a nil value)" error

Post by yetneverdone »

MichaelShort wrote: Wed Apr 05, 2017 12:58 pm @DireMitten, the output prints "1 Button1" and then it errors again with "attempt to index global x (a nil value)."

@yetneverdone, that solution also outputs "attempt to index global x (a nil value)."
It has to do with the textbutton library. Could you show us the "new" function of that?
MichaelShort
Prole
Posts: 14
Joined: Mon Sep 28, 2015 3:28 pm

Re: "Attempt to index global x a (a nil value)" error

Post by MichaelShort »

Davisdude's solution worked, thanks for the help.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 37 guests