Importing code

BlitzMax Forums/BlitzMax Beginners Area/Importing code

Fry Crayola(Posted 2005) [#1]
Just a quick question on importing code.

Let's say I have three files, called A, B and C.

A is the "main" file. B and C are additional functions and data. B uses some of the functions of C, while C is standalone. A uses functions of both B and C.

Where should the "import" commands go?

I tried putting the two commands in A, like so:

import "File B"
import "File C"


But to no avail. During compilation, Max replied (using Strict mode) that it couldn't find a Type that was accessed in B, but declared in C. So I swapped the files, but that had no effect.

I got a working solution by importing file B from file A (if you follow), and importing C from B.

However I'm concerned. I plan to have many future files - D, E, F and so forth, each corresponding to different areas of the game. Each will want to access the other. Do I simply include the import code in each file? So, for example, File B will have:

import "File C"


Whilst file E may have
import "File B"
import "File C"
import "File D"


This won't cause any bloating of the exe file will it? Will BlitzMax recognise that File C has already been compiled?

Is there a better way of managing the imports, or is this correct?

Thanks!


PGF(Posted 2005) [#2]
I take it that the files are actually named A.bmx, B.bmx and C.bmx. If so then the correct thing to do would be to put the following into your main file A.bmx:
Import "B.bmx"
Import "C.bmx"

That's how I do it. There doesn't appear to be a problem with the order of definitions in the respective files.


Perturbatio(Posted 2005) [#3]
You might need to import C into B and A, Import is different from include.

*EDIT*
Think of Import as local to the file you use it in and include being global.


Fry Crayola(Posted 2005) [#4]
Thanks guys. Yeah, the files are named correctly (not that they're actually A, B, and C, of course).

Import as local. Now it makes sense. I'm used to the old Includes from Blitz3D.

Ta!


PGF(Posted 2005) [#5]
Whoops. I mistook Import for Include.

My example should have been:
Include "B.bmx"
Include "C.bmx"

I started out attempting to use Import for a project but found a major problem. Types defined in different included modules can't refer to each other - that is B contain a reference to C and C contain a reference to B.

There are workarounds such as deriving B and C from the same parent but I found this impractical due to the large object hierarchy in my project. The other alternative is to put them in one file which leads back to what you want to avoid.

Also I was disappointed to find that project build time became much longer with Import. I had expected it to be shorter since modules are only rebuilt if necessary. However any change to a type further up the hierarchy caused every descendent to rebuild. Seemed like every build involves overhead that ends up costing more than the simpler way.

So now I just have a main file that uses Include to pull in about 20 separate files. One per type. The whole thing builds in about 5 seconds which is fast enough that it hardly matters.