Codea thinks classes are functions? FIXED

This program that I have been working is behaving like some of my classes are functions.

On my iPad, it errors out at

ball = Ball()

in the code below, saying that there inst any “function parameters”

Game = class()

function Game:init()
    gameState = PLAYING
    rope:ropeUpdate
    ball = Ball()
    walls = Walls()
    tac = Tac()
    rope = Rope()

end

Here is my ball class

Ball = class()

function Ball:init()
    
    self.d = 75
    self.ball = physics.body(CIRCLE,self.d/2)
    self.ball.x = WIDTH/2
    self.ball.y = HEIGHT/2
    self.ball.gravityScale = 1
    self.ball.restitution = .5
    self.ball.friction = 1
    self.ball.linearVelocity = vec2(0,253)

end

function Ball:draw()
    
    pushStyle()
    fill(229, 16, 36, 255)
    ellipse(self.ball.x,self.ball.y, self.d)
    popStyle()

end

function Ball:getBall()
    return self.ball
end

But, here’s where the plot thickens, in air code, it errors in main when I try to create a Game() with

game = Game()

In the following code of Main saying that its a function just like the ball error.

-- Ball Project
function setup()
    
    game = Game()
 
end


function draw()
    background(31, 32, 53, 255)
    
    game:draw()
    
end

It especially confuses me why I’m getting 2 different error codes. Is the Air Code editor broken or something?

And I NEVER define a function any of the names of my classes.

Any ideas as to what the cause of this is? I’ve tried for a couple hours and think that a different set of eyes would do it well. Thank you, who ever looks at this to help me out

Ill paste my complete Game class to down below

Game = class()

function Game:init()
    gameState = PLAYING
    rope:ropeUpdate
    ball = Ball()
    walls = Walls()
    tac = Tac()
    rope = Rope()

end

function Game:draw()
    
    self:ropeUpdate
    rope:draw()
    ball:draw()
    walls:draw()
    tac:draw()  
    
end

function Game:ropeUpdate()
    if rope:getRope == nil then
        rope:ropeCreateCheck(ball:getBall(), tac:getTac())
    end
end

Rope:ropeUpdate needs brackets on the end!

*facepalm

Thanks, problem solved. I spent over an hour racking my brains for some deep hidden secret to this madness…

From what I can tell, Lua doesn’t have classes. Everything in Lua is either an atomic type (int, number, string, boolean) or a table, which is really a fancy kind of array. (Technically, it’s an “associative array”, also known as a collection in MS languages.) Lua simply treats function definitions as another type of value in the table.

So when you’re using myObject=ClassName() to instantiate an object, what you’re really doing is creating a copy of a variable named “ClassName”.

Here’s something to try:

function setup()
  x={} -- creates a blank table
  x["hi"]="hello"
  print(x.hi)
end

as you can see, syntatically, x.hi is the same as x[“hi”]. This leads to some very interesting lexical possibilities… but that’s another conversation.

Short version: yes. Instantiating a class is a function call. No more, no less. =)

@tomxp411

Lua has a few basic primitive types plus table, which can be used as a hash table or a more or less traditional array indexed by integers.

While object-orientation isn’t built in, it’s very easy to tack on. There are many different approaches, one of which is used by Codea.

However, although OOPishness isn’t built in, it’s still pretty OOPy.

It’s easy to throw together a little library that provides most of the traditional OOPish bits you’d actually use (inheritance, polymorphism, encapsulation, etc.).

You can use the equivalent of a static method as a constructor (SomeClass.new) or an initializer similar to the Objective-C Way (SomeClass:init).

Codea’s implementation makes use of the __call metamethod which lets you specify a function to be used if the user “calls” your table (class/object).

Ignoring the underlying bits, which most devs do, the .Net Way sets aside some keywords and conventions to turn static methods into constructors, which really isn’t all that different from a typical OOPish Lua library.

You can even (I do) roll your own pseudo-strong typing. It requires a little more hackery, but I think it’s worth it. And because Lua is ridiculously flexible (you’re far more likely to bend it than break it), you can replace the built in type function with a wrapper of your own that, by the time you’re done, makes it all feel very natural.

c# is my favorite language, and .Net is, on the whole, gobsmackingly amazing, but if you want to hack away on your iWhatever, Lua’s hard to beat.

At first glance, I was disappointed. You just have to throw yourself into the voodoo of Lua to realize its potential and smear yourself in its magnificence.