Page 1 of 2

Noob here.

Posted: Thu Dec 03, 2009 7:52 am
by Millow
Iv'e been messing with love/lua for about 3 days now, and I've hit a snag while trying to make a simple side scrolling beat'em up. I get the error, main.lua: 43: '}' expected (to close'{' at line 2 'draw', and can't seem to figure out what the problem is.

Here is line 43, draw, I know it's probably some of the worst coding you have ever seen, so feel free to point out any other issues. I'm just trying to grasp this programming thing.
draw = function(self)
if self.facing == 1 then }
if self.y <= 480 then self.vspeed = self.vspeed + self.gravity else
love.graphics.draw(self.walking_right, self.x, self.y)
else if
love.graphics.draw(self.falling_right, self.x, self.y)
end
else if self.facing == -1 then
if self.y <= 480 then self.vspeed = self.vspeed + self.gravity else
love.graphics.draw(self.walking_left, self.x, self.y)
else if
love.graphics.draw(sellf, self.x, self.y)
end
else if love.graphics.draw(self.falling_left, self.x, self.y)

end
end
}

Re: Noob here.

Posted: Thu Dec 03, 2009 8:04 am
by subrime
There are 3 problems:

(1) curly braces
You have scattered some curly braces in there... lua does not use them like c.

So try changing:
if self.facing == 1 then }
into:
if self.facing == 1 then

and remove the final } at the end.

(2) basic structure
The if keyword can be used in the following ways:

Code: Select all

-- one case
if test then
  -- do something
end

-- two cases
if test then
  -- do something
else
  -- do something else
end

-- many cases
if test1 then
  -- do something
elseif test2 then
  -- do something else
elseif test3 then
  -- and so on
else
  -- getting the idea?
end
(3) spelling
You also have a typo:
love.graphics.draw(sellf, self.x, self.y)
Look carefully at the first argument (sellf)

It looks like you might get some benefit from going over basic lua docs.

Re: Noob here.

Posted: Thu Dec 03, 2009 8:25 am
by Millow
Thanks for the reply.
I guess I probably should read the docs..... :death:

Re: Noob here.

Posted: Fri Dec 04, 2009 7:57 am
by Millow
What dose, Error in update (arg 2), expected 'float' got nil mean?
function update(self,dt)

if love.keyboard.isDown(love.key_d) then
x = x + (speed * dt)
self.facing = 1
elseif love.keyboard.isDown(love.key_a) then
x = x - (speed * dt)
self.facing = 2
else
facing = 0
end

if love.keyboard.isDown(love.key_s) then
y = y + (speed * dt)
elseif love.keyboard.isDown(love.key_w) then
y = y - (speed * dt)
end

leftanime:update(dt)
rightanime:update(dt)

end

Re: Noob here.

Posted: Fri Dec 04, 2009 9:02 am
by kikito
The error is here:

Code: Select all

function update(self, dt)
The error says: Error in update (arg 2), expected 'float' got nil - this means that "in the update function (that accepts two parameters) I was expecting a float value, but I got a nil value instead"

This happens because the 'self' parameter is an undefined (nil) variable. update() only needs one parameter (dt). Try removing self so it looks like this:

Code: Select all

function update(dt)
And see how it goes.

Re: Noob here.

Posted: Fri Dec 04, 2009 2:36 pm
by Robin
Actually, ;)

dt is nil here. Because:

Code: Select all

function test(a, b)
    print("a is", a, "b is", b)
end

test(0.1) -- prints: a is 0.1     b is nil
update() is called with one argument, which we usually call dt. If you define more arguments, only the first will contain that value, the rest will be nil. And if a value is nil, you can't do arithmetics with it.

The solution kikito gave is correct, though.

Re: Noob here.

Posted: Sun Dec 06, 2009 11:51 am
by Millow
Thanks! I removed it and it compiled, :rofl:

Re: Noob here.

Posted: Sun Dec 06, 2009 6:13 pm
by osgeld
you could use curly braces in lua, it usually ignores them, but you have to have propper {} pairs for that to work

Re: Noob here.

Posted: Sun Dec 06, 2009 6:32 pm
by Robin
osgeld wrote:you could use curly braces in lua, it usually ignores them, but you have to have propper {} pairs for that to work
Not in standard Lua you can't. Syntax error.

Re: Noob here.

Posted: Sun Dec 06, 2009 7:19 pm
by osgeld
well poop!

I remember it working somewhere in lua, maybe some aicent version, not that i ever really tried