Problem - attempt to concatenate local 'y' (a nil value)

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
clepto
Prole
Posts: 35
Joined: Wed Jul 25, 2012 10:20 pm

Problem - attempt to concatenate local 'y' (a nil value)

Post by clepto »

Hello, i am beginner in love so take it easy on me :crazy:

i have watch goature tutorial until number 12 and i tried to create an entity based on his tutorial...

when i try to run i get

Code: Select all

Error: entities/hero.lua:9: attempt to concatenate local 'y' (a nil value)
stack traceback:
	entities/hero.lua:9: in function 'load'
	entities.lua:30: in function 'Create'
	main.lua:11: in function 'load'
	[string "boot.lua"]:378: in function <[string "boot.lua"]:373>
	[C]: in function 'xpcall'
i attached the .love file

any help-suggestions and anything else is more than welcome :)
Attachments
Blondy.love
(74.51 KiB) Downloaded 122 times
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Problem - attempt to concatenate local 'y' (a nil value)

Post by Qcode »

So from what I can tell, on that line you are trying to print the hero's x and y. You have this.

Code: Select all

 print("x is " .. x .. " and y is " .. y)
Which is a perfectly acceptable line of code. So now lets trace back a bit farther to find where the problem is.
That line of code is found in this function

Code: Select all

function ent:load( x, y )
Which is also a correct line of code. The x and y must be defined in a higher function though. Lets go back one more step. This function is called in line 30 in entities.lua

Code: Select all

ent:load()
Though this would be fine, you can see you're missing out on the x and y on the calling.

Code: Select all

ent:load(x, y)
Is what it needs to be changed to.
If you don't define the x and y higher up, then they don't exist, and therefore cannot be printed.
clepto
Prole
Posts: 35
Joined: Wed Jul 25, 2012 10:20 pm

Re: Problem - attempt to concatenate local 'y' (a nil value)

Post by clepto »

yes your are right! thanks!

but now i get

Code: Select all

Error: entities/hero.lua:4: attempt to index local 'self' (a number value)
stack traceback:
	entities/hero.lua:4: in function 'setPos'
	entities/hero.lua:10: in function 'load'
	entities.lua:30: in function 'Create'
	main.lua:11: in function 'load'
	[string "boot.lua"]:378: in function <[string "boot.lua"]:373>
	[C]: in function 'xpcall'
why?

edit: is there a diffirence in using . or : ? no right?
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Problem - attempt to concatenate local 'y' (a nil value)

Post by Nixola »

There is. Writing ent:load(x, y) is the same as writing ent.load(ent, x, y) but not as writing ent.load(x, y)
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Petunien
Party member
Posts: 191
Joined: Fri Feb 03, 2012 8:02 pm
Location: South Tyrol (Italy)

Re: Problem - attempt to concatenate local 'y' (a nil value)

Post by Petunien »

Lua supports a little bit OOP.
The keyword "self" is for objects of a class. But ignore that until you got better and have some experience. :)

If you are interested in it: http://lua-users.org/wiki/ObjectOrientationTutorial
"Docendo discimus" - Lucius Annaeus Seneca
clepto
Prole
Posts: 35
Joined: Wed Jul 25, 2012 10:20 pm

Re: Problem - attempt to concatenate local 'y' (a nil value)

Post by clepto »

so whats the right one? the "." ?

@petunien, so i just delete self.? how to solve the problem?
User avatar
Petunien
Party member
Posts: 191
Joined: Fri Feb 03, 2012 8:02 pm
Location: South Tyrol (Italy)

Re: Problem - attempt to concatenate local 'y' (a nil value)

Post by Petunien »

Both, the "." and the ":" have specific jobs. The ":" is in my opinion useful for objects.

Regarding the error, no, don't just delete it. It won't work then.

I had a look in the code, but actually, I don't understand much.

Please wait until a more experienced user is gonna help you. :)
"Docendo discimus" - Lucius Annaeus Seneca
clepto
Prole
Posts: 35
Joined: Wed Jul 25, 2012 10:20 pm

Re: Problem - attempt to concatenate local 'y' (a nil value)

Post by clepto »

Thanks! :)

lets get back to your problem :P
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Problem - attempt to concatenate local 'y' (a nil value)

Post by Santos »

Try changing line 10 of hero.lua to:

Code: Select all

self:setPos( x, y )
Using a . instead of a : on this line meant that ent:setPos was receiving only two numbers, instead of the entity itself and two numbers. So the self variable was actually set to the number that x is, hence "attempt to index local 'self' (a number value)". This... is a confusing explanation. :D

*Finding something from a previous thread!*

function t:f(x, y)

is simply the same as

function t.f(self, x, y)

(They both have the parameter "self". Using the ":", it is hidden!)

and

t:f(x, y)

is simply the same as

t.f(t, x, y)

(Using the ":", the table sends itself as the first parameter.)

If this is still confusing, don't worry. :D Just call things function which look like function e.f() with e.f(), and functions which look like function e:f() with e:f().

Hope this helps! ^^
clepto
Prole
Posts: 35
Joined: Wed Jul 25, 2012 10:20 pm

Re: Problem - attempt to concatenate local 'y' (a nil value)

Post by clepto »

thanks! i change the line to

Code: Select all

ent:setPos( x, y )
(also change the self. with ent.) and it running but i cant see anywhere my hero entity...

i have study to do :crazy:
Post Reply

Who is online

Users browsing this forum: No registered users and 91 guests