Problem with SECS and self. [FIXED]

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
xXOdysseusXx
Prole
Posts: 15
Joined: Sun Nov 13, 2011 4:56 pm

Problem with SECS and self. [FIXED]

Post by xXOdysseusXx »

Hello! I'm a relatively new programmer. I learned how to use lua A LONG time ago with a little game called Roblox :awesome:. Having moved to making my own 2d games with Love2d, I have encountered my first major problem. I can't seem to get self.(field) to be recognized as a value of the class. Here is my code for the class.

Code: Select all

require("lib/SECS")

Circle = class:new()

function Circle:init(dt)
	self.xspeed = 0
	self.yspeed = 0
	self.x = love.graphics.getWidth()/2 - 25
	self.y = love.graphics.getHeight()/2
	self.radius = 25
end

function Circle:draw()
	love.graphics.setColor(255,0,0)
	love.graphics.circle("fill",self.xpos,self.ypos,self.radius,50)
end

function Circle:update(dt)
	if self.y >= (love.graphics.getHeight() - self.radius)  then
	self.y = love.graphics.getHeight() - self.radius + 1
	end
	Circle:processKeys()
	Circle:processSpeed(dt)
end

function Circle:processKeys()
self.yspeed = 0
self.xspeed = 0
	if love.keyboard.isDown( "w" ) == true then
		self.yspeed = -100
	end
	if love.keyboard.isDown( "s" ) == true then
		self.yspeed = 100
	end
	if love.keyboard.isDown( "a" ) == true then
		self.xspeed = -100
	end
	if love.keyboard.isDown( "d" ) == true then
		self.xspeed = 100
	end
end

function Circle:processSpeed(dt)
self.y = self.y + self.yspeed * dt
self.x = self.x + self.xspeed * dt
end
My error will usually be something like "attempt to perform arithmatic on field 'blah' (a nil value). :x Anybody know what's wrong? If you need to see the full code please request.
Last edited by xXOdysseusXx on Sun Nov 13, 2011 7:51 pm, edited 1 time in total.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Problem with SECS and self.

Post by tentus »

I think I see your problem, let me trim this down for you:

Code: Select all

function Circle:init(dt)
	self.x = love.graphics.getWidth()/2 - 25
	self.y = love.graphics.getHeight()/2
	self.radius = 25
end

function Circle:draw()
	love.graphics.circle("fill", self.xpos, self.ypos, self.radius, 50)
end
Do you see it now?

Generally speaking, the error screens in Love are actually really useful. If they say they have an error with a variable name, tracking down that variable will often reveal your bug. This is why consistent, descriptive variable names are so important.
Kurosuke needs beta testers
xXOdysseusXx
Prole
Posts: 15
Joined: Sun Nov 13, 2011 4:56 pm

Re: Problem with SECS and self.

Post by xXOdysseusXx »

Thanks for the help! That was a simple error. But I'm still getting a problem.

This time I'll be more specific. It says

Code: Select all

Circle.lua:44: attempt to perform arithmetic on field 'y' (a nil value)
This one doesn't seem as easy as a fix. :?

EDIT: Like 44 is

Code: Select all

self.y = self.y + self.yspeed * dt
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Problem with SECS and self.

Post by tentus »

Ah, that one's a little trickier. The problem is that

Code: Select all

Circle:processKeys()
Circle:processSpeed(dt)
should be

Code: Select all

self:processKeys()
self:processSpeed(dt)
Get kikito or bartbes to explain how the semicolon + self works, I've always been rubbish at explaining it.

Also, a cool trick that you might find useful:

Code: Select all

if love.keyboard.isDown( "w" ) then
is the same as

Code: Select all

if love.keyboard.isDown( "w" ) == true then
Kurosuke needs beta testers
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Problem with SECS and self.

Post by TechnoCat »

tentus wrote:Get kikito or bartbes to explain how the semicolon + self works, I've always been rubbish at explaining it.
http://www.lua.org/manual/5.1/manual.html#2.5.8 wrote:A call v:name(args) is syntactic sugar for v.name(v,args), except that v is evaluated only once.
xXOdysseusXx
Prole
Posts: 15
Joined: Sun Nov 13, 2011 4:56 pm

Re: Problem with SECS and self.

Post by xXOdysseusXx »

Awesome thanks for the help guys! :awesome:
Post Reply

Who is online

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