difference between Include and Import?
BlitzMax Forums/BlitzMax Beginners Area/difference between Include and Import?
| ||
just curious what the difference is... And also, how to use them properly and run included/imported BMX files in the main file. |
| ||
Something like this. |
| ||
Hehe. I wondered the same thing about a week ago. Test code:' include.bmx Print "Included" ' include test.bmx Include "include.bmx" Include "include.bmx" ' import.bmx Print "Imported" ' import test.bmx Import "import.bmx" Import "import.bmx"Include just inserts the code where you specify. Import builds and links the code once per file. It is recursive in that it will import the imports. |
| ||
Include just inserts the code where you specify. Import builds and links the code once per file. It is recursive in that it will import the imports. Thanks for telling me, but I have 2 more questions. First, does the included code compile with the main code (into a single executable) when using Include, or does it need subfolders with the code files in there? I wonder the same about Import, though I'm pretty sure Import causes the imported code files to compile with the main program. Second, how does one initialize the code in the included files? judging by what I've seen in other peoples' code, I assume it'd go like this: Import "data.bmx" and it is called just like a function with: data.init() I am probably mistaken. |
| ||
how does one initialize the code in the included files? Anything at the root-level of the source file will be executed automatically (anything outside functions,types that is) |
| ||
Include just takes the code in a file and sticks it in your main file as if you had copy/pasted it. Subfolders are optional. I don't follow the second question. |
| ||
When you compile (for your final executable) EVERYTHING is put into the exe - you don't need to distribute any code files with your final game, in subfolders, includes or otherwise. |
| ||
I don't follow the second question. I just thought that when you include/import, the included files were simply recognized by the program (analogous to defining a function or type, but not yet calling it in the main loop,) but you execute the included code by using some sort of init() thingie in the main loop. This theory of mine explains, to my limited understanding, why so many programs have all the includes at the very beginning of the program. Do you understand now? |
| ||
Yes I think so. You need to include code before you use it, but you don't need to init it. You just start using whatever stuff is inside it right out the box, as it were. |
| ||
To be completely Clear If this is my code: Main.bmx Global A=1 Include "Extras.bmx" Print A Extras.bmx A=2 The program will print "2". EDIT: I should point out that this is (intentionally) a very bad use of include files. You should never use include files in such a manner because it makes it very hard to read your program later. I've written this purely to illustrate how Include works. |
| ||
Yes I think so. You need to include code before you use it, but you don't need to init it. You just start using whatever stuff is inside it right out the box, as it were. Wait, so that means it executes right at the exact time it is "included." That is not good for me. What if I wanted to load it in memory and execute it in the main loop? I plan on using separate code files for user input, graphics/sound setup, etc. and as such, when I include a file which is supposed to test input, it needs to be ran repeatedly as in a loop. Putting the INCLUDE/IMPORT code in the main loop just seems incorrect to me. There HAS to be another way. |
| ||
No, the code is not executed automatically. It is *available* to be executed. So if you have a function sayHello() in your include file, and you include that file in your main file, the function sayHello() is available in your main file too. You can then use sayHello() in your main file. |
| ||
Wait, so that means it executes right at the exact time it is "included." It almost appears as if you think that BlitzMax is an interpreted language? When you Include a file, ALL the code from the included file is essentially PASTED into the exact part of the file where the line with Include was. It is not really pasted, but that is the easiest way to think about it. When the compiler compiles the code, it sees one LARGE file. Import is different. Each Imported file is compiled separately, creating individual object files, which are linked into your binary, eventually - which is why you Import at the top of your file. |
| ||
Wait, so that means it executes right at the exact time it is "included." That is not good for me. What if I wanted to load it in memory and execute it in the main loop? I plan on using separate code files for user input, graphics/sound setup, etc. and as such, when I include a file which is supposed to test input, it needs to be ran repeatedly as in a loop. Putting the INCLUDE/IMPORT code in the main loop just seems incorrect to me. There HAS to be another way. Fortunately, the way you want to do it, is exactly the way to do it, and the way you don't want to do it, is the way you shouldn't do it. :) |
| ||
Import is meant to be used in conjunction with Framework, so that you can manually specify which modules are included in your exe file, as well as being a way to import third party modules. |