function pointers in classes

I’m trying to wrap my head around function pointers.

say I have a class set up like this:

Obj = class()

Obj.TEST1_ANIM = { f=Obj.anim1, name="anim1" }
Obj.TEST2_ANIM = { f=Obj.anim2, name="anim2" }

function Obj:init()
  self.anim = nil
end

function Obj:anim1()
  print( "Obj:anim1()" )
end

function Obj:anim2()
  print( "Obj:anim2()" )
end

--end class

function setup()
  ob = Obj()
  ob.anim = Obj.TEST1_ANIM
  print( Obj.TEST1_ANIM ) -- prints an address
  print( Obj.TEST1_ANIM.f ) -- prints "nil"
  print( ob.anim.f )  -- prints "nil"
  ob.anim.f(self)  -- errors out
end

in my head, I’m expecting the 2nd and 3rd print to print the same address.

after some testing, I discovered that you have to put the structures AFTER all of the functions:


Obj = class()


function Obj:init()
  self.anim = nil
end

function Obj:anim1()
  print( "Obj:anim1()" )
end

function Obj:anim2()
  print( "Obj:anim2()" )
end

-- put the function structures with names AFTER function implementations so the parser can find them
Obj.TEST1_ANIM = { f=Obj.anim1, name="anim1" }
Obj.TEST2_ANIM = { f=Obj.anim2, name="anim2" }

--end class

function setup()
  ob = Obj()
  ob.anim = Obj.TEST1_ANIM
  print( Obj.TEST1_ANIM ) -- prints an address
  print( Obj.TEST1_ANIM.f ) -- prints an address
  print( ob.anim.f )  -- prints same address as above
  ob.anim.f(self)  -- calls the proper function
end

you must be aware that there are 3 processes:
1/ compilation.
2/ run setup().
3/ create obj instance.
things not already defined at a moment in these processes are nil.
You have put your definition in the compilation. Hence the order problem.
You could put it in setup(), then there is not the ptoblem.
Or in the init()


Obj = class()


function Obj:init()
  self.anim = nil
  self.TEST1_ANIM = { f=Obj.anim1, name="anim1" }
  self.TEST2_ANIM = { f=Obj.anim2, name="anim2" }
end

function Obj:anim1()
  print( "Obj:anim1()" )
end

function Obj:anim2()
  print( "Obj:anim2()" )
end

--end class

function setup()
  ob = Obj()
  ob.anim = Obj.TEST1_ANIM
  print( Obj.TEST1_ANIM ) -- prints an address
  print( Obj.TEST1_ANIM.f ) -- prints an address
  print( ob.anim.f )  -- prints same address as above
  ob.anim.f(self)  -- calls the proper function
end

my purpose for putting them outside of the init is so that every instance doesn’t make an exclusive copy of the data. I was trying to do something similar to static class structs. Perhaps I am misunderstanding how classes and class properties work?

@matkatmusic - you can have class-wide properties that are shared by all instances, just name them like so

--this can be outside a function or inside
--it is a property of the class in general
Obj.width=3 
--then any instance of the class (or any code anywhere) can get the value
a=Obj.width

You can also write class functions that can be used by any code anywhere. Any function that doesn’t rely on “self” variables can (if you want) be defined with a dot and called with a dot, eg

function Obj.Cube(n)
    return n*n*n
end
--you can call it from inside or outside the class with
x=Obj.Cube(4)

this might help a little, too

http://coolcodea.wordpress.com/2013/04/12/31-a-lesson-from-the-roller-coaster-demo/