Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to inherit class from another file in the same project?

in Questions Posts: 17

I have this file

----------------
-- Class: Animal 
----------------

Animal = class()

function Animal:init( word )
    self.talk = word
end

function Animal:speak()
    print( self.talk )
end

----------------
-- Class: Dog
----------------

Dog = class(Animal)

function Dog:init()
    Animal.init( self, "Woof" )
end

----------------
-- Main
----------------

function setup()
    d = Dog()
    d:speak()

    c = Animal("Meow")
    c:speak()
end

This works no problem, but if you separate the classes across two files, it will not inherit the Animal class. How do I separate the class in two files and still be able to inherit?

Comments

  • Jmv38Jmv38 Mod
    Posts: 3,295

    1/ from your project A editor, tap the + sign on top right.
    2/ in 'dependencies', check the other project B with you classes.
    tadaaaaa! project A knows your classes in B

  • edited May 2015 Posts: 17

    I figured it out, file execution order matters. The classes are excuted from left to right in the tab, I had my super class execute after the subclass inherited, so my subclass was inheriting when the superclass was still null.

  • Posts: 17

    @Jmv28 thanks! I didn't make my self clear that I was talking about the same project

  • Posts: 17

    You know I didn't see anywhere in the doc about execution order, but it turns out it's a thing. Is it me or is it actually not documented?

  • dave1707dave1707 Mod
    Posts: 7,530

    Probably not documented. There are a few thing that aren't documented, you'll find those as you write more code.

  • IgnatzIgnatz Mod
    Posts: 5,396

    It's a quirky thing about Codea/Lua. You can also have multiple copies of the same functions, and Codea will use the right hand one.

  • Posts: 212

    @Simeon had to pick some order for compiling the tabs. The choices available were either alphabetical, time created, or how they're ordered in the project.

  • Posts: 2,020

    Tab ordering is documented here: https://bitbucket.org/TwoLivesLeft/core/wiki/CodeaMain

    Although it could perhaps add that subclasses need to be below/ to the right of their parents

Sign In or Register to comment.