"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 » Tue Apr 04, 2017 1:30 pm

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 » Tue Apr 04, 2017 2:24 pm

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: 378
Joined: Sat Sep 24, 2016 11:20 am
Contact:

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

Post by yetneverdone » Wed Apr 05, 2017 5:04 am

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 » 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)."

Lucyy
Prole
Posts: 42
Joined: Thu Oct 15, 2015 6:57 am
Contact:

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

Post by Lucyy » Wed Apr 05, 2017 1:38 pm

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 » Wed Apr 05, 2017 2:07 pm

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: 378
Joined: Sat Sep 24, 2016 11:20 am
Contact:

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

Post by yetneverdone » Wed Apr 05, 2017 3:01 pm

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 » Thu Apr 06, 2017 12:53 pm

Davisdude's solution worked, thanks for the help.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 54 guests