Problem importing other monkey-files

Monkey Forums/Monkey Programming/Problem importing other monkey-files

chimaera(Posted 2011) [#1]
Hi,

I can't seem to get my head around the import-command in Monkey which is note only very annoying and but also makes me a bit ashamed. :(

I have imported an external monkey code file into my main-file. I then try to do a LoadString-command in the imported file. I get a compile error stating: Identifier "LoadString" not found.

I won't share all the code (simply because it is too much). Here is the initial part of the main code file.

Import mojo
Import assetManager

Global myApp:MyApp

Function Main()
	myApp = New MyApp
End

Class MyApp Extends App
	Field assetManager:c_assetManager

	Method OnCreate()
		assetManager = New c_assetManager
		assetManager.init()
	End

	Method OnUpdate()
	...
	End

	Method OnRender()
	...
	End
End


And here is the imported file-code, called assetManager.monkey (placed in the same directory as the main file):

Class c_assetManager
	Method New()
		Print "[assetManager.create] - Starting assetManager"
	End
	
	Method init()
		Print LoadString ("asset_index.txt")
		'The asset_index.txt is in program.data-folder
	End
End


So this won't compile with the error I mentioned above. To test so that there was no problem with the LoadString-command I copy/pasted that line (see above) straight into OnCreate() in the main file (and I removed the import of assetManager.monkey), and that worked like a charm: printing the content of the txt-file.

So I guess this is a something of a scope issue or something the like. It seems that even though assetManager.monket is successfully imported it can't seem to reach "mojo"-commands from within it self.

Ok, long story. I hope that someone can help me, since it is driving me crazy.


GfK(Posted 2011) [#2]
Put Import mojo at the top of assetManager.monkey.


chimaera(Posted 2011) [#3]
That answer was to simple, but hey: It totally worked. Thanks for making me looking more of an idiot. HEHE.

But why is not the the mojo-import inherited (or shared) from the Main-file? Maybe I am a bit confused, but wasn't that how it worked in Bmax (not saying that this is exactly the same language)?

Thanks so much GfK!


GfK(Posted 2011) [#4]
I asked the same question and was given an explanation which I understood at the time, but I can't remember any more. :)

[edit] http://www.jungleide.com/?page_id=33/jungle-ide-group4/general-discussion-forum5/intellisense-problems-thread166

All to do with how the compiler works - it does make sense to keep everything modular with its own dependencies because code is more portable that way.


ziggy(Posted 2011) [#5]
Yes, each file has to have all the dependencies it uses. Monkey allow cyclic imports, so not a big deal and code is kept clean and self-contained. It's much better than the BlitzMax import system.
also, imports are relative to the file where they appear following this rules:

The import is relative to:
1.- The file where the Import command appears, if the file can't be found this way:
2.- The monkey entry point file, if the file can't be found this way:
3.- The modules folder, and if it is not resolved:
4.- Compilation error


Shinkiro1(Posted 2011) [#6]
I have struggled with this stuff myself and in the end placed all monkey files in one folder (and importing dependencies in every other file)
Is there a way to import files from a parent folder?


ziggy(Posted 2011) [#7]
Is there a way to import files from a parent folder?

Yes. You can make imports relative to the program entry point.
mainfile.monkey
folder1/secondfile.monkey
folder1/thirdfile.monkey
folder1/folder2/anotherfile.monkey

If the file folder1/folder2/anotherfile.monkey is imported by mainfile.monkey at any point (even if it is imported by a file importing it) you can add "Import mainfile" in folder1/folder2/anotherfile.monkey and it'll work.
Remember that program entry point folder works itself like the mod folder and anything there is accesible using the import path.


DruggedBunny(Posted 2011) [#8]
You can do this too:

mainfile.monkey:

Import mojo

Import import1
Import import2

Function Main () ..


import1.monkey:

Import mainfile

Function Test1 ()
	Print "1"
End



import2.monkey:

Import mainfile

Function Test2 ()
	Print "2"
End



This way, all imports get access to the contents of all other imports, as they inherit the imports made in mainfile.monkey, including mojo.

If you want to place all of the imports in a sub-folder (eg. called imports)...

mainfile.monkey:

Import mojo

Import imports.import1 ' Subfolder_name . monkey_file [no extension]
Import imports.import2

Function Main ()
	Print "Hello"
	Test1
	Test2
End


You don't need to change Import mainfile in the import files -- they still look as they do up there ^^ and will find mainfile automatically.