Creating modules

About Monkey 2 Forums Monkey 2 Programming Help Creating modules

This topic contains 8 replies, has 2 voices, and was last updated by  Mark Sibly 3 years ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #950

    peterigz
    Participant

    Hello, just downloaded the latest version of Monkey2 and everything just works out of the box so far, seems like things are really coming along nicely 🙂

    I’m looking to convert some modules over, but just wondering about how I should go about it in terms of organising the files. Cut a long story short, can I import just one part of a module? For example in blitzmax/monkey I could do…

    Import rigz.collision

    …if I just wanted to use my collision module only. Is it possible or should I be splitting things into separate module folders? Thanks!

    #954

    Mark Sibly
    Keymaster

    Each module is physically ‘atomic’, ie: you either import all of it or none of it.

    However, each file in a module can be in it’s own namespace so you can logically split things up within a module.

    So you could choose to put everything in a single ‘rigz’ module, which users would import with:

    #Import “<rigz>”

    Then, if they wanted to ‘use’ just the collision stuff…

    Using rigz.collision

    Note that the other non-collision stuff in the rigz module is still available, only users will need to fully qualify symbols, eg:

    rigz.system.Blah()   ‘need this ‘coz we’re not Using rigz.system

    The ‘Using’ just means they can skip the full qualification.

    My advice for now would be to stick everything in one module. If you are using the lastest mx2 build, you will also need a ‘module.json’ file in the module dir that should have at least this in it:

     

     

     

     

     

     

     

    #979

    peterigz
    Participant

    Ahh ok, that makes sense, thanks 🙂

    #991

    peterigz
    Participant

    Regarding the “Modules must not have cyclic dependancies.”, in monkey I had collision.monkey, and quadtree.monkey, they both imported each other which won’t work now. So should I basically have them both in one file or is there a different way of going about it?

    #992

    Mark Sibly
    Keymaster

    A module is not a file – it’s a bunch of files contained in a top-level directory inside ‘modules’.

    Files within a module can reference each other no problem.

    This was kind of confused in monkey1 – there’s what people think of as the ‘mojo’ module that contains all the graphics, audio, input etc stuff (in mostly separate files), but there is also the mojo ‘module'(!) which is the file called ‘mojo.monkey’.

    But in mx2, a module is just a bunch of related files that you stick in the same dir.

    I suggest having a look at the modules/mojo folder and files to get an idea of how it works. Note that image.monkey2 can reference stuff declared in texture.monkey2, and vice versa if necessary. They don’t even have to #import each other (although they can if they want) – all the #importing of monkey2 files is done in the main mojo.monkey2 file, sort of like a ‘makefile’.

    Also note that each file starts with a ‘Namespace’. This is roughly equivalent to the namespace mx1 implicitly creates for each file (or ‘module’!). But it’s way more flexible, because it’s multi-level AND has nothing to do with the actual file path, meaning you can move files around freely without breaking stuff! How I’d love to split up mojo2’s graphics.monkey file…

     

     

    #993

    peterigz
    Participant

    I see, I was confusing myself where I’m just converting monkey 1 code and running each file individually to catch errors and seeing duplicate identifiers. Now I’ve moved all my imports into the main module file and everything makes sense again, I can just run the main file now and it reports errors on all the imported files (or just makemods to do the same). Thanks!

    #994

    Mark Sibly
    Keymaster

    seeing duplicate identifiers

    Actually, that could be a bug.

    Files should be able to cyclically #import each other – basically, only the first #import of a particular file actually does anything, successive #imports of the same file are ignored (in theory).

    If it happens again and you can reproduce, please post some sample code!

     

     

    #1012

    peterigz
    Participant

    Hi Mark, this is what caused the Duplicate identifiers, simple test module:

    Module name “test” with files:

    test.monkey2:

    [/crayon]

    something.monkey2:

    [/crayon]

    I guess it mattered more in monkey where you could import only part of a module if you needed too so those individual imports needed to be there in each file as dependencies, but now that the whole module is imported you can organise it differently which I quite like, feels less messy.

    #1015

    Mark Sibly
    Keymaster

    It’s a bug, will fix!

     

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.